使用C#windowsforms应用程序导出Microsoft Excel中的数据

当我试图导出数据到excel时,我得到了exception….例外是

COMException:从HRESULTexception:0x800A03EC。

我该如何解决这个错误? 我该如何得到解决scheme? 告诉我这个问题的解决scheme…

提前致谢…

我的代码是:

{ 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; 

}

我做错了什么? 有什么办法解决这个例外….任何人PLZ告诉我…

以下示例代码适合我。 你可以试一下吗? 修改了您的示例。 在最后释放所有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; } 

你的rowCount在你得到错误的地方是否正确? 你从1开始,然后立即递增 – 所以当你在foreach循环中检查第1行时, rowCount实际上是2。

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

get_Range需要单元格的名称作为string,即像“A1”,“B25”等。您可以尝试用下面的代码replace行:

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

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(); } } } 

你可以在这里看到现场的样品,如果你想自己尝试下载免费试用。

免责声明:我自己的SpreadsheetGear LLC