C# 读取excel组件方法?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 读取excel组件方法?相关的知识,希望对你有一定的参考价值。
这是我以前写的winform 把Excel 数据导入到Access 表中 下面的后台代码using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
//Download by http://www.codefans.net
namespace ParkLottery
public partial class ImportPerson : Form
public ImportPerson()
InitializeComponent();
private void button1_Click(object sender, EventArgs e)
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "Excel文件(*.xls)|*.xls";
if (dlg.ShowDialog() == DialogResult.OK)
string filePath = dlg.FileName;
this.textBox1.Text = filePath;
private void button2_Click(object sender, EventArgs e)
if (textBox1.Text.Length == 0)
MessageBox.Show("请选择导入数据的Execl文件");
else
try
OleDbConnectionStringBuilder connectStringBuilder = new OleDbConnectionStringBuilder();
connectStringBuilder.DataSource = this.textBox1.Text.Trim();
connectStringBuilder.Provider = "Microsoft.Jet.OLEDB.4.0";
connectStringBuilder.Add("Extended Properties", "Excel 8.0");
using (OleDbConnection cn = new OleDbConnection(connectStringBuilder.ConnectionString))
DataSet ds = new DataSet();
string sql = "Select * from [Sheet1$]";
OleDbCommand cmdLiming = new OleDbCommand(sql, cn);
cn.Open();
using (OleDbDataReader drLiming = cmdLiming.ExecuteReader())
ds.Load(drLiming, LoadOption.OverwriteChanges, new string[] "Sheet1" );
DataTable dt = ds.Tables["Sheet1"];
if (dt.Rows.Count > 0)
for (int i = 0; i < dt.Rows.Count; i++)
//写入数据库数据
string MySql = "insert into ClientInfo values('"+dt.Rows[i]["姓名"].ToString()+"','"+dt.Rows[i]["姓名"].ToString()+"','0','"+dt.Rows[i]["备注"].ToString()+"','0','"+i.ToString()+"')";
new DataAccess().SQLExecute(MySql); //执行sql语句插入到access数据库中
MessageBox.Show("数据导入成功!");
else
MessageBox.Show("请检查你的Excel中是否存在数据");
catch (Exception ex)
MessageBox.Show(ex.ToString());
追问
我想请问一下有没有快速读取excel数据到数据库的方法 类似于批量导入 非常感谢
参考技术A NOPI组件可以读取EXCEL方法,你在网上找一下。本回答被提问者采纳以上是关于C# 读取excel组件方法?的主要内容,如果未能解决你的问题,请参考以下文章
C#调用OleDbConnection类读取Excel表格时,报错外部组件发生异常!