我想使用的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文件及可以追加和保存的另一个名称。

此致 吉里什

有帮助吗?

解决方案

参见此处信息

其他提示

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

它建立在ExcelPackage并继承其许可证(GPL),所以它可能不适合您的要求。

如果您使用的是2007年版本的Excel,我会建议使用 ExcelPackage 代替 - 这是处理OpenXML标准的实现,它的速度更快和很多比COM互操作少凌乱,你可以说甚至没有的Excel(办公室)安装,像你的Web服务器上的机器上运行它。

高度推荐 - 但不限于Excel 2007和向上

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top