DataGridView导出到Excel
2008-09-06 10:03:43 来源:WEB开发网3、成功引用了Excel之后,怎么使用?对于将DataGridView导出成Excel的应用来说,估计大家需要考虑的就是性能问题,如果一个导出操作要用户等几分钟甚至十几分钟的话,是很难让很多人接受的。具体的解释我也不做了,就把代码贴在这里好了。
view plaincopy to clipboardprint?
public void DataGridView2Excel(System.Windows.Forms.DataGridView datagridview,string SheetName)
{
int iRows = 0;
int iCols = 0;
int iTrueCols = 0;
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Add(System.Reflection.Missing.Value);
Microsoft.Office.Interop.Excel.Worksheet ws = null;
if (wb.Worksheets.Count > 0)
{
ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(1);
}
else
{
wb.Worksheets.Add(System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(1);
}
if (ws != null)
{
if (SheetName.Trim() != "")
{
ws.Name = SheetName;
}
iRows = datagridview.Rows.Count; //加上列头行
iTrueCols = datagridview.Columns.Count; //包含隐藏的列,一共有多少列
//求列数,省略Visible = false的列
for (int i = 0; i < datagridview.Columns.Count; i++)
{
if (datagridview.Columns[i].Visible) iCols++;
}
string[,] dimArray = new string[iRows + 1, iCols];
for (int j = 0, k = 0; j < iTrueCols; j++)
{
//省略Visible = false的列
if (datagridview.Columns[j].Visible)
{
dimArray[0, k] = datagridview.Columns[j].HeaderText;
k++;
}
}
for (int i = 0; i < iRows; i++)
{
for (int j = 0, k = 0; j < iTrueCols; j++)
{
//省略Visible = false的列
if (datagridview.Columns[j].Visible)
{
dimArray[i + 1, k] = datagridview.Rows[i].Cells[j].Value.ToString();
k++;
}
}
}
ws.get_Range(ws.Cells[1, 1], ws.Cells[iRows + 1, iCols]).Value2 = dimArray;
ws.get_Range(ws.Cells[1, 1], ws.Cells[1, iCols]).Font.Bold = true;
ws.get_Range(ws.Cells[1, 1], ws.Cells[iRows + 1, iCols]).Font.Size = 10.0;
ws.get_Range(ws.Cells[1, 1], ws.Cells[iRows + 1, iCols]).RowHeight = 14.25;
//ws.Columns[.ColumnWidth = datagridview.Columns[0].Width;
for (int j = 0, k = 0; j < iTrueCols; j++)
{
//省略Visible = false的列
if (datagridview.Columns[j].Visible)
{
ws.get_Range(ws.Cells[1, k + 1], ws.Cells[1, k + 1]).ColumnWidth = (datagridview.Columns[j].Width / 8.4) > 255 ? 255 : (datagridview.Columns[j].Width / 8.4);
//ws.Columns.c = datagridview.Columns[j].Width;
k++;
}
}
}
app.Visible = true;
}
public void DataGridView2Excel(System.Windows.Forms.DataGridView datagridview,string SheetName)
{
int iRows = 0;
int iCols = 0;
int iTrueCols = 0;
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Add(System.Reflection.Missing.Value);
Microsoft.Office.Interop.Excel.Worksheet ws = null;
if (wb.Worksheets.Count > 0)
{
ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(1);
}
else
{
wb.Worksheets.Add(System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(1);
}
if (ws != null)
{
if (SheetName.Trim() != "")
{
ws.Name = SheetName;
}
iRows = datagridview.Rows.Count; //加上列头行
iTrueCols = datagridview.Columns.Count; //包含隐藏的列,一共有多少列
//求列数,省略Visible = false的列
for (int i = 0; i < datagridview.Columns.Count; i++)
{
if (datagridview.Columns[i].Visible) iCols++;
}
string[,] dimArray = new string[iRows + 1, iCols];
for (int j = 0, k = 0; j < iTrueCols; j++)
{
//省略Visible = false的列
if (datagridview.Columns[j].Visible)
{
dimArray[0, k] = datagridview.Columns[j].HeaderText;
k++;
}
}
for (int i = 0; i < iRows; i++)
{
for (int j = 0, k = 0; j < iTrueCols; j++)
{
//省略Visible = false的列
if (datagridview.Columns[j].Visible)
{
dimArray[i + 1, k] = datagridview.Rows[i].Cells[j].Value.ToString();
k++;
}
}
}
ws.get_Range(ws.Cells[1, 1], ws.Cells[iRows + 1, iCols]).Value2 = dimArray;
ws.get_Range(ws.Cells[1, 1], ws.Cells[1, iCols]).Font.Bold = true;
ws.get_Range(ws.Cells[1, 1], ws.Cells[iRows + 1, iCols]).Font.Size = 10.0;
ws.get_Range(ws.Cells[1, 1], ws.Cells[iRows + 1, iCols]).RowHeight = 14.25;
//ws.Columns[.ColumnWidth = datagridview.Columns[0].Width;
for (int j = 0, k = 0; j < iTrueCols; j++)
{
//省略Visible = false的列
if (datagridview.Columns[j].Visible)
{
ws.get_Range(ws.Cells[1, k + 1], ws.Cells[1, k + 1]).ColumnWidth = (datagridview.Columns[j].Width / 8.4) > 255 ? 255 : (datagridview.Columns[j].Width / 8.4);
//ws.Columns.c = datagridview.Columns[j].Width;
k++;
}
}
}
app.Visible = true;
}
Tags:DataGridView 导出 Excel
编辑录入:爽爽 [复制链接] [打 印]- ››Excel 2010:用数据透视表对数据立体化分析
- ››Excel 2010:数据分析透视图的用法
- ››Excel 2010:简洁实用的迷你图
- ››Excel 2010:快速分析数据之数据条
- ››Excel 2010:快速创建丰富的数据图表
- ››Excel 2010:动态洞察数据的切片器
- ››Excel 2010:使用数据透视表对数据进行立体化分析...
- ››Excel 2010:将数据分析结果分页显示
- ››Excel 2010:数据分析透视图
- ››Excel 2010:快速分析数据之图标集
- ››Excel 2010:快速标注数据表中的前3名数据
- ››Excel 2010:便捷的打印工作表选项
更多精彩
赞助商链接