生活随笔
收集整理的這篇文章主要介紹了
将数据导入到已存在的excel文件中
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
CRUD數據訪問類基類 ? ? using?System; ? using?System.Collections; ? using?System.Collections.Generic; ? using?System.Text; ? using?System.Data; ? using?System.Data.OleDb; ? ? ? namespace?myexcel{ ? ????public?class?DbExcel ? ????{ ? ???????? ? ????????///?< summary > ? ????????///?獲取讀取連接字符串 ? ????????///?</ summary > ? ????????///?< param ?name ="phyfilepath" > </ param > ? ????????///?< returns > </ returns > ? ????????private?static?string?GetOptionConnstr(string?phyfilepath) ? ????????{ ? ????????????string?endstr ?=?phyfilepath .Substring(phyfilepath.IndexOf('.')?+?1); ? ????????????if?(endstr.ToLower()?==?"xlsx") ? ????????????{ ? ????????????????return?"Provider =Microsoft .ACE.OleDb.12.0;Extended?Properties =\"Excel?12.0;HDR =no ;\";Data?Source ="?+?phyfilepath; ? ????????????} ? ? ????????????return?"Provider =Microsoft .Jet.OLEDB.4.0;Extended?Properties =\"Excel?8.0;HDR =no ;\";Data?Source ="?+?phyfilepath; ? ????????} ? ? ? ????????///?< summary > ? ????????///?獲取操作連接字符串 ? ????????///?</ summary > ? ????????///?< param ?name ="phyfilepath" > </ param > ? ????????///?< returns > </ returns > ? ????????private?static?string?GetReadConnStr(string?phyfilepath) ? ????????{ ? ????????????string?endstr ?=?phyfilepath .Substring(phyfilepath.IndexOf('.')?+?1); ? ????????????if?(endstr.ToLower()?==?"xlsx") ? ????????????{ ? ????????????????return?"Provider =Microsoft .ACE.OleDb.12.0;Extended?Properties =\"Excel?12.0;HDR =yes ;IMEX =1 \";Data?Source ="+phyfilepath;? ? ????????????} ? ??? ? ????????????????return?"Provider =Microsoft .Jet."?+ ? ????????????????"OLEDB.4.0;Extended?Properties =\"Excel?8.0;HDR =Yes ;IMEX =1 \";Data?Source ="+phyfilepath;???????????????? ? ????????} ? ? ? ????????public?static?DataTable?GetExcelDataTable(string?phyfilepath) ? ????????{ ? ????????????OleDbConnection?conn ?=?null ; ? ????????????string?sheetName ?=?"Sheet1" ; ? ????????????DataTable?dataTable ?=?null ; ? ???????????? ? ????????????try ? ????????????{ ? ????????????????using?(conn ?=?new ?OleDbConnection(GetReadConnStr(phyfilepath))) ? ????????????????{ ? ????????????????????conn.Open(); ? ????????????????????DataTable?sheetNames ?=?conn .GetOleDbSchemaTable(OleDbSchemaGuid.Tables,?null); ? ? ????????????????????????foreach?(DataRow?dr?in?sheetNames.Rows) ? ????????????????????????{ ? ????????????????????????????sheetName ?=?dr [2].ToString().Trim(); ? ????????????????????????????break; ? ????????????????????????} ? ????????????????????OleDbDataAdapter?oada ?=?new ?OleDbDataAdapter("select?*?from?["?+?sheetName?+?"]",?conn); ? ????????????????????DataSet?ds ?=?new ?DataSet(); ? ????????????????????oada.Fill(ds,?"InitData"); ? ? ????????????????????dataTable ?=?ds .Tables["InitData"]; ? ????????????????} ? ????????????} ? ????????????catch?(Exception?ex) ? ????????????{ ? ????????????????throw?new?BaseDBException(ex.Message); ? ????????????} ? ????????????finally ? ????????????{ ? ????????????????conn.Close(); ? ????????????} ? ? ????????????return?dataTable; ? ????????} ? ? ????????public?static?DataTable?GetExcelSheetTable(string?phyfilepath) ? ????????{ ? ????????????OleDbConnection?conn ?=?null ; ? ????????????string?sheetName ?=?"Sheet1$" ; ? ????????????DataTable?dataTable ?=?null ; ? ? ????????????try ? ????????????{ ? ????????????????using?(conn ?=?new ?OleDbConnection(GetReadConnStr(phyfilepath))) ? ????????????????{ ? ????????????????????conn.Open(); ? ????????????????????OleDbDataAdapter?oada ?=?new ?OleDbDataAdapter("select?*?from?["?+?sheetName?+?"]",?conn); ? ????????????????????DataSet?ds ?=?new ?DataSet(); ? ????????????????????oada.Fill(ds,?"InitData"); ? ? ????????????????????dataTable ?=?ds .Tables["InitData"]; ? ????????????????} ? ????????????} ? ????????????catch?(Exception?ex) ? ????????????{ ? ????????????????throw?new?BaseDBException(ex.Message); ? ????????????????//return?null; ? ?????????????? ? ????????????} ? ????????????finally ? ????????????{ ? ????????????????conn.Close(); ? ????????????} ? ? ????????????return?dataTable; ? ????????} ? ? ????????public?static?DataTable?GetExcelSheetTable(string?phyfilepath,string?SheetName) ? ????????{ ? ????????????OleDbConnection?conn ?=?null ; ? ????????????string?sheetName ?=?SheetName ; ? ????????????DataTable?dataTable ?=?null ; ? ? ????????????try ? ????????????{ ? ????????????????using?(conn ?=?new ?OleDbConnection(GetReadConnStr(phyfilepath))) ? ????????????????{ ? ????????????????????conn.Open(); ? ????????????????????DataTable?sheetNames ?=?conn .GetOleDbSchemaTable(OleDbSchemaGuid.Tables,?null); ? ? ? ????????????????????OleDbDataAdapter?oada ?=?new ?OleDbDataAdapter("select?*?from?["?+?sheetName?+?"]",?conn); ? ????????????????????DataSet?ds ?=?new ?DataSet(); ? ????????????????????oada.Fill(ds,?"InitData"); ? ? ????????????????????dataTable ?=?ds .Tables["InitData"]; ? ????????????????} ? ????????????} ? ????????????catch?(Exception?ex) ? ????????????{ ? ????????????????throw?new?BaseDBException(ex.Message); ? ????????????} ? ????????????finally ? ????????????{ ? ????????????????conn.Close(); ? ????????????} ? ? ????????????return?dataTable; ? ????????} ? ???????? ? ????????public?static?void?ExcelColDataUpdate(string?phyfilepath,?string?sheetName,string?setvalue,string?where) ? ????????{ ? ????????????OleDbConnection?conn ?=?null ; ? ????????????try ? ????????????{ ? ????????????????using?(conn ?=?new ?OleDbConnection(GetOptionConnstr(phyfilepath))) ? ????????????????{ ? ????????????????????conn.Open(); ? ????????????????????OleDbCommand?cmd ?=?new ?OleDbCommand(); ? ????????????????????cmd.CommandType ?=?CommandType.Text; ? ? ????????????????????cmd.CommandText ?=?"UPDATE?[" +sheetName+"$]?"+setvalue+"?"+where; ? ????????????????????cmd.Connection ?=?conn ; ? ????????????????????cmd.ExecuteNonQuery(); ? ? ????????????????} ? ????????????} ? ????????????catch?(Exception?ex) ? ????????????{ ? ????????????????throw?new?BaseDBException(ex.Message); ? ????????????} ? ????????????finally ? ????????????{ ? ????????????????conn.Close(); ? ????????????} ? ???????? ? ???????? ? ????????} ? ? ????????public?static?void?ExcelColDataInsert(string?phyfilepath,?string?sheetName,?string?columnNames,?string?values) ? ????????{ ? ????????????OleDbConnection?conn ?=?null ; ? ????????????try ? ????????????{ ? ????????????????using?(conn ?=?new ?OleDbConnection(GetOptionConnstr(phyfilepath))) ? ????????????????{ ? ????????????????????conn.Open(); ? ????????????????????OleDbCommand?cmd ?=?new ?OleDbCommand(); ? ????????????????????cmd.CommandType ?=?CommandType.Text; ? ? ????????????????????cmd.CommandText ?=?"insert?into?[" ?+?sheetName?+?"$]?("?+?columnNames?+?")?values("?+?values?+?")"; ? ????????????????????cmd.Connection ?=?conn ; ? ????????????????????cmd.ExecuteNonQuery(); ? ???????????????????? ? ????????????????} ? ????????????} ? ????????????catch?(Exception?ex) ? ????????????{ ? ????????????????throw?new?BaseDBException(ex.Message); ? ????????????} ? ????????????finally ? ????????????{ ? ????????????????conn.Close(); ? ????????????} ? ???????? ? ????????} ? ? ????????public?static?void?ExcelVoucherDataInsert(string?filePath,?string?sheetName,?DataTable?dt) ? ????????{ ? ????????????StringBuilder?sb ?=?new ?StringBuilder(); ? ????????????if?(dt ?==?null?||?dt.Rows.Count ?==?0)?return; ? ????????????try ? ????????????{ ? ????????????????using?(OleDbConnection?conn ?=?new ?OleDbConnection(GetOptionConnstr(filePath))) ? ????????????????{ ? ????????????????????conn.Open(); ? ????????????????????foreach?(DataRow?row?in?dt.Rows) ? ????????????????????{ ? ????????????????????????OleDbCommand?cmd ?=?new ?OleDbCommand(); ? ????????????????????????cmd.CommandType ?=?CommandType.Text; ? ????????????????????????cmd.CommandText ?=?"insert?into?[" ?+?sheetName?+?"$]?values('"?+?row["FName"].ToString()?+?"','"?+?row["FNo"].ToString()?+?"','"?+?row["FIName"].ToString()?+?"')"; ? ????????????????????????cmd.Connection ?=?conn ; ? ????????????????????????cmd.ExecuteNonQuery(); ? ????????????????????} ? ????????????????} ? ????????????} ? ????????????catch?(Exception?ex) ? ????????????{ ? ????????????????throw?new?BaseDBException(ex.Message); ? ????????????} ? ????????} ? ? ????????public?static?void??ExcelVoucherDataInsert(string?filePath,?string?sheetName,?string?itemClass?,?string?number?,?string?name) ? ????????{ ? ????????????try ? ????????????{ ? ????????????????using?(OleDbConnection?conn ?=?new ?OleDbConnection(GetOptionConnstr(filePath))) ? ????????????????{ ? ????????????????????conn.Open(); ? ????????????????????OleDbCommand?cmd ?=?new ?OleDbCommand(); ? ????????????????????cmd.CommandType ?=?CommandType.Text; ? ? ????????????????????cmd.CommandText ?=?"insert?into?[" ?+?sheetName?+?"$]?values('"?+?itemClass?+?"','"?+?number?+?"','"?+?name?+?"')"; ? ????????????????????cmd.Connection ?=?conn ; ? ????????????????????cmd.ExecuteNonQuery(); ? ????????????????} ? ????????????} ? ????????????catch(Exception?ex) ? ????????????{ ? ????????????????throw?new?BaseDBException(ex.Message); ? ????????????} ? ????????} ? ???????? ? ? ????????public?static?void?ExcelColDataInsert(string?phyfilepath,?string?sheetName,?string?columnName,?string[]?values) ? ????????{ ? ????????????OleDbConnection?conn ?=?null ; ? ????????????try ? ????????????{ ? ????????????????using?(conn ?=?new ?OleDbConnection(GetOptionConnstr(phyfilepath))) ? ????????????????{ ? ????????????????????conn.Open(); ? ????????????????????OleDbCommand?cmd ?=?new ?OleDbCommand(); ? ????????????????????cmd.CommandType ?=?CommandType.Text; ? ? ????????????????????foreach?(string?str?in?values) ? ????????????????????{ ? ????????????????????????cmd.CommandText ?=?"insert?into?[" ?+?sheetName?+?"$]?("?+?columnName?+?")?values('?"?+?str?+?"')"; ? ????????????????????????cmd.Connection ?=?conn ; ? ????????????????????????cmd.ExecuteNonQuery(); ? ????????????????????} ? ????????????????} ? ????????????} ? ????????????catch?(Exception?ex) ? ????????????{ ? ????????????????throw?new?BaseDBException(ex.Message); ? ????????????} ? ????????????finally ? ????????????{ ? ????????????????conn.Close(); ? ????????????} ? ????????} ? ???? ? ????} ? ? }? 編寫連接與操作excel文件的通用函數 ?
代碼 ? ? protected?void?DoOleSql(string?sql,?string?database) ? ? ??{??? ? ? ??OleDbConnection?conn ?=?new ?OleDbConnection(); ? ? ??conn.ConnectionString ?=?"Provider=Microsoft.Jet.OLEDB.4.0;Data?Source=" ?+?Server.MapPath("\\")?+?database?+?";?Extended?Properties ='Excel?8.0;HDR=no;IMEX=0' "; ? ? ??try ? ? ??{//打開連接 ? ? ??conn.Open(); ? ? ??} ? ? ??catch?(Exception?e) ? ? ??{ ? ? ??Response.Write(e.ToString()); ? ? ??} ? ? ??OleDbCommand?olecommand ?=?new ?OleDbCommand(sql,?conn);??? ? ? ??try ? ? ??{//執行語句 ? ? ??olecommand.ExecuteNonQuery(); ? ? ??} ? ? ??catch?(Exception?eee) ? ? ??{ ? ? ??Response.Write(eee.ToString()); ? ? ??conn.Close(); ? ? ??} ? ? ??finally ? ? ??{ ? ? ??conn.Close();//關閉數據庫 ? ? ??} ? ? ??conn.Close(); ? ? }? 注:1)使用 Excel 工作簿時,默認情況下,區域中的第一行是標題行(或字段名稱)。如果第一個區域不包含標題,您可以在連接字符串的擴展屬性中指定 HDR=NO。如果您在連接字符串中指定 HDR=NO,Jet OLE DB 提供程序將自動為您命名字段(F1 表示第一個字段,F2 表示第二個字段,依此類推);2)IMEX=1將所有讀入數據看作字符,其他值(0、2)請查閱相關幫助文檔;3)如果出現“找不到可安裝的isam”錯誤,一般是連接字符串錯誤 3、從excel文件讀取數據 string sql = "select * from [sheet1$]"; DoOleSql(sql,"test.xls"); 4、更新excel文件中的數據 string sql = "update [sheet1$] set FieldName1='333' where FieldName2='b3'"; DoOleSql(sql,"test.xls"); 5、向excel文件插入數據 string sql = "insert into [sheet1$](FieldName1,FieldName2,…) values('a',’b’,…)"; DoOleSql(sql,"test.xls"); 6、刪除excel文件中的數據:不提倡使用這種方法 7、對于非標準結構的excel表格,可以指定excel中sheet的范圍 1)讀取數據:string sql = "select * from [sheet1$A3:F20]"; 2)更新數據:string sql = "update [sheet1$A9:F15] set FieldName='333' where AnotherFieldName='b3'"; 3)插入數據:string sql = "insert into [sheet1$A9:F15](FieldName1,FieldName2,…) values('a',’b’,…)"; 4)刪除數據:不提倡 注:1)代碼根據需要可以自行修改;2)如果出現“操作必須使用一個可更新的查詢”錯誤,可能sql語句中對excel文件中的“字段”引用有錯誤,或對 excel文件不具有“修改”權限;3)如果出現“不能擴充選定范圍”錯誤,可能是對excel文件引用的“范圍”有錯誤。
轉載于:https://blog.51cto.com/linzheng/1080854
總結
以上是生活随笔 為你收集整理的将数据导入到已存在的excel文件中 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。