C#操作Excel,Word
首先添加对excel组件的引用 ->com->Microsoft Excel 11或12
如果本机Office 没有完全安装 还需要下载 office 2003 pia组件
1.C#写Excel文件
using Excel = Microsoft.Office.Interop.Excel; public static void OperatExcel(DataTable dt,string FileName,string SavePath) { Excel.Application excel = new Excel.Application(); object objMissing = System.Reflection.Missing.Value; //打开excel文件 // excel = new Excel.ApplicationClass(); excel.Visible = false; //打开工作簿 Excel.Workbook mybook = excel.Workbooks.Open(FileName, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing ); Excel.Worksheet mysheet = (Excel.Worksheet)mybook.Worksheets.get_Item(1); //设置第10行为红色 //mysheet.get_Range((Excel.Range)mysheet.Cells[10, 2]).Select(); // mysheet.get_Range((Excel.Range)mysheet.Cells[10, 2],).Text = "sssss"; ; for (int i = 0; i < 9; i++) { for (int j = 0; j < 5; j++) { mysheet.Cells[i + 3, j + 2] = dt.Rows[i+1][j+1].ToString(); } } excel.ActiveWorkbook.SaveAs(SavePath, Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null); excel.Quit(); excel = null; GC.Collect();//垃圾回收 }
这里“GC.Collect();//垃圾回收 ”不一定能把excel进程回收掉
C# 操作excel后关闭
using System.Runtime.InteropServices; [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID); protected void Button1_Click(object sender, EventArgs e) { Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass(); excel.Workbooks.Open("d:\aaa.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); IntPtr t = new IntPtr(excel.Hwnd); int k = 0; GetWindowThreadProcessId(t, out k); System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k); p.Kill(); }
2.C#读Excel文件
string strConnection = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source ={0}; Extended Properties = Excel 8.0", strFileName); OleDbConnection oleConnection = new OleDbConnection(strConnection); //try //{ string strCommondText = "SELECT * FROM [Device$] "; oleConnection.Open(); DataSet dsRead = new DataSet(); OleDbDataAdapter oleAdper = new OleDbDataAdapter(strCommondText, oleConnection); oleAdper.Fill(dsRead, "Pantent"); Maticsoft.Model.Device model = new Maticsoft.Model.Device(); Maticsoft.DAL.Device dal = new Maticsoft.DAL.Device(); Maticsoft.DAL.Users dalUser = new Maticsoft.DAL.Users(); DataTable dt = dsRead.Tables[0]; for (int i = 1; i < dt.Rows.Count; i++) { model.deviceId= dt.Rows[i][0].ToString(); dal.Add(model); } return dsRead; //} //catch (System.Exception) //{ // throw new ApplicationException("读取数据源文件时出错"); //} //finally //{ // oleConnection.Close(); //}
3. C# word操作.需要对word模板设置指定的书签位置
Word.Application _wordApp = new Word.Application(); object filename = OpenPath + fileName; object isReadonly = false; object isVisible = true; object missing = System.Reflection.Missing.Value; Word.Document odoc = _wordApp.Documents.Open(ref filename, ref missing, ref isReadonly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing); odoc.Activate(); //在书签处输出文字 object oBookmark0 = "CourseName"; odoc.Bookmarks.get_Item(ref oBookmark0).Range.Text = courseName; object oBookMark1 = "LabName"; odoc.Bookmarks.get_Item(ref oBookMark1).Range.Text = labName; object oBookMark2 = "StuName"; odoc.Bookmarks.get_Item(ref oBookMark2).Range.Text = stuName; object oBookMark3 = "StuId"; odoc.Bookmarks.get_Item(ref oBookMark3).Range.Text = stuId; object oBookMark4 = "StuClassName"; odoc.Bookmarks.get_Item(ref oBookMark4).Range.Text = stuClassName; //另存为 string newFileName = string.Format("{0}{1}{2}.doc", stuId, stuName, labName); filename = SavePath + newFileName; odoc.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); odoc.Close(ref missing, ref missing, ref missing); _wordApp.Quit(ref missing, ref missing, ref missing); _wordApp = null; GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers();