将datagridview中的数据导出到excel表格窗口形式 [英] export data in datagridview to excel sheet windows form
本文介绍了将datagridview中的数据导出到excel表格窗口形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 DataGridView
,当我显示导出到excel工作表按钮时,出现以下错误:
I have DataGridView
and when I display export to excel sheet button following error appeared:
(Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD)))
here
(
xlWorkBook = xlApp.Workbooks.Add(misValue);
)
和我使用
的此引用(Microsoft excel 12.0)
and this reference which I used (Microsoft excel 12.0)
private void button1_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application xlApp ;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook ;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet ;
object misValue = System.Reflection.Missing.Value;
xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
int i = 0;
int j = 0;
for (i = 0; i <= DGData.RowCount - 1; i++)
{
for (j = 0; j <= DGData.ColumnCount - 1; j++)
{
DataGridViewCell cell = DGData[j, i];
xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
}
}
xlWorkBook.SaveAs("csharp.net-informations.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
MessageBox.Show("Excel file created , you can find the file c:\\csharp.net-informations.xls");
}
推荐答案
错误可能是由于用户本地设置与Office版本不同,以下解决方案可能对您有用,请尝试一下并让我们知道会发生什么:
Error might be because the user local settings are different from the office version, the solution below might work for you, try it and let us know what happens:
在创建Excel对象之前
Before creating Excel object
System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
关闭Excel后:
System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
这篇关于将datagridview中的数据导出到excel表格窗口形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文