我遇到了一个新问题,我想将一个datagridview导出到excel并搜索脚本。我找到的脚本在这一行中给我一个错误。
" ExcelApp.Cells [i + 2,j + 1] = dgvTabelle.Rows [i] .Cells [j] .Value.ToString();"
整个脚本看起来像这样!
SaveFileDialog sfd = new SaveFileDialog();
sfd.InitialDirectory = "C:";
sfd.Filter = "Excel File|* .xlsx";
sfd.FileName = "Flugxyz";
sfd.Title = "Speichern von Flugdatei";
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
ExcelApp.Application.Workbooks.Add(Type.Missing);
//Storing header part in Excel
for (int i = 1; i < dgvTabelle.Columns.Count + 1; i++)
{
ExcelApp.Cells[1, i] = dgvTabelle.Columns[i - 1].HeaderText;
}
//Storing Each row and column value to excel sheet
for (int i = 0; i < dgvTabelle.Rows.Count; i++)
{
for (int j = 0; j< dgvTabelle.Columns.Count; j ++)
{
ExcelApp.Cells[i + 2, j + 1] = dgvTabelle.Rows[i].Cells[j].Value.ToString();
}
}
ExcelApp.ActiveWorkbook.SaveCopyAs(sfd.FileName.ToString());
ExcelApp.ActiveWorkbook.Saved = true;
ExcelApp.Quit();