我创建了一个Windows窗体应用程序,以从csv读取到gridview,然后从gridview导出到excel。它在我的系统上运行正常,但是我将exe文件发送到另一个系统,并且该exe正在运行,但未导出数据。错误是bad indexing
// creating Excel Application
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
// creating new WorkBook within Excel application
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
// creating new Excelsheet in workbook
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
// store its reference to worksheet
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
// storing header part in Excel
for (int i = 1; i < PandMGDV.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = PandMGDV.Columns[i - 1].HeaderText;
worksheet.Cells[1, i].Font.Bold = true;
}
// storing Each row and column value to excel sheet
for (int gi = 0; gi < PandMGDV.Rows.Count-1; gi++)
{
for (int gj = 0; gj < PandMGDV.Columns.Count; gj++)
{
worksheet.Cells[gi + 2, gj + 1] = PandMGDV.Rows[gi].Cells[gj].Value.ToString();
}
}
string fileName = Interaction.InputBox("Enter the name of the Excel file", "Save Excel File", "Default Text", -1, -1);
//MessageBox.Show(fileName);
workbook.SaveAs(fileName + ".xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
workbook.Close(true, Type.Missing, Type.Missing);
app.Quit();
MessageBox.Show("Excel file created", "File Path is 'c:\\User\\UserName\\MyDocuments\\"+ fileName + ".xls.xls'", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult dialogResult = MessageBox.Show("Do you want to close the application", "Application", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dialogResult == DialogResult.Yes)
{
this.Close();
}
}