C#对Excel进行处理
C#对Excel进行处理
方法:使用微软的Microsoft.Office.Interop.Excel插件,直接调用,非常简单。可以在微软官网下载,
也可以私聊我要源码。
不说废话,直接上代码,代码如下:
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.Data.OleDb;
using System.IO;
namespace WindowsFormsApplication_Excel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string filePath = "";
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
// openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "*.*|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
filePath = openFileDialog1.FileName;
System.Data.DataTable dt = ImportExcel(filePath);
this.dataGridView1.DataSource = dt;
}
}
private void button2_Click(object sender, EventArgs e)
{
System.Data.DataTable dt = this.dataGridView1.DataSource as System.Data.DataTable;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "*.xls|*.*";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
// ExportExcel(saveFileDialog1.FileName + ".xls", dt);
ExportExcel(saveFileDialog1.FileName, dt);
}
}
private System.Data.DataTable ImportExcel(string path)
{
DataSet ds = new DataSet();
string strConn = "";
if (Path.GetExtension(path) == ".xls")
{
strConn = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;", path);
}
else
{
strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0;", path);
}
using (var oledbConn = new OleDbConnection(strConn))
{
oledbConn.Open();
var sheetName = oledbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new[] { null, null, null, "Table" });
var sheet = new string[sheetName.Rows.Count];
for (int i = 0, j = sheetName.Rows.Count; i < j; i++)
{
sheet[i] = sheetName.Rows[i]["TABLE_NAME"].ToString();
}
var adapter = new OleDbDataAdapter(string.Format("select * from [{0}]", sheet[0]), oledbConn);
adapter.Fill(ds);
}
return ds.Tables[0];
}
public void ExportExcel(string fileName, System.Data.DataTable dt)
{
string FileName = fileName;
long totalCount = dt.Rows.Count; long rowRead = 0;
float percent = 0;
Microsoft.Office.Interop.Excel.Application xlApp = null;
xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
Microsoft.Office.Interop.Excel.Range range;
for (int i = 0; i < dt.Columns.Count; i++)
{
worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1];
}
for (int r = 0; r < dt.Rows.Count; r++)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
worksheet.Cells[r + 2, i + 1] = dt.Rows[r][i];
}
rowRead++;
percent = ((float)(100 * rowRead)) / totalCount;
this.Text = "正在导出数据,已导出[" + percent.ToString("0.00") + "%]...";
System.Windows.Forms.Application.DoEvents();
}
//range = worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[dt.Rows.Count + 2, dt.Columns.Count]);
workbook.Saved = true;
workbook.SaveCopyAs(FileName);
MessageBox.Show("保存成功!");
}
}
}
初始画面如下:
打开表格: