C# – 如何以编程方式添加Excel工作表 – Office XP / 2003

我刚刚开始通过C#拨动Excel,以便能够自动创build,并添加到Excel文件。

我可以打开文件并更新其数据并移动现有的工作表。 我的问题是如何添加新的床单?

我试过了:

Excel.Worksheet newWorksheet; newWorksheet = (Excel.Worksheet)excelApp.ThisWorkbook.Worksheets.Add( Type.Missing, Type.Missing, Type.Missing, Type.Missing); 

但是我得到COMexception ,我的谷歌search没有给我任何答案。

来自HRESULT的exception:0x800A03EC来源是:“Interop.Excel”

我希望有人能把我从痛苦中解救出来。

您需要将项目中的COM引用添加到Microsoft Excel 11.0 Object Library – 或适当的任何版本。

这段代码适用于我:

 private void AddWorksheetToExcelWorkbook(string fullFilename,string worksheetName) { Microsoft.Office.Interop.Excel.Application xlApp = null; Workbook xlWorkbook = null; Sheets xlSheets = null; Worksheet xlNewSheet = null; try { xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null) return; // Uncomment the line below if you want to see what's happening in Excel // xlApp.Visible = true; xlWorkbook = xlApp.Workbooks.Open(fullFilename, 0, false, 5, "", "", false, XlPlatform.xlWindows, "", true, false, 0, true, false, false); xlSheets = xlWorkbook.Sheets as Sheets; // The first argument below inserts the new worksheet as the first one xlNewSheet = (Worksheet)xlSheets.Add(xlSheets[1], Type.Missing, Type.Missing, Type.Missing); xlNewSheet.Name = worksheetName; xlWorkbook.Save(); xlWorkbook.Close(Type.Missing,Type.Missing,Type.Missing); xlApp.Quit(); } finally { Marshal.ReleaseComObject(xlNewSheet); Marshal.ReleaseComObject(xlSheets); Marshal.ReleaseComObject(xlWorkbook); Marshal.ReleaseComObject(xlApp); xlApp = null; } } 

请注意,要对正确清理和释放COM对象引用时要非常小心。 包含在该StackOverflow问题是一个有用的经验法则: “不要使用2点与COM对象” 。 在你的代码中; 你会遇到麻烦。 我上面的演示代码没有正确清理Excel应用程序,但这是一个开始!

在查看这个问题时,我发现一些其他的链接:

  • 用C#打开和导航Excel
  • 如何:使用COM Interop创buildExcel电子表格(C#编程指南)
  • 如何:将新的工作表添加到工作簿

根据MSDN

要使用COM互操作,您必须拥有pipe理员或高级用户安全权限。

希望有所帮助。

非常感谢你的回复。 @AR。,你的明星,它完美的作品。 我昨天晚上注意到Excel.exe没有closures; 所以我做了一些研究,并发现如何释放COM对象。 这是我最后的代码:

 using System; using System.Collections.Generic; using System.Text; using System.Reflection; using System.IO; using Excel; namespace testExcelconsoleApp { class Program { private String fileLoc = @"C:\temp\test.xls"; static void Main(string[] args) { Program p = new Program(); p.createExcel(); } private void createExcel() { Excel.Application excelApp = null; Excel.Workbook workbook = null; Excel.Sheets sheets = null; Excel.Worksheet newSheet = null; try { FileInfo file = new FileInfo(fileLoc); if (file.Exists) { excelApp = new Excel.Application(); workbook = excelApp.Workbooks.Open(fileLoc, 0, false, 5, "", "", false, XlPlatform.xlWindows, "", true, false, 0, true, false, false); sheets = workbook.Sheets; //check columns exist foreach (Excel.Worksheet sheet in sheets) { Console.WriteLine(sheet.Name); sheet.Select(Type.Missing); System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet); } newSheet = (Worksheet)sheets.Add(sheets[1], Type.Missing, Type.Missing, Type.Missing); newSheet.Name = "My New Sheet"; newSheet.Cells[1, 1] = "BOO!"; workbook.Save(); workbook.Close(null, null, null); excelApp.Quit(); } } finally { System.Runtime.InteropServices.Marshal.ReleaseComObject(newSheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(sheets); System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); newSheet = null; sheets = null; workbook = null; excelApp = null; GC.Collect(); } } } } 

谢谢你的帮助。

另一个“上涨”的AR …,但如果你不必使用interop我会完全避免它。 这个产品其实很有趣: http : //www.clearoffice.com/它提供了一个非常直观的,完全托pipe的,用于处理excel文件的API,似乎是免费的。 (至less暂时) SpreadSheetGear也是优秀但价格昂贵。

我的两分钱

不要忘记包含参考 Microsoft Excel 12.0/11.0 object Library

 using Excel = Microsoft.Office.Interop.Excel; // Include this Namespace 

 Microsoft.Office.Interop.Excel.Application xlApp = null; Excel.Workbook xlWorkbook = null; Excel.Sheets xlSheets = null; Excel.Worksheet xlNewSheet = null; string worksheetName ="Sheet_Name"; object readOnly1 = false; object isVisible = true; object missing = System.Reflection.Missing.Value; try { xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null) return; // Uncomment the line below if you want to see what's happening in Excel // xlApp.Visible = true; xlWorkbook = xlApp.Workbooks.Open(@"C:\Book1.xls", missing, readOnly1, missing, missing, missing, missing, missing, missing, missing, missing, isVisible, missing, missing, missing); xlSheets = (Excel.Sheets)xlWorkbook.Sheets; // The first argument below inserts the new worksheet as the first one xlNewSheet = (Excel.Worksheet)xlSheets.Add(xlSheets[1], Type.Missing, Type.Missing, Type.Missing); xlNewSheet.Name = worksheetName; xlWorkbook.Save(); xlWorkbook.Close(Type.Missing, Type.Missing, Type.Missing); xlApp.Quit(); } finally { System.Runtime.InteropServices.Marshal.ReleaseComObject(xlNewSheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbook); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); //xlApp = null; } 

您可以使用OLEDB创build和操作Excel文件。 看到这个问题的链接和示例。

以下是我想到的一些事情:

  1. 您不能同时打开同一个对象的多个实例。 例如,如果您创build一个名为xlsheet1的新Excel工作表对象,则必须在创build另一个Excel工作表对象ex xlsheet2之前将其释放。 这似乎是COM丢失跟踪对象,并在服务器上留下一个僵尸进程。

  2. 如果您有多个用户访问同一文件,则使用与excel.workbooks关联的open方法也很难closures。 使用Add方法,它可以在不locking文件的情况下工作。 例如。 xlBook = xlBooks.Add("C:\location\XlTemplate.xls")

  3. 释放COM对象后,将垃圾回收放置在一个单独的块或方法中。

COM绝对不是一个好的select。 更具体地说,如果你正在处理networking环境,这是不行的…

我已经成功地使用了以下开源项目:

  • 用于OOXML格式的ExcelPackage(Office 2007)

  • NPOI for .XLS格式(Office 2003)

看看这些博客文章:

在C#中创buildExcel电子表格.XLS和.XLSX

NPOI与Excel表和dynamic图表

这是我用来添加addtional工作表

 Workbook workbook = null; Worksheet worksheet = null; workbook = app.Workbooks.Add(1); workbook.Sheets.Add(); Worksheet additionalWorksheet = workbook.ActiveSheet; 

我在VSTO中有一个类似问题的应用程序级外接程序,添加新表单时,除了HRESULT:0x800A03EC外。

错误代码0x800A03EC(或-2146827284)意味着NAME_NOT_FOUND; 换句话说,你已经要求一些东西了,Excel找不到它。

Dominic Zukiewicz @ Excel错误HRESULT:0x800A03EC尝试获取单元格名称的范围

然后我终于意识到ThisWorkbook引发了exception。 ActiveWorkbook正常。

 Excel.Worksheet newSheetException = Globals.ThisAddIn.Application.ThisWorkbook.Worksheets.Add(Type.Missing, sheet, Type.Missing, Type.Missing); Excel.Worksheet newSheetNoException = Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets.Add(Type.Missing, sheet, Type.Missing, Type.Missing);