在C#中以excel方式导出datagridview数据时出错 [英] Getting error when I export datagridview data in excel in C#
本文介绍了在C#中以excel方式导出datagridview数据时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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);
推荐答案
如果你想把它保存在其他地方,改变这一行。
之后,查询/询问有关用户输入或传递参数的问题。
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);
这篇关于在C#中以excel方式导出datagridview数据时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文