导出为excel不导出所有行 [英] Export to excel not exporting all rows
问题描述
我想知道是否有人可以帮助我,我使用我收到的示例代码将数据网格视图导出到Excel。 datagridview正确显示所有行,但是当我导出到excel并检入excel时,数据丢失,任何帮助将不胜感激,因为我不知道我错过了什么或做错了
string excel_file = directoryFinance;
int cols;
// 打开文件
StreamWriter wr = new StreamWriter(excel_file);
// 确定列数和写入文件列
cols = dataGridView1.Columns.Count;
for ( int i = 0 ; i < cols; i ++)
{
wr.Write(dataGridView1.Columns [i] .Name.ToString()。 ToUpper()+ \t);
}
wr.WriteLine();
// 将行写入excel文件
for ( int i = 0 ; i < (dataGridView1.Rows.Count - 1 ); i ++)
{
for ( int j = 0 ; j < cols; j ++)
{
if (dataGridView1.Rows [i] .Cells [j] .Value!= null )
wr.Write(dataGridView1.Rows [i] .Cells [j] .Value + \t);
else
{
wr.Write( \t跨度>);
}
}
wr.WriteLine();
}
// 关闭文件
wr.Close() ;
谢谢
Kallie
< blockquote>正如我所见,您正在尝试将数据从DataGridView导出到制表符分隔文件。
一些提示:
1)定义变量,类型: DataGridViewCell [ ^ ],在第二个循环中使用它,
2) DataGridViewCell.Value返回的值 [ ^ 是一样的类型,作为其来源。因此,如果 DGVCell.Value
等于 DBNull.Value
,则它不能无效 [ ^ ]。
Quote:在数据库应用程序中,null对象是有效值一个领域。此类区分空值(空对象)和未初始化值(DBNull.Value实例)。例如,表可以包含未初始化字段的记录。默认情况下,这些未初始化的字段具有DBNull值。
更多关于: null(C#reference) [ ^ ],可空类型(C#) [ ^ ]和 DBNull类 [ ^ ]。
尝试替换:
(dataGridView1.Rows [i] .Cells [j] .Value!= null )
with:
DataGridViewCell cell = this .dataGridView1.Rows [i] .Cells [j];
if (cell.Value.ToString!= DbNull.Value.ToString)
wr.Write(cell.Value.ToString + \t);
else
wr.Write( \t);
看看下面的例子:
http://dotnetask.com/Resource.aspx?Resourceid=644 [ ^ ]
http://csharp.net-informations.com/excel/csharp-excel-datagridview.htm [ ^ ]
http://csharp.net-informations.com/datagridview/csharp-datagridview-export-excel.htm [ ^ ]
你会在CP找到更多;)使用 [搜索]
此网站右上角的文本框。
Hi,
i wonder if anyone can help me, i am exporting a datagridview to excel using sample code i received. the datagridview shows all rows correctly, but when i export to excel and check in excel there is data missing , any help will be appreciated as i am not sure what i am missing or doing wrong
string excel_file = directoryFinance;
int cols;
//open file
StreamWriter wr = new StreamWriter(excel_file);
//determine the number of columns and write columns to file
cols = dataGridView1.Columns.Count;
for (int i = 0; i < cols; i++)
{
wr.Write(dataGridView1.Columns[i].Name.ToString().ToUpper() + "\t");
}
wr.WriteLine();
//write rows to excel file
for (int i = 0; i < (dataGridView1.Rows.Count - 1); i++)
{
for (int j = 0; j < cols; j++)
{
if (dataGridView1.Rows[i].Cells[j].Value != null)
wr.Write(dataGridView1.Rows[i].Cells[j].Value + "\t");
else
{
wr.Write("\t");
}
}
wr.WriteLine();
}
//close file
wr.Close();
Thank you
Kallie
As i see, you are trying to export data from DataGridView to tab-separated file.
Some tips:
1) Define variable, type of: DataGridViewCell[^], to use it inside second loop,
2) Value returned by DataGridViewCell.Value[^] is the same type, as its source. So, ifDGVCell.Value
is equal toDBNull.Value
, then it can''t be null[^].
Quote:In database applications, a null object is a valid value for a field. This class differentiates between a null value (a null object) and an uninitialized value (the DBNull.Value instance). For example, a table can have records with uninitialized fields. By default, these uninitialized fields have the DBNull value.
More about: null (C# reference)[^], Nullable Types (C#)[^] and DBNull Class[^].
Try to replace:
(dataGridView1.Rows[i].Cells[j].Value != null)
with:
DataGridViewCell cell = this.dataGridView1.Rows[i].Cells[j]; if (cell.Value.ToString != DbNull.Value.ToString) wr.Write(cell.Value.ToString + "\t"); else wr.Write("\t");
Have a look at below examples:
http://dotnetask.com/Resource.aspx?Resourceid=644[^]
http://csharp.net-informations.com/excel/csharp-excel-datagridview.htm[^]
http://csharp.net-informations.com/datagridview/csharp-datagridview-export-excel.htm[^]
More you''ll find at CP ;) Use[Search]
textbox in the right-top corner of this site.
这篇关于导出为excel不导出所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!