Click here to Skip to main content
14,426,402 members
Rate this:
Please Sign up or sign in to vote.
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.

What I have tried:

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);
Updated 20-Feb-19 5:09am
CHill60 20-Feb-19 9:14am
I suggest you use the forum at wherever you found the code in the first place

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Change this line, if you want to save it somewhere else.

After that, lookup / ask questions about "user input", or "passing parameters".

workbook.SaveAs("Desktop:\\output.xls", Type.Missing);
Member 9983063 20-Feb-19 14:13pm
i don't want to save some where else i just need the solution of this problem please help me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100