c#winform界面如何将excel导入数据库sql server 求助
2个回答
展开全部
1.建立与excel表的连接,获得工作表ObjWorkSheet,
using Microsoft.Office.Interop.Excel;
Microsoft.Office.Interop.Excel.Application ObjExcel = new Microsoft.Office.Interop.Excel.Application();
Workbook ObjWorkBook;
Worksheet ObjWorkSheet = null;
ObjWorkBook = ObjExcel.Workbooks.Open(sPathName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in ObjWorkBook.Sheets){if (sheet.Name.ToLower() == sheetName.ToLower()){ObjWorkSheet = sheet;break;}}if (ObjWorkSheet == null)
throw new Exception(string.Format("{0} not found!!", sheetName));
2.得到单元格中的内容,注意i从1开始,j从1开始计,i==1时,对应字段名称所在的行
Range cell = (Range)ObjWorkSheet.Cells[i, j];
单元格的值为cell.value2
3.设置字段类型需要做一个交互界面,你先获得并显示字段名称,然后对其设置类型
4.由交互界面获得字段和字段类型,利用你的编程接口将excel的数据导入数据库
我的是用oracle数据库的,与你的接口不同,所以只能说这么多了.
using Microsoft.Office.Interop.Excel;
Microsoft.Office.Interop.Excel.Application ObjExcel = new Microsoft.Office.Interop.Excel.Application();
Workbook ObjWorkBook;
Worksheet ObjWorkSheet = null;
ObjWorkBook = ObjExcel.Workbooks.Open(sPathName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in ObjWorkBook.Sheets){if (sheet.Name.ToLower() == sheetName.ToLower()){ObjWorkSheet = sheet;break;}}if (ObjWorkSheet == null)
throw new Exception(string.Format("{0} not found!!", sheetName));
2.得到单元格中的内容,注意i从1开始,j从1开始计,i==1时,对应字段名称所在的行
Range cell = (Range)ObjWorkSheet.Cells[i, j];
单元格的值为cell.value2
3.设置字段类型需要做一个交互界面,你先获得并显示字段名称,然后对其设置类型
4.由交互界面获得字段和字段类型,利用你的编程接口将excel的数据导入数据库
我的是用oracle数据库的,与你的接口不同,所以只能说这么多了.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |