关于DataGridView导出到Excel.
Hi,大家好,本人最近在练习做有关datagridview的功能,我有个问题,我现在假设我在为一个企业设计软件显示客户信息,我绑定datagirdview数据库了然后设置在datagridview里面只显示客户的id和姓名,而不显示其他信息,因为当有上千条信息时会很慢,所以这样做可以加快速度,但是问题是这样子的话我从datagridview里导出excel也就只能显示ID和姓名。。请问怎么修改下面的代码可以把导出的excel文件显示客户的所有信息呢?#region 将DataGridView中的数据导入到Excel中,DataGridView无需绑定数据源
/// <summary>
/// 将DataGridView中的数据导入到Excel中,DataGridView无需绑定数据源
/// </summary>
/// <param name="datagridview">DataGridView</param>
/// <param name="SheetName">Excel sheet title</param>
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]; // 需要修改string[iRows + 1, iCols]为string[iRows, iCols]
string[,] dimArray = new string[iRows, 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 i = 0; i < iRows - 1 ; 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.get_Range(ws.Cells[1, 1], ws.Cells[iRows, 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, iCols]).Font.Size = 10.0;
ws.get_Range(ws.Cells[1, 1], ws.Cells[iRows, 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;
}
#endregion --------------------编程问答-------------------- 你可以重新读一个datatable 再导入到excel,,数据绑定到datagridview中是很慢,,如果数据少你可以绑定datagridview中但不显示出来 --------------------编程问答-------------------- 获取数据到dataset数据集,再导出dataset到excel
或根据ID查询用户信息,添加到excel里
--------------------编程问答-------------------- http://topic.csdn.net/u/20091108/01/2893410f-001d-4b35-8252-cd3f941b7be7.html?18341
来看看我写的EXCEL效率谈 希望对你有帮助 --------------------编程问答-------------------- 你可以把客户的所有信息都读到一个list中,但是和datagirdview只绑定想显示的两个字段。
补充:.NET技术 , C#