gpt4 book ai didi

c# - 将 datagridview 中的数据导出到 excel 工作表窗口窗体

转载 作者:太空宇宙 更新时间:2023-11-03 14:21:37 24 4
gpt4 key购买 nike

我有 DataGridView,当我显示导出到 excel 工作表按钮时出现以下错误:

(Old format or invalid type library. (Exception from HRESULT: 0x80028018  (TYPE_E_INVDATAREAD)))

here
(
xlWorkBook = xlApp.Workbooks.Add(misValue);
)

以及我使用的这个引用(微软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 版本不同,下面的解决方案可能适合您,试试吧,让我们知道发生了什么:

创建Excel对象之前

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;

关于c# - 将 datagridview 中的数据导出到 excel 工作表窗口窗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5202861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com