[英]Error while exporting from a DataGridView into an Excel Worksheet
我將一些數據收集到DataGridView
“ dataGridView2”中,並想將其導出到Excel中的工作表中,但是出現了HRESULT
錯誤,錯誤代碼為0x800A03EC
,以下是代碼:
private void button2_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application app = new.Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = app.Workbooks.Open(@path);
Microsoft.Office.Interop.Excel.Worksheet worksheet = workbook.ActiveSheet;
//Couunt the number cells in the
//datagridview asign it to an integers
//variable
int rHtcount = dataGridView2.Rows.Count;
int iii = 0;
//put each values from each cells of the
//datagridview into each cell in the
//destination
for(; iii < rHtcount; iii++)
{
worksheet.Cells[iii + 2, "H"].Value = dataGridView2.Rows[iii].Cells[0].Value;
}
app.Visible = true;
workbook.Close();
}
請幫助我該怎么辦,因為我認為上面的代碼是正確的,而且我不為什么會Exception from HRESULT: 0x800A03EC
拋出這樣的錯誤Exception from HRESULT: 0x800A03EC
嘗試替換new.Microsoft.Office.Interop.Excel.Application();
帶有new Microsoft.Office.Interop.Excel.Application();
HTH !!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.