-1
我正在使用按鈕的單擊事件將datagridview導出到Excel。數據正在導出,但表格的最後一行未顯示。我玩過我的代碼,但我無法弄清楚問題所在。如果有人會幫助我,我會很感激。datagridview導出爲excel不顯示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;
}
}
}
最後行您導出的是datagridview9.Rows.Count - 2(從for循環的最大值和行[j-2]引用)。這顯然是倒數第二排。 – 2014-09-01 14:04:02
順便說一句,這個問題本身很好,但由於它看起來像一個簡單的錯字,我已經標記爲關閉它,因爲它不太可能有助於其他人。 – 2014-09-01 14:06:15