Excel表如何导入数据库(如SQL数据库或ACCESS数据)?求代码
7 回答
汪汪一只猫
TA贡献1898条经验 获得超8个赞
public static DataSet ReadExcelFileToDataSet(String fileName, string sheetName) { FileInfo fileInfo = new FileInfo(fileName); String strConn; if (fileInfo.Extension.ToLower() == ".xlsx") { strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0 Xml;HDR=YES'"; } else { throw new Exception("only .xlsx file can be read"); } OleDbConnection Conn = new OleDbConnection(strConn); DataSet ds = new DataSet(); try { string SQL = "select * from [" + sheetName + "$]"; Conn.Open(); OleDbDataAdapter da = new OleDbDataAdapter(SQL, strConn); da.Fill(ds); return ds; } catch (Exception e) { throw new Exception(e.Message); } finally { Conn.Close(); Conn.Dispose(); } }
- 7 回答
- 0 关注
- 645 浏览
添加回答
举报
0/150
提交
取消