C#讀寫Excel文件相關(guān)技巧
一直想小結(jié)一些C#讀寫Excel文件的相關(guān)技巧,畢竟Excel打印更為方便和實(shí)用,一個(gè)是Excel打印輸出編碼比Word文件打印數(shù)據(jù)簡(jiǎn)單些,另一個(gè)是Excel本身對(duì)數(shù)據(jù)超強(qiáng)計(jì)算處理功能;趕巧最近項(xiàng)目又涉及Excel報(bào)表統(tǒng)計(jì)打印的問(wèn)題,所以在把其中的一些技術(shù)記錄下來(lái)與大家一起分析討論,次篇主要涉及兩個(gè)方面內(nèi)容:
1、C#讀寫Excel文件
A、設(shè)計(jì)Excel模版
B、打開一個(gè)目標(biāo)文件并且讀取模版內(nèi)容
C、目標(biāo)文件按格式寫入需要的數(shù)據(jù)
D、保存并且輸出目標(biāo)Excel文件
2、 Excel對(duì)象資源釋放,這個(gè)在以前項(xiàng)目沒有注意徹底釋放使用到Excel對(duì)象,對(duì)客戶計(jì)算機(jī)資源造成一定浪費(fèi),此次得到徹底解決。
下面是一個(gè)C#讀寫Excel文件并打印輸出的Demo
1、 創(chuàng)建一個(gè)叫DemoExcel的項(xiàng)目
2、 引用COM,包括:Microsoft.Excel.x.0.Object.Library,Microsoft.Office.x.0.Object.Library
建議安裝正版OFFICE,而且版本在11.0以上(Office2003以上),引用以上兩個(gè)Com后,在項(xiàng)目引用欄發(fā)現(xiàn)多了Excel、Microsoft.Office.Core,VBIDE三個(gè) 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ù)需要移動(dòng)或移除它。
- 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中,并且目標(biāo)文件可以改寫
- System.IO.File.Copy(templetFile, outputFile, true);
- if (this.ExcelRS != null)
- ExcelRS = null;
- //實(shí)例化ExcelRS對(duì)象
- ExcelRS = new Microsoft.Office.Interop.Excel.ApplicationClass();
- //打開目標(biāo)文件outputFile
- RSbook = ExcelRS.Workbooks.Open(outputFile, missing, missing, missing, missing, missing,
- missing, missing, missing, missing, missing, missing, missing, missing, missing);
- //設(shè)置第一個(gè)工作溥
- RSsheet = (Microsoft.Office.Interop.Excel.Worksheet)RSbook.Sheets.get_Item(1);
- //激活當(dāng)前工作溥
- RSsheet.Activate();
- 在當(dāng)前工作溥寫入內(nèi)容#region 在當(dāng)前工作溥寫入內(nèi)容
- 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
- //保存目標(biāo)文件
- RSbook.Save();
- //設(shè)置DisplayAlerts
- ExcelRS.DisplayAlerts = false;
- ExcelRS.Visible = true;
- //ExcelRS.DisplayAlerts = true;
- //釋放對(duì)象
- RSsheet = null;
- RSbook = null;
- ExcelRS = null;
- //釋放內(nèi)存
- GcCollect();
- }
- catch (Exception ex)
- ...{
- MessageBox.Show(ex.ToString());
- return false;
- }
- return true;
- }
- #endregion
- public void GcCollect()
- ...{
- GC.Collect();
- GC.WaitForPendingFinalizers();
- GC.Collect();
- GC.WaitForPendingFinalizers();
- }
- }
- }
特別說(shuō)明:
a、引用Microsoft.Office.Interop.Excel;using Microsoft.Office.Core;
b、(關(guān)鍵)在程序中特別釋放Excel資源的時(shí)候既要設(shè)置對(duì)象為null,又要強(qiáng)制回收內(nèi)存,這樣才能徹底回收資源。
c、引用的Office組建版本是個(gè)敏感問(wèn)題,不同版本之間有細(xì)微差別,需要分別處理。
本文來(lái)自曾玄昴在CSDN博客中的文章《C#讀寫Excel文檔(---續(xù)C#讀寫Word文件)》
【編輯推薦】



















