0

我想使用 Microsoft.Office.Interop.Excel dll 将数据写入 Excel 工作表。我有一个代码:

if (System.IO.File.Exists(strFileName))
{
    System.IO.File.SetAttributes(strFileName, FileAttributes.Normal);
    System.IO.File.Delete(strFileName);
}

// Open an instance of excel. Create a new workbook.
// A workbook by default has three sheets, so if you just want 
//a single one, delete sheet 2 and 3

Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Excel._Workbook xlWB = (Excel._Workbook)xlApp.Workbooks.Add(Missing.Value);

Excel._Worksheet xlSheet = (Excel._Worksheet)xlWB.Sheets[1];
((Excel._Worksheet)xlWB.Sheets[2]).Delete();
((Excel._Worksheet)xlWB.Sheets[2]).Delete();

xlSheet.Name = strSheetName;

// Write a value into A1
xlSheet.Cells[2, 1] = "Tags";
xlSheet.Cells[2, 2] = "Leak Test";
xlSheet.Cells[2, 3] = "FIR";
xlSheet.Cells[2, 4] = "SOP";

xlWB.SaveAs(strFileName, Missing.Value, Missing.Value, Missing.Value, 
Missing.Value,Missing.Value, 
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, 
Missing.Value, Missing.Value, Missing.Value, Missing.Value);
xlApp.Quit();

// Release the COM object, set the Excel variables to Null, and tell the 
//Garbage Collector to do its thing
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWB);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);

xlSheet = null;
xlWB = null;
xlApp = null;

现在我想从路径中打开现有的 excel 文件,然后将一些数据放入我们提供的命名的 excel 表中,然后它将以编程方式保存到特定的路径中。

请回复我的来源,如果有任何可以打开现有的 excel 文件并且可以附加并用其他名称保存。

问候, 吉里什

4

3 回答 3

3

ExcelPackage 不再维护。它似乎有一些错误。在那里阅读评论,我发现http://epplus.codeplex.com/

它建立在 ExcelPackage 之上并继承了它的许可证 (GPL),因此它可能不适合您的要求。

于 2011-05-25T12:27:55.790 回答
1

如果您使用的是 2007 版本的 Excel,我建议您改用ExcelPackage - 它是 OpenXML 标准的一种实现,它比 COM 互操作要快得多且不那么混乱,而且您可以在不支持它的机器上运行它甚至安装了 Excel (Office),就像在您的 Web 服务器上一样。

强烈推荐 - 但仅限于 Excel 2007 及更高版本。

于 2009-10-30T05:34:09.010 回答
0

有关信息,请参阅此处。

于 2009-10-30T05:31:15.030 回答