在C#中建立、讀、寫EXCEL檔案(基於COM)
阿新 • • 發佈:2019-01-22
1. EXCEL Library
在使用C#中的excel模組之前,我們需要先把excel library加入到project中。
首先建立一個空專案,然後建立一個按鈕。隨後,如下圖點選“專案”->“新增引用”:
隨後選擇microsoft excel 1X.0 object library。
2. 在C#中程式設計建立excel檔案
首先初始化excel object
Excel.Application excelApp = new Excel.ApplicationClass();
在建立excel workbook之前,檢查系統是否安裝excel
判斷檔案是否存在,如果存在就開啟workbook,如果不存在就新建一個if(excelApp == null){ // if equal null means EXCEL is not installed. MessageBox.Show("Excel is not properly installed!"); return; }
在建立完workbook之後,下一步就是新建worksheet並寫入資料// open a workbook,if not exist, create a new one Excel.Workbook workBook; if(File.Exists(filename)) { workBook = excelApp.Workbooks.Open(filename, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); } else { workBook = excelApp.Workbooks.Add(true); }
有兩個選項可以設定,如下,visable屬性設定為true的話,excel程式會啟動;false的話,excel只在後臺執行。//new a worksheet Excel.Worksheet workSheet = workBook.ActiveSheet as Excel.Worksheet; //write data workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(1);//獲得第i個sheet,準備寫入 workSheet.Cells[1, 3] = "(1,3)Content";
displayalert設定為true將會顯示excel中的提示資訊。
//set visible the Excel will run in background
excelApp.Visible = false;
//set false the alerts will not display
excelApp.DisplayAlerts = false;
儲存檔案,關閉workbook
workBook.SaveAs(filename);
workBook.Close(false, Missing.Value, Missing.Value);
退出並清理objects,回收記憶體
//quit and clean up objects
excelApp.Quit();
workSheet = null;
workBook = null;
excelApp = null;
GC.Collect();
3.完整程式碼
最後附上完整程式碼(Form_Start.cs):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
namespace InfoExtraction
{
public partial class Form_Start : Form
{
public Form_Start()
{
InitializeComponent();
}
private void Form_Start_Load(object sender, EventArgs e)
{
}
private void button_run_Click(object sender, EventArgs e)
{
string currentPath = Directory.GetCurrentDirectory();
WriteExcel(currentPath + "\\results.xlsx");
MessageBox.Show("Success!");
}
private void button_exit_Click(object sender, EventArgs e)
{
Close();
Application.Exit();
}
public void WriteExcel(string filename) {
//new an excel object
Excel.Application excelApp = new Excel.ApplicationClass();
if(excelApp == null){
// if equal null means EXCEL is not installed.
MessageBox.Show("Excel is not properly installed!");
return;
}
// open a workbook,if not exist, create a new one
Excel.Workbook workBook;
if(File.Exists(filename))
{
workBook = excelApp.Workbooks.Open(filename, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
}
else
{
workBook = excelApp.Workbooks.Add(true);
}
//new a worksheet
Excel.Worksheet workSheet = workBook.ActiveSheet as Excel.Worksheet;
//write data
workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(1);//獲得第i個sheet,準備寫入
workSheet.Cells[1, 3] = "(1,3)Content";
//set visible the Excel will run in background
excelApp.Visible = false;
//set false the alerts will not display
excelApp.DisplayAlerts = false;
//workBook.SaveAs(filename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
workBook.SaveAs(filename);
workBook.Close(false, Missing.Value, Missing.Value);
//quit and clean up objects
excelApp.Quit();
workSheet = null;
workBook = null;
excelApp = null;
GC.Collect();
}
}
}