close
1.前言
利用Interop操作Excel,並將資料寫入檔案中。
2.說明
加入參考
//版本12.0 Microsoft Office Excel 2007 Microsoft.Office.Interop.Excel
加入命名空間
using Excel=Microsoft.Office.Interop.Excel;
程式碼:
//使用Application類別,開啟Excel應用程式 Excel.ApplicationClass app = new Microsoft.Office.Interop.Excel.ApplicationClass(); //Excel文件開啟可見 app.Visible = true; //開啟新的活頁簿 Excel.Workbook wb = app.Workbooks.Add(Excel.XlSheetType.xlWorksheet); //建立新的工作表,並設定為焦點工作區 Excel.Worksheet ws = (Excel.Worksheet)app.ActiveSheet; //設定工作範圍並寫入單元格,本範例使用R語言中的pressure dataset Excel.Range rg = (Excel.Range)ws.Cells[1, 1]; rg.Value2 = "temperature"; rg = (Excel.Range)ws.Cells[1, 2]; rg.Value2 = "pressure"; rg = (Excel.Range)ws.Cells[2, 1]; rg.Value2 = 0; rg = (Excel.Range)ws.Cells[2, 2]; rg.Value2 = 0.0002; rg = (Excel.Range)ws.Cells[3, 1]; rg.Value2 = 20; rg = (Excel.Range)ws.Cells[3, 2]; rg.Value2 = 0.0012; rg = (Excel.Range)ws.Cells[4, 1]; rg.Value2 = 40; rg = (Excel.Range)ws.Cells[4, 2]; rg.Value2 = 0.0060; rg = (Excel.Range)ws.Cells[5, 1]; rg.Value2 = 60; rg = (Excel.Range)ws.Cells[5, 2]; rg.Value2 = 0.0300; rg = (Excel.Range)ws.Cells[6, 1]; rg.Value2 = 80; rg = (Excel.Range)ws.Cells[6, 2]; rg.Value2 = 0.0900; rg = (Excel.Range)ws.Cells[7, 1]; rg.Value2 = 100; rg = (Excel.Range)ws.Cells[7, 2]; rg.Value2 = 0.2700; rg = (Excel.Range)ws.Cells[8, 1]; rg.Value2 = 120; rg = (Excel.Range)ws.Cells[8, 2]; rg.Value2 = 0.7500; rg = (Excel.Range)ws.Cells[9, 1]; rg.Value2 = 140; rg = (Excel.Range)ws.Cells[9, 2]; rg.Value2 = 1.8500; rg = (Excel.Range)ws.Cells[10, 1]; rg.Value2 = 160; rg = (Excel.Range)ws.Cells[10, 2]; rg.Value2 = 4.2000; rg = (Excel.Range)ws.Cells[11, 1]; rg.Value2 = 180; rg = (Excel.Range)ws.Cells[11, 2]; rg.Value2 = 8.8000; rg = (Excel.Range)ws.Cells[12, 1]; rg.Value2 = 200; rg = (Excel.Range)ws.Cells[12, 2]; rg.Value2 = 17.3000; rg = (Excel.Range)ws.Cells[13, 1]; rg.Value2 = 220; rg = (Excel.Range)ws.Cells[13, 2]; rg.Value2 = 32.1000; rg = (Excel.Range)ws.Cells[14, 1]; rg.Value2 = 240; rg = (Excel.Range)ws.Cells[14, 2]; rg.Value2 = 57.0000; rg = (Excel.Range)ws.Cells[15, 1]; rg.Value2 = 260; rg = (Excel.Range)ws.Cells[15, 2]; rg.Value2 = 96.0000; rg = (Excel.Range)ws.Cells[16, 1]; rg.Value2 = 280; rg = (Excel.Range)ws.Cells[16, 2]; rg.Value2 = 157.0000; rg = (Excel.Range)ws.Cells[17, 1]; rg.Value2 = 300; rg = (Excel.Range)ws.Cells[17, 2]; rg.Value2 = 247.0000; rg = (Excel.Range)ws.Cells[18, 1]; rg.Value2 = 320; rg = (Excel.Range)ws.Cells[18, 2]; rg.Value2 = 376.0000; rg = (Excel.Range)ws.Cells[19, 1]; rg.Value2 = 340; rg = (Excel.Range)ws.Cells[19, 2]; rg.Value2 = 558.0000; rg = (Excel.Range)ws.Cells[20, 1]; rg.Value2 = 360; rg = (Excel.Range)ws.Cells[20, 2]; rg.Value2 = 806.0000; //停用警告訊息 app.DisplayAlerts = false; //另存活頁簿 wb.SaveAs(@"D:\TMP\pressure.xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); if (app != null) { //關閉活頁簿 wb.Close(Type.Missing, @"D:\TMP\pressure.xlsx", Type.Missing); //關閉Excel app.Quit(); //釋放資源 System.Runtime.InteropServices.Marshal.ReleaseComObject(rg); System.Runtime.InteropServices.Marshal.ReleaseComObject(ws); System.Runtime.InteropServices.Marshal.ReleaseComObject(wb); System.Runtime.InteropServices.Marshal.ReleaseComObject(app); rg = null; ws = null; wb = null; app = null; } GC.Collect();
文章標籤
全站熱搜
留言列表