- 58
- 0
- 约4.51千字
- 约 15页
- 2017-09-08 发布于河南
- 举报
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
您可能关注的文档
- 4wd与awd的区别(The difference between 4WD and AWD).doc
- 4土壤环境污染与防治(4 soil environment pollution and control).doc
- 4招赶走孕期4种常见病(4 trick to drive out 4 common diseases during pregnancy).doc
- 4rcp营销组合策略探析_以竹碳制品为例(4rcp analysis of the marketing strategy to _ bamboo carbon products for example).doc
- 4月22日 《汉代风云人物—窦婴与外戚》 易中天.shtml(April 22nd - Han Dynasty and influential man Dou Ying Yi Zhongtian.Shtml).doc
- 4字词语(4 character word).doc
- 4月26日 培育家(April 26th foster).doc
- 4月7日 《话说及时雨宋江》 张 俊(In April 7th as rain in time Zhang Jun).doc
- 46中生活小常识+10种适合睡前进食的食(46, +10 common sense of life, suitable for eating before going to bed eating).doc
- 4种传男不传女的遗传病 别生儿子(4 kinds of hereditary diseases of male and female, don't give birth to a son).doc
- 广东省广州省实验中学教育集团2025-2026学年八年级上学期期中考试物理试题(解析版).docx
- 广东省广州大学附属中学2025-2026学年八年级上学期奥班期中物理试题(解析版).docx
- 广东省广州市第八十六中学2025-2026学年八年级上学期期中物理试题(含答案).docx
- 广东省广州市第八十九中学2025-2026学年八年级上学期期中考试物理试题(解析版).docx
- 广东省广州市第二中学2025-2026学年八年级上学期期中考试物理试题(含答案).docx
- 广东省广州市第八十六中学2025-2026学年八年级上学期期中物理试题(解析版).docx
- 广东省广州市第八十九中学2025-2026学年八年级上学期期中考试物理试题(含答案).docx
- 广东省广州市第二中学2025-2026学年八年级上学期期中考试物理试题(解析版).docx
- 2026《中国人寿上海分公司营销员培训体系优化研究》18000字.docx
- 《生物探究性实验教学》中小学教师资格模拟试题.docx
原创力文档

文档评论(0)