C#讀寫Excel文件相關技巧
一直想小結一些C#讀寫Excel文件的相關技巧,畢竟Excel打印更為方便和實用,一個是Excel打印輸出編碼比Word文件打印數(shù)據(jù)簡單些,另一個是Excel本身對數(shù)據(jù)超強計算處理功能;趕巧最近項目又涉及Excel報表統(tǒng)計打印的問題,所以在把其中的一些技術記錄下來與大家一起分析討論,次篇主要涉及兩個方面內容:
1、C#讀寫Excel文件
A、設計Excel模版
B、打開一個目標文件并且讀取模版內容
C、目標文件按格式寫入需要的數(shù)據(jù)
D、保存并且輸出目標Excel文件
2、 Excel對象資源釋放,這個在以前項目沒有注意徹底釋放使用到Excel對象,對客戶計算機資源造成一定浪費,此次得到徹底解決。
下面是一個C#讀寫Excel文件并打印輸出的Demo
1、 創(chuàng)建一個叫DemoExcel的項目
2、 引用COM,包括:Microsoft.Excel.x.0.Object.Library,Microsoft.Office.x.0.Object.Library
建議安裝正版OFFICE,而且版本在11.0以上(Office2003以上),引用以上兩個Com后,在項目引用欄發(fā)現(xiàn)多了Excel、Microsoft.Office.Core,VBIDE三個 Library.
3、 下面建立一些模擬的數(shù)據(jù),此處為街鎮(zhèn)信息
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using Microsoft.Office.Interop.Excel;
- using Microsoft.Office.Core;
- using System.IO;
- using System.Reflection;
- namespace DemoExcel
- ...{
- public partial class Form1 : Form
- ...{
- private object missing = Missing.Value;
- private Microsoft.Office.Interop.Excel.Application ExcelRS;
- private Microsoft.Office.Interop.Excel.Workbook RSbook;
- private Microsoft.Office.Interop.Excel.Worksheet RSsheet;
- public Form1()
- ...{
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- ...{
- // TODO: 這行代碼將數(shù)據(jù)加載到表“dataSet1.STREET”中。您可以根據(jù)需要移動或移除它。
- this.sTREETTableAdapter.Fill(this.dataSet1.STREET);
- }
- private void button1_Click(object sender, EventArgs e)
- ...{
- string OutFilePath = System.Windows.Forms.Application.StartupPath + @" emp.xls";
- string TemplateFilePath = System.Windows.Forms.Application.StartupPath + @"模版.xls";
- PrintInit(TemplateFilePath,OutFilePath);
- }
- Excle輸出前初始化#region Excle輸出前初始化
- /**////
- ///
- ///
- ///
- public bool PrintInit(string templetFile, string outputFile)
- ...{
- try
- ...{
- if (templetFile == null)
- ...{
- MessageBox.Show("Excel模板文件路徑不能為空!");
- return false;
- }
- if (outputFile == null)
- ...{
- MessageBox.Show("輸出Excel文件路徑不能為空!");
- return false;
- }
- //把模版文件templetFile拷貝到目輸出文件outputFile中,并且目標文件可以改寫
- System.IO.File.Copy(templetFile, outputFile, true);
- if (this.ExcelRS != null)
- ExcelRS = null;
- //實例化ExcelRS對象
- ExcelRS = new Microsoft.Office.Interop.Excel.ApplicationClass();
- //打開目標文件outputFile
- RSbook = ExcelRS.Workbooks.Open(outputFile, missing, missing, missing, missing, missing,
- missing, missing, missing, missing, missing, missing, missing, missing, missing);
- //設置第一個工作溥
- RSsheet = (Microsoft.Office.Interop.Excel.Worksheet)RSbook.Sheets.get_Item(1);
- //激活當前工作溥
- RSsheet.Activate();
- 在當前工作溥寫入內容#region 在當前工作溥寫入內容
- for (int i = 0; i < this.dataGridView1.RowCount; i++)
- ...{
- RSsheet.Cells[3 + i, 1] = this.dataGridView1[0, i].Value.ToString();
- RSsheet.Cells[3 + i, 2] = this.dataGridView1[1, i].Value.ToString();
- RSsheet.Cells[3 + i, 3] = this.dataGridView1[2, i].Value.ToString();
- }
- #endregion
- //保存目標文件
- RSbook.Save();
- //設置DisplayAlerts
- ExcelRS.DisplayAlerts = false;
- ExcelRS.Visible = true;
- //ExcelRS.DisplayAlerts = true;
- //釋放對象
- RSsheet = null;
- RSbook = null;
- ExcelRS = null;
- //釋放內存
- GcCollect();
- }
- catch (Exception ex)
- ...{
- MessageBox.Show(ex.ToString());
- return false;
- }
- return true;
- }
- #endregion
- public void GcCollect()
- ...{
- GC.Collect();
- GC.WaitForPendingFinalizers();
- GC.Collect();
- GC.WaitForPendingFinalizers();
- }
- }
- }
特別說明:
a、引用Microsoft.Office.Interop.Excel;using Microsoft.Office.Core;
b、(關鍵)在程序中特別釋放Excel資源的時候既要設置對象為null,又要強制回收內存,這樣才能徹底回收資源。
c、引用的Office組建版本是個敏感問題,不同版本之間有細微差別,需要分別處理。
本文來自曾玄昴在CSDN博客中的文章《C#讀寫Excel文檔(---續(xù)C#讀寫Word文件)》
【編輯推薦】