从DataSet填充工作表时出错 [英] Error while Populating a Worksheet from a DataSet

查看:90
本文介绍了从DataSet填充工作表时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误:



由于以下错误,检索CLSID为{00020819-0000-0000-C000-000000000046}的组件的COM类工厂失败:80040154 Class未注册(HRESULT异常:0x80040154(REGDB_E_CLASSNOTREG))。



代码:

Error :

Retrieving the COM class factory for component with CLSID {00020819-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

Code :

Microsoft.Office.Interop.Excel.Workbook workbook = new Microsoft.Office.Interop.Excel.Workbook();
foreach (DataTable dt1 in ds.Tables)
{
    // Create the worksheet to represent this data table
    Microsoft.Office.Interop.Excel.Worksheet worksheet = workbook.Worksheets.Add(dt1.TableName);
    // Create column headers for each column
    for (int columnIndex = 0; columnIndex < dt1.Columns.Count; columnIndex++)
    {
        worksheet.Rows[0].Cells[columnIndex].value = dt1.Columns[columnIndex].ColumnName;
    }
    // Starting at row index 1, copy all data rows in
    // the data table to the worksheet
    int rowIndex = 1;
    foreach (DataRow dataRow in dt1.Rows)
    {
        Microsoft.Office.Interop.Excel.ListColumns row = worksheet.Rows[rowIndex++];
        for (int columnIndex = 0; columnIndex < dataRow.ItemArray.Length; columnIndex++)
        {
            row.Add(dataRow.ItemArray[columnIndex]);
        }
    }
}
workbook.SaveAs("C:\\Data.xls");









已添加 pre 标签。

[/编辑]

推荐答案

部署代码后在任何服务器。由于安全问题,Office没有安装在那里。我的建议是使用这些库

Epplus



阅读这些文章

对Office的支持已经结束
Once you deploy your code in any server. Due to security concern Office is not installed there. My suggestion is to use these library
Epplus

Read these Article
Support for Office has ended


正在使用 Microsoft.Office.Interop.Excel ,您需要在服务器上安装Microsoft Office。



请安装并试用。这就是它抛出异常的原因。
As you are using Microsoft.Office.Interop.Excel, you need to install Microsoft Office on the Server.

Please install and try. That is why it is throwing the Exception.


这篇关于从DataSet填充工作表时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆