IM使用此代碼輸出一個gridview表到Microsoft Excel:導出的GridView表到Excel
private void button5_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] = "Distance";
for (int j = 2; j <= datagridview1.Rows.Count; j++)
{
for (int i = 2; i <= 3; i++)
{
ws.Cells[j, i] = datagridview1.Rows[j - 2].Cells[i - 1].Value;
}
}
其工作,但第一個欄只顯示沒有數據的標題文字,我不知道是什麼問題,並且我試圖對代碼做一些更改,但我無法達到解決方案,請提供任何幫助?
它,當我按下按鈕給我的錯誤,該錯誤表示: COM異常是未處理的 異常來自HARESULT 0X800A03EC – 2014-08-28 07:48:31