C# 操作Excel文件
用C#实现一下功能:
根据给定的完整文件名FilePath的Excel文件:
首先判断文件是否存在,如果存在打开它向文件的表末尾添加新数据,如果不存在则创建他并打开向它的表中田间数据。
[解决办法]
Console.WriteLine("Filling data into the worksheet ...");
// Set the column header
oCells = oSheet.Cells;
oCells[1, 1] = "First Name";
oCells[1, 2] = "Last Name";
oCells[1, 3] = "Full Name";
// Construct an array of user names
string[,] saNames = new string[,] {
{"John", "Smith"},
{"Tom", "Brown"},
{"Sue", "Thomas"},
{"Jane", "Jones"},
{"Adam", "Johnson"}};
// Fill A2:B6 with an array of values (First and Last Names).
oRng1 = oSheet.get_Range("A2", "B6");
oRng1.Value2 = saNames;
// Fill C2:C6 with a relative formula (=A2 & " " & B2).
oRng2 = oSheet.get_Range("C2", "C6");
oRng2.Formula = "=A2 & \" \" & B2";
/////////////////////////////////////////////////////////////////////
// Save the workbook as a xlsx file and close it.
//
Console.WriteLine("Save and close the workbook");
string fileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().
Location) + "\\MSDN.xls";
oWB.SaveAs(fileName,missing,
missing, missing, missing, missing,
Excel.XlSaveAsAccessMode.xlNoChange,
missing, missing, missing, missing, missing);
oWB.Close(missing, missing, missing);
/////////////////////////////////////////////////////////////////////
// Quit the Excel application.
//
Console.WriteLine("Quit the Excel application");
// Excel will stick around after Quit if it is not under user control
// and there are outstanding references. When Excel is started or
// attached programmatically and Excel.Application.Visible = false,
// Excel.Application.UserControl is false. The UserControl property
// can be explicitly set to True which should force the application
// to terminate when Quit is called, regardless of outstanding
// references.
oXL.UserControl = true;
oXL.Quit();
/////////////////////////////////////////////////////////////////////
// Clean up the unmanaged COM resources.
//
// Explicitly call Marshal.FinalReleaseComObject on all accessor
// objects. See http://support.microsoft.com/kb/317109.
Marshal.FinalReleaseComObject(oRng2);
oRng2 = null;
Marshal.FinalReleaseComObject(oRng1);
oRng1 = null;
Marshal.FinalReleaseComObject(oCells);
oCells = null;
Marshal.FinalReleaseComObject(oSheet);
oSheet = null;
Marshal.FinalReleaseComObject(oWB);
oWB = null;
Marshal.FinalReleaseComObject(oWBs);
oWBs = null;
Marshal.FinalReleaseComObject(oXL);
oXL = null;
// [-and/or-]
// Force a garbage collection as soon as the calling function is off
// the stack (at which point these objects are no longer rooted) and
// then call GC.WaitForPendingFinalizers.
GC.Collect();
GC.WaitForPendingFinalizers();
// GC needs to be called twice in order to get the Finalizers called
// - the first time in, it simply makes a list of what is to be
// finalized, the second time in, it actually the finalizing. Only
// then will the object do its automatic ReleaseComObject.
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
然后附加说明:
一、添加引用
添加com组件(Microsoft Office 11.0 Object Library )命名空间为Microsoft.Office.Interop.Excel
即Microsoft.Office.Interop.Excel.dll 你必须安装office
添加Excel.exe引用默认路径为C:\Program Files\Microsoft Office\OFFICE11\Excel.exe
[解决办法]
帮顶···············
[解决办法]
。。。。有点复杂