使用C#windows窗体应用程序在microsoft excel中导出数据

时间:2010-03-09 11:58:45

标签: c# excel ms-office

当我尝试将数据导出到excel时,我得到了异常....例外是

  

COMException:来自HRESULT的异常:0x800A03EC。

我该如何解决这个错误?我该如何获得解决方案?告诉我这个问题的解决方案......

提前致谢...

我的代码是:

         {
        oxl = new Excel.Application();
        oxl.Visible = true;
        oxl.DisplayAlerts = false;


        wbook = oxl.Workbooks.Add(Missing.Value);

        wsheet = (Excel.Worksheet)wbook.ActiveSheet;
        wsheet.Name = "Customers"; 



        DataTable dt = InstituteTypeDetail();

        int rowCount = 1;
        foreach (DataRow dr in dt.Rows)
        {
            rowCount += 1;
            for (int i = 1; i < dt.Columns.Count + 1; i++)
            {
                // Add the header the first time through
                if (rowCount == 2)
                {
                    wsheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
                }
                wsheet.Cells[rowCount, i] = dr[i - 1].ToString();
            }
        }

        range = wsheet.get_Range(wsheet.Cells[1, 1],
                      wsheet.Cells[rowCount, dt.Columns.Count]);//In this place i got the exception
        range.EntireColumn.AutoFit();


        wsheet = null;
        range = null;

}

我做错了什么?解决这个异常的方法是什么....任何人都告诉我......

4 个答案:

答案 0 :(得分:1)

在您收到错误的时候,rowCount实际上是否正确?您从1开始,但随后立即递增 - 所以当您在foreach循环中检查第1行时,rowCount实际上是2。

我认为您的rowCount应该初始化为0(零。)然后您的标题行检查应该查找if (rowCount == 1)

答案 1 :(得分:1)

以下示例代码适用于我。你能尝试一下吗?修改了你的样本。此外,最后在最后发布对所有com对象的引用也是一种好习惯。

Application oxl = null;
        try
        {
            oxl = new Application( );
            oxl.Visible = true;
            oxl.DisplayAlerts = false;

            string fileName = @"D:\one.xls";
            object missing = Missing.Value;
            Workbook wbook = oxl.Workbooks.Open( fileName, missing, missing, missing, missing, missing,missing,missing,missing,missing,missing,missing,missing,missing,missing );

            Worksheet wsheet = ( Worksheet )wbook.ActiveSheet;
            wsheet.Name = "Customers";


            System.Data.DataTable dt = new System.Data.DataTable( "test" );
            dt.Columns.Add( "col1" );
            dt.Columns.Add( "col2" );
            dt.Columns.Add( "col3" );

            dt.Rows.Add( new object[ ] { "one", "one", "one" } );
            dt.Rows.Add( new object[ ] { "two", "two", "two" } );
            dt.Rows.Add( new object[ ] { "three", "three", "three" } );


            for ( int i = 1 ; i <=  dt.Columns.Count ; i++ )
            {
                wsheet.Cells[ 1, i ] = dt.Columns[ i - 1 ].ColumnName;
            }

            for ( int j = 1 ; j <= dt.Rows.Count ; j++ )
            {
                for ( int k = 0 ; k <  dt.Columns.Count ; k++ )
                {
                    DataRow dr = dt.Rows[ k ];
                    wsheet.Cells[ j +1, k+1 ] = dr[ k ].ToString( );
                }
            }

            Range range = wsheet.get_Range( wsheet.Cells[ 1, 1 ],
                          wsheet.Cells[ dt.Rows.Count + 1, dt.Columns.Count ] );//In this place i got the exception
            range.EntireColumn.AutoFit( );

            wbook.Save( );

            wsheet = null;
            range = null;


        }
        finally
        {
            oxl.Quit( );
            System.Runtime.InteropServices.Marshal.ReleaseComObject( oxl );
            oxl = null;
        }

答案 2 :(得分:0)

get_Range期望单元格的名称为字符串,即“A1”,“B25”等。您可以尝试使用以下代码替换该行:

range = wsheet.Cells(1, 1);
range = range.Resize(rowCount, dt.Columns.Count);

答案 3 :(得分:0)

SpreadsheetGear for .NET将允许您使用.NET中的Excel工作簿,而不会出现与Excel COM Interop相关的问题。 SpreadsheetGear也比COM Interop更快 - 特别是当你的代码似乎循环遍历许多单元格时。

以下是一些使用SpreadsheetGear API执行与代码相同的代码:

using System;
using SpreadsheetGear;

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = @"D:\one.xls";
            IWorkbook wbook = SpreadsheetGear.Factory.GetWorkbook(fileName);
            IWorksheet wsheet = wbook.ActiveWorksheet;
            wsheet.Name = "Customers";
            System.Data.DataTable dt = new System.Data.DataTable("test");
            dt.Columns.Add("col1");
            dt.Columns.Add("col2");
            dt.Columns.Add("col3");
            dt.Rows.Add(new object[] { "one", "one", "one" });
            dt.Rows.Add(new object[] { "two", "two", "two" });
            dt.Rows.Add(new object[] { "three", "three", "three" });
            wsheet.Cells[0, 0, dt.Rows.Count - 1, dt.Columns.Count - 1].CopyFromDataTable(dt, SpreadsheetGear.Data.SetDataFlags.None);
            wsheet.UsedRange.EntireColumn.AutoFit();
            wbook.Save();
        }
    }
}

如果您想亲自试用,可以查看实时样本here并下载免费试用here

免责声明:我拥有SpreadsheetGear LLC