怎么把一个excel导入到winform的dataGridView中 先去google如何操作excel,读取单元格内容,入库 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我还是个新手 主要是excel这方面的导入导出基本没用过 所以不会啊 调用: EcxelToDataGridView("test.xls",dataGridView1); /// Excel数据导入方法 public void EcxelToDataGridView(string filePath,DataGridView dgv) { //根据路径打开一个Excel文件并将数据填充到DataSet中 string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + filePath + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";//HDR=YES 有两个值:YES/NO,表示第一行是否字段名,默认是YES,第一行是字段名 OleDbConnection conn = new OleDbConnection(strConn); conn.Open(); string strExcel = ""; OleDbDataAdapter myCommand = null; DataSet ds = null; strExcel = "select * from [sheet1$]"; myCommand = new OleDbDataAdapter(strExcel, strConn); ds = new DataSet(); myCommand.Fill(ds, "table1"); dataGridView1.DataSource = ds.Tables[0].DefaultView; } http://www.cnblogs.com/yxhblog/archive/2012/06/06/2537642.html 大神 能给个全一点的代码么 [sheet1$]这个表名怎么获取啊 就是你建立EXCEL的sheet1,默认是这个,如果你用别的,这里修改就可,你还有什么疑问? private void button1_Click(object sender, EventArgs e) { //调用 EcxelToDataGridView(@"d:\\test.xls", dataGridView1); } 可以的, EcxelToDataGridView(你文件名的参数, dataGridView1); Invoke类型的已垃圾回收委托进行了回调,这可能会导致应用程序崩溃、损坏和数据丢失求大神指导! C#应用程序,如何实现退出系统这个按钮的功能【求帮助】 对象在作用域执行完毕后是否会自动释放 如何点击确定和取消后关闭窗口 textbox 问题求教! C#的四个基本技巧 可怜母亲身患癌症,寻求天下抗癌治癌的方法,恳求大家帮忙,知道不知道的,都帮我顶.跪求版主勿删我帖!! 在WIN98下装.NET构架,安装不上的问题!!! 我做了一个image的控件,大家给点意见吧 请教高手,大家帮帮忙。 关于Dns.GetHostByName和Dns.GetHostEntry MDI父窗口下生成子窗口问题
调用: EcxelToDataGridView("test.xls",dataGridView1);
/// Excel数据导入方法
public void EcxelToDataGridView(string filePath,DataGridView dgv)
{
//根据路径打开一个Excel文件并将数据填充到DataSet中
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + filePath + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";//HDR=YES 有两个值:YES/NO,表示第一行是否字段名,默认是YES,第一行是字段名
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel = "select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommand.Fill(ds, "table1");
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
private void button1_Click(object sender, EventArgs e)
{
//调用
EcxelToDataGridView(@"d:\\test.xls", dataGridView1);
}