C#ListView控件OleDb方法读取EXCEL

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#ListView控件OleDb方法读取EXCEL相关的知识,希望对你有一定的参考价值。

我是一个初学者,用OleDb方法读取EXCEL,发现,excel中只有一条记录的时候,程序会在序号列多出一个2,而excel要有两行以上的记录,就没有问题,如下图所示:

下面是C#代码
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;
private void button1_Click(object sender, EventArgs e)

OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Excel文件(*xls)|*xls";
dialog.FileName = "";
if (dialog .ShowDialog()==DialogResult.OK)

string PathString = dialog.FileName.Trim(); //定义文件路径
this.listView1.Items.Clear();//先清空表格
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + PathString + ";" + "Extended Properties ='Excel 8.0;HDR=NO;IMEX=1';";
string strExcel = "select*from [sheet1$]";//指定命令对象的命令文本
OleDbConnection conn = null; //定义一个OleDbConnection对象
try

conn = new OleDbConnection(strConn ); //实例化
conn.Open();
OleDbCommand cmd = new OleDbCommand(strExcel,conn ); //定义命令对象
OleDbDataReader rears = cmd.ExecuteReader(); //执行命令对象,并用reas指向结果集
int i =1;
while (rears .Read ()) // 读取一条数据

string profile = rears[0].ToString();
ListViewItem Lv = new ListViewItem();
Lv.SubItems[0].Text = i.ToString();
i++;
Lv.SubItems.Add(profile);
listView1.Items.Add(Lv);


catch (OleDbException ex)

MessageBox.Show (("打开错误!" + ex.Message));

finally

conn.Close();




参考技术A int i =0;
Excel 的编号是冲0开始的!你从0开始的时候添加,
还有,你可以去看看NPOI,对于Excel的操作,我建议你使用NPOI来操作,非常的方便。追问

即使是从0开始,也会出现上面的问题,只不过第二行序列号处不是2了,而是1.请高手试试。

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#ListView控件OleDb方法读取EXCEL的主要内容,如果未能解决你的问题,请参考以下文章

从 ORACLE 读取的最快 OLEDB

OleDB读取Myxls生成的Excel文本,结果只能读取到第一列的值

如何将数据库里的数据添加到listview中

用C#listview控件Details类型,发现当拉动表头调整列宽度过程时,listview会重画,造成不停的闪烁

C#如何读取xml文件内容并用listview控件显示

c#listview添加控件时不显示滚动条