datagridview export to excel不显示excel表中的最后一行

时间:2014-09-01 13:58:28

标签: c# excel datagridview export rows

我使用按钮的点击事件将数据网格视图导出到Excel。数据正在导出,但表的最后一行未显示。我玩过我的代码,但我无法弄清楚问题。如果有人会帮助我,我将不胜感激。

private void button34_Click(object sender, EventArgs e)
{
    Microsoft.Office.Interop.Excel.Application Excel = new    
    Microsoft.Office.Interop.Excel.Application();
    Workbook wb = Excel.Workbooks.Add(XlSheetType.xlWorksheet);
    Worksheet ws = (Worksheet)Excel.ActiveSheet;
    Excel.Visible = true;
    ws.Cells[1, 1] = "VehiclePlateNumber";
    ws.Cells[1, 2] = "VehicleDescription";
    ws.Cells[1, 3] = "DeviceCode";
    ws.Cells[1, 4] = "DriverName";
    ws.Cells[1, 5] = "DriverKeyTag";

    for (int j = 2; j <= datagridview9.Rows.Count; j++)
    {
        for (int i = 1; i <= 5; i++)
        {
            ws.Cells[j, i] = datagridview9.Rows[j - 2].Cells[i - 1].Value;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

请将其更改为以下声明并检查一次

  

for(int j = 2; j&lt; = datagridview9.Rows.Count + 1; j ++)