Missing the latest data when exporting gridview to excel

When I exporting gridview to excel there’s missing data which is the last. Here is my code in exporting data.

private void ToCsV(DataGridView dGV, string filename)
    {
        string stOutput = "";
        // Export titles:
        string sHeaders = "";

        for (int j = 0; j < dGV.Columns.Count; j++)
            sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + "t";
        stOutput += sHeaders + "rn";
        // Export data.
        for (int i = 0; i < dGV.RowCount - 1; i++)
        {
            string stLine = "";
            for (int j = 1; j < dGV.Rows[i].Cells.Count; j++)
                stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + "t";
            stOutput += stLine + "rn";
        }
        Encoding utf16 = Encoding.GetEncoding(1254);
        byte[] output = utf16.GetBytes(stOutput);
        FileStream fs = new FileStream(filename, FileMode.Create);
        BinaryWriter bw = new BinaryWriter(fs);
        bw.Write(output, 0, output.Length); //write the encoded file
        bw.Flush();
        bw.Close();
        fs.Close();
    }

this is the code for the button.

SaveFileDialog sfd = new SaveFileDialog();
        sfd.Filter = "Excel Documents  (.xls)|*.xls";
        sfd.FileName = "";
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            //ToCsV(dataGridView1, @"c:export.xls");
            ToCsV(gvHistory, sfd.FileName); // Here dataGridview1 is your grid view name
        }

You have

for (int i = 0; i < dGV.RowCount - 1; i++)
{
  ...
}

So you explicitly exclude the last row from the export.

Change i < dGV.RowCount - 1
to i < dGV.RowCount
.

Additionally, the code:

string stLine = "";
for (int j = 1; j < dGV.Rows[i].Cells.Count; j++)
  stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + "t";
stOutput += stLine + "rn";

Is basically an example when to use StringBuilder
instead of string
. It will get progressively slower as you get more rows. The better version is:

StringBuilder sbOutput = new StringBuilder();

for (int j = 0; j < dGV.Columns.Count; j++)
   sbOutput.AppendLine(Convert.ToString(dGV.Columns[j].HeaderText) + "t");
for (int i = 0; i < dGV.RowCount - 1; i++)
{
  for (int j = 1; j < dGV.Rows[i].Cells.Count; j++)
    sbOutput.Append(Convert.ToString(dGV.Rows[i].Cells[j].Value) + "t");
  sbOutput.AppendLine();
}
Encoding utf16 = Encoding.GetEncoding(1254);
byte[] output = utf16.GetBytes(sbOutput.ToString());

Next, when iterating over a collection, you can use the for(int i=0; i < length; i++)
pattern, but it’s usually better to simply use a foreach
. That way you are sure that it will
loop all the rows – that’s what a foreach does by definition. The added benefit is that the loop variable has a much nicer name, i.e. row
instead of dGV.Rows[i]
. So the code will be:

foreach(var column in dGV.Columns)
   sbOutput.AppendLine(Convert.ToString(column.HeaderText) + "t");

foreach (var row in dGV.Rows)
{
  foreach (var cell in row.Cells)
    sbOutput.Append(Convert.ToString(cell.Value) + "t");
  sbOutput.AppendLine();
}
Encoding utf16 = Encoding.GetEncoding(1254);
byte[] output = utf16.GetBytes(sbOutput.ToString());

which is way more readable and way less prone to silly ‘off-by-one’ errors.

Lastly, a nitpicking point: the method is called ToCsv
, but the values are not comma-separated, but tab-separated values. It’s not much of an offense, because, somehow, the term “CSV” has evolved to mean any format where rows are on separate lines, and the values in the row are somehow
separated, but still, tab-separated values is a thing.

Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Missing the latest data when exporting gridview to excel

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录