excel导入导出到数据库(Excel import and export to database).docVIP

  • 58
  • 0
  • 约4.51千字
  • 约 15页
  • 2017-09-08 发布于河南
  • 举报

excel导入导出到数据库(Excel import and export to database).doc

excel导入导出到数据库(Excel import and export to database)

excel导入导出到数据库(Excel import and export to database) 使用系统; 使用system.collections.generic; 使用LINQ系统; 使用系统文本; 使用microsoft.office.interop.excel; 使用system.data.oledb; 使用系统数据; 使用系统反射; 命名空间常见 { excelio公共类 { 私有_returnstatus; _returnmessage字符串; 私人const string create_error =“无法创建Excel文件,可能是你的电脑没有安装Excel!”; 私人const string import_error =“您的Excel文件打开,请保存并关闭它。”; 私人const string export_error =“这是一个错误,Excel文件可以打开时,它是出口。“n”; public int returnstatus { 得到的回报_returnstatus {;} } 公共字符串返回消息 { 得到的回报_returnmessage {;} } 公共excelio() { } 摘要 / / / Excel文件的导出 Each sheet / / / for a DataTable / / / /summary The full path of /param fileName name= param / / / Excel file / / / returns/returns Public DataSet ImportExcel (string fileName) { Application xlApp = new, ApplicationClass (); If (xlApp = null) { _ReturnStatus = -1; _ReturnMessage = CREATE_ERROR; Return null; } Workbook workbook; Try { Workbook = xlApp.Workbooks.Open (fileName, 0, false, 5, 、,), false, XlPlatform.xlWindows,,, true, false, 0, true, 1, 0); } Catch { _ReturnStatus = -1; _ReturnMessage = IMPORT_ERROR; Return null; } Int n = workbook.Worksheets.Count; String[] SheetSet = new string[n]; System.Collections. ArrayList al =新系统arraylist()。收藏; 对于(int = i 1;;i = n;i + +) { sheetset [我- 1 ] =((表)的工作簿。工作表[我]的名字); } 工作簿。关闭(NULL,NULL,NULL); xlapp。quit(); 如果(工作簿)!= null) { 系统运行时,即可看到。元帅。releasecomobject(练习册); 工作簿=空; } 如果(xlapp!= null) { 系统运行时,即可看到。元帅。releasecomobject(xlapp); xlapp = null; } collect() GC; DataSet ds =新dataset(); 字符串connstr =“商=微软。飞机。OLEDB数据源。4;=”+文件名+“;扩展属性= Excel 8”; 使用(OleDbConnection conn =新OleDbConnection(connstr)) { 康涅狄格州open(); OleDbDataAdapter da; 对于(int = i 1;;i = n;i + +) { SQL select * from [”+ sheetset [我- 1 ] +”$ ]”; DA =新数据适配器(SQL,Conn); 大。填充(DS,sheetset [我- 1 ]); dispose() DA; } 康涅狄格州(); 康涅狄格州dispose(); } 返回DS; } 摘要 / / / Excel导入 / / /每个DataTable为一个工作表,工作表的名字为DataTable的名字 // / / / 参数名=“DS”数据源 /参数 / / / 参数名=“savefilename”要保存的Excel文件的完整路径 /参数 // 返回 返回 市民bool ExportExcel(集DS,字符串savefilename) { 如果(DS = NULL) { _retur

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档