我刚刚开始通过C#调整Excel,以便能够自动创建,并添加到Excel文件中.
我可以打开文件并更新其数据并浏览现有的工作表.我的问题是如何添加新表?
我试过了:
Excel.Worksheet newWorksheet; newWorksheet = (Excel.Worksheet)excelApp.ThisWorkbook.Worksheets.Add( Type.Missing, Type.Missing, Type.Missing, Type.Missing);
但我得到COM异常,我的谷歌搜索没有给我任何答案.
来自HRESULT的异常:0x800A03EC来源是:"Interop.Excel"
我希望有人能够让我摆脱困境.
您需要在项目中将"Microsoft Excel 11.0 Object Library
COM"引用添加到" " - 或者任何适合的版本.
这段代码适合我:
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问题中是一个有用的经验法则:"切勿对COM对象使用2个点".在你的代码中; 你会遇到真正的麻烦.我上面的演示代码没有正确清理Excel应用程序,但它是一个开始!
在查看这个问题时,我发现其他一些链接:
使用C#打开和导航Excel
如何:使用COM Interop创建Excel电子表格(C#编程指南)
如何:向工作簿添加新工作表
根据MSDN
要使用COM互操作,您必须具有管理员或高级用户安全权限.
希望有所帮助.
在此感谢您的一些优秀回复.@AR.,你的明星,它完美无缺.昨晚我注意到Excel.exe
没有关闭; 所以我做了一些研究,发现了如何释放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(); } } } }
谢谢你的帮助.