Получение ошибки при экспорте данных datagridview в excel на языке C#
Hello Guys, i am facing an issue and that is i want to export Datagridview data in excel and i have code am getting this code from here Export DataGridView To Excel In C#[^] Export DataGridView To Excel In C#[^] but the issue is when I export so it's saved automatically I want it's asking me where I want to save and second when it saves so I get this error Additional information: Exception from HRESULT: 0x800A03ECand one more thing I don't want to exit application after saving this file. please help me guys, please. Thank you.
Что я уже пробовал:
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; // see the excel sheet behind the program app.Visible = true; // get the reference of first sheet. By default its name is Sheet1. // store its reference to worksheet worksheet = workbook.Sheets["Sheet1"]; worksheet = workbook.ActiveSheet; // changing the name of active sheet worksheet.Name = "Exported from gridview"; // storing header part in Excel for (int i = 1; i < dataGridView4.Columns.Count+1; i++) { worksheet.Cells[1, i] = dataGridView4.Columns[i - 1].HeaderText; } // storing Each row and column value to excel sheet for (int i = 0; i < dataGridView4.Rows.Count - 1; i++) { for (int j = 0; j < dataGridView4.Columns.Count; j++) { worksheet.Cells[i + 2, j + 1] = dataGridView4.Rows[i].Cells[j].Value.ToString(); } } // save the application workbook.SaveAs("Desktop:\\output.xls", Type.Missing);
CHill60
Я предлагаю вам использовать форум там, где вы нашли код в первую очередь