110,534
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace testcheckbox
{
public partial class excel : Form
{
public excel()
{
InitializeComponent();
}
private string xlsname = string.Empty;
DataSet ds = new DataSet();
private void toolStripLabel1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "EXCEL文件(*.xls)|*.xls";
if (ofd.ShowDialog() == DialogResult.OK)
{
xlsname = ofd.FileName;
}
toolStripStatusLabel1.Text = xlsname;
readxls(xlsname);
}
private void readxls(string args_xlsname)
{
string connstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + args_xlsname + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\";";
toolStripStatusLabel1.Text = connstr;
using (OleDbConnection conn = new OleDbConnection(connstr))
{
using (OleDbDataAdapter da = new OleDbDataAdapter("Select * from [Sheet1$]", conn))
{
da.Fill(ds,"excel");
}
}
dataGridView1.DataSource = ds.Tables["excel"];
}
}
}