C#读取Excel的方法
2013-12-24 12:34
447 查看
C#读写Excel文件并打印输出的Demo
1、 创建一个叫DemoExcel的项目
2、 引用COM,包括:Microsoft.Excel.x.0.Object.Library,Microsoft.Office.x.0.Object.Library
建议安装正版OFFICE,而且版本在11.0以上(Office2003以上),引用以上两个Com后,在项目引用栏发现多了Excel、Microsoft.Office.Core,VBIDE三个 Library.
代码
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: 这行代码将数据加载到表“dataSet1.STREET”中。您可以根据需要移动或移除它。 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输出前初始化 /**//// <summary> /// /// </summary> /// <returns></returns> 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(); } } }
文章转载自: C#如何读取Excel http://www.studyofnet.com/news/259.html
相关文章推荐
- C# 读取EXCEL文件的三种经典方法
- ASP.NET(C#) 读取EXCEL另加解决日期问题的方法分享
- C# 用数据库读取Excel出现“定义了过多字段”错误的解决方法
- C# 读取EXCEL文件的三种经典方法
- C# 读取EXCEL文件的三种经典方法
- C# 用数据库读取Excel出现“定义了过多字段”错误的解决方法 .
- C#_读取EXCEL文件的三种经典方法.
- C#读取Excel方法总结
- C#读取Excel 几种方法的体会
- C# 读取excel 几种方法的集合 读取excel多sheet
- C#读取Excel几种方法的体会
- 运用C#读取导入的Excel部分数据为空的解决方法
- C#读取Excel并转化成XML的方法
- C# 读取EXCEL文件的三种经典方法
- c# 读取excel数据的两种方法
- C#使用Ado.net读取Excel表的方法
- C#读取Excel到DataSet的方法
- C# 读取EXCEL文件的三种经典方法
- 解决C#读取EXCEL中数字无法读取的方法
- 用c#读取excel文件缺少第一行数据解决方法