0
我正在使用此代碼將DataGridView行導出到Excel工作表。使用此導出到Excel代碼無法寫入單元格
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
int i = 0;
int j = 0;
for (i = 0; i <= dgv.RowCount - 1; i++)
{
for (j = 0; j <= dgv.ColumnCount - 1; j++)
{
DataGridViewCell cell = dgv[j, i];
xlWorkSheet.Cells[i + 5, j + 2] = cell.Value;
}
}
string SavedFilePath = Application.StartupPath + "\\GeneratedExcelReports\\";
xlWorkBook.SaveAs(SavedFilePath + ExcelFileNameToCreate + ".xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
如果我加入這行代碼:
xlWorkSheet.Cells[0,1].Value = "My Test Report";
它會產生錯誤,因爲它在智能影音感打開該列表毫無作爲的價值。如何在運行時編輯特定的單元格?
什麼是錯誤? –