您的位置:首页 > 编程语言 > C#

C#读取Excel的方法

2013-12-24 12:34 405 查看
 

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