c#操作excel的方法

时间:2024-10-13 02:27:53

1、新建项目,添加按钮,功能是读取excelDataTable dt = new DataTable();dt = ReadExcel("D:\\搜狗高速下载\\新建文件夹\\resdandwrite\\111.xlsx", "表格");目录和excel的表格名称

c#操作excel的方法

2、读取excel的方法DataSet ds = new DataSet(); string strConn = "Provider=Microsoft.Ace.OleDb.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=NO;IMEX=1';"; try { OleDbConnection conn = new OleDbConnection(strConn); string sql = "select * from [" + SheetName + "$]"; conn.Open(); OleDbDataAdapter myCommand = new OleDbDataAdapter(sql, strConn); myCommand.Fill(ds, SheetName + "$]"); DataTable dt = ds.Tables[0]; conn.Close(); } catch (Exception ex) { return null; }SheetName就是表格名称

3、插入数据string sql = "Insert Into [" + SheetName + "$](A,B,C) Values(" + t1.Text +" ," +t2.Text+"," + t3.Text+")";

c#操作excel的方法

4、更新数据 衡痕贤伎string sql = "UPDATE [" + SheetName + "$] set A=媪青怍牙" + dd.Rows[i][0].ToString() + " ,B=" + dd.Rows[i][1].ToString() + ",C=" + dd.Rows[i][2].ToString() + " where 序号=" + (i + 1);

c#操作excel的方法
© 手抄报圈