C# 如何快速将数据从DataTable复制到Excel?

C# 如何快速将数据从DataTable复制到Excel?,c#,excel,datatable,C#,Excel,Datatable,我从谷歌获得了以下代码,可以快速将数据表复制到excel。逐细胞技术非常缓慢 代码:- Excel.Application excelApp = new Excel.Application(); excelApp.DisplayAlerts = false; Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet; xlWorkBook = excelApp.Workbooks.Add(misValue); xlWorkSheet = (E

我从谷歌获得了以下代码,可以快速将数据表复制到excel。逐细胞技术非常缓慢

代码:-

Excel.Application excelApp = new Excel.Application();
excelApp.DisplayAlerts = false;

Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
xlWorkBook = excelApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.ActiveSheet;


xlWorkSheet.Cells[1, 1] = "Name";
xlWorkSheet.Cells[1, 2] = "Index";
xlWorkSheet.Cells[1, 3] = "Cat";
xlWorkSheet.Cells[1, 4] = "CatNumber";
xlWorkSheet.Cells[1, 5] = "ID";
xlWorkSheet.Cells[1, 6] = "Type";
xlWorkSheet.Cells[1, 7] = "Description";
xlWorkSheet.Cells[1, 8] = "Sou";
xlWorkSheet.Cells[1, 9] = "IID";
xlWorkSheet.Cells[1, 10] = "Time";
xlWorkSheet.Cells[1, 11] = "TimeW";
xlWorkSheet.Cells[1, 12] = "User";


if (dt1.TableName == Convert.ToString(objType1.Name))
{
    object[,] arr = new object[dt1.Rows.Count, dt1.Columns.Count];

    for (int r = 0; r < dt1.Rows.Count; r++)
    {
        DataRow dr = dt1.Rows[r];
        for (int c = 0; c < dt1.Columns.Count; c++)
        {
            arr[r, c] = dr[c];
        }
    }

    //Set Excel Range to Paste the Data
    Microsoft.Office.Interop.Excel.Range c1 = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[2, 1];
    Microsoft.Office.Interop.Excel.Range c2 = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1 + dt1.Rows.Count, dt1.Columns.Count];
    Microsoft.Office.Interop.Excel.Range range = xlWorkSheet.get_Range(c1, c2);

    //Fill Array in Excel
    range.Value2 = arr;
注意:dt1是我拥有的具有多个不同数据类型列的数据表。我正在将文件保存到扩展名abc.xls


现在,我想我面临的问题是,每当描述很大时,它会抛出一个异常HRESULT OX800A03EC,同时将该特定描述复制到range.value2。

是否必须将其写入excel文件?您可以改为输出到csv吗?是的excel文件。我可以复制到csv文件,但我认为这可能会产生更多问题,因为描述包含许多特殊字符,如“:;”;,等等。如果它不会产生问题,我非常乐意将它导出到那里。我只需要将这些数据导出为excel格式。另外,如果我逐个单元格执行相同的任务,那么长的描述将被复制,没有任何问题,但它会导致性能的巨大延迟,这是不应该的!尝试将扩展名feom xls更改为csv,但仍然是相同的期望值:-您所说的“描述”是什么意思?那是多少个字符?你能给我们做一个可复制的样品吗?