分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

.net导入excel文件到dataset

发布时间:2023-09-06 02:25责任编辑:彭小芳关键词:excel

1.需要引入:

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

//2007及以上版本
XSSFWorkbook xWorkbook=new XSSFWorkbook(fileStream);
//2003版本
HSSFWorkbook hWorkbook=new HSSFWorkbook(fileStream);

2.传入文件路径,返回dataset数据集合

public static DataSet ImportExcel(string filePath)
???????{
???????????DataSet ds = null;
???????????try
???????????{
???????????????FileStream fileStream = new FileStream(filePath, FileMode.Open);
???????????????HSSFWorkbook workbook = new HSSFWorkbook(fileStream);
???????????????ISheet sheet = null;
???????????????IRow row = null;
???????????????ds = new DataSet();
???????????????DataTable dt = null;
???????????????for (int i = 0; i < workbook.Count; i++)
???????????????{
???????????????????dt = new DataTable();
???????????????????dt.TableName = "table" + i.ToString();
???????????????????//获取sheet表
???????????????????sheet = workbook.GetSheetAt(i);
???????????????????//起始行索引
???????????????????int rowIndex = sheet.FirstRowNum;
???????????????????//获取行数
???????????????????int rowCount = sheet.LastRowNum;
???????????????????//获取第一行
???????????????????IRow firstRow = sheet.GetRow(rowIndex);
???????????????????//起始列索引
???????????????????int colIndex = firstRow.FirstCellNum;
???????????????????//获取列数
???????????????????int colCount = firstRow.LastCellNum;
???????????????????DataColumn dc = null;
???????????????????//获取列数
???????????????????for (int j = colIndex; j < colCount; j++)
???????????????????{
???????????????????????dc = new DataColumn(firstRow.GetCell(j).StringCellValue);
???????????????????????dt.Columns.Add(dc);
???????????????????}
???????????????????//跳过第一行列名
???????????????????rowIndex++;
???????????????????for (int k = rowIndex; k <= rowCount; k++)
???????????????????{
???????????????????????DataRow dr = dt.NewRow();
???????????????????????row = sheet.GetRow(k);
???????????????????????for (int l = colIndex; l < colCount; l++)
???????????????????????{
???????????????????????????if (row.GetCell(l) == null)
???????????????????????????{
???????????????????????????????continue;
???????????????????????????}
???????????????????????????else {
???????????????????????????????row.GetCell(l).SetCellType(CellType.String);
???????????????????????????????//stuUser.setPhone(row.getCell(0).getStringCellValue());
???????????????????????????????dr[l] = row.GetCell(l).StringCellValue;
???????????????????????????}
???????????????????????????//dr[l] = row.GetCell(l).StringCellValue;

???????????????????????}
???????????????????????dt.Rows.Add(dr);
???????????????????}
???????????????????ds.Tables.Add(dt);
???????????????}
???????????????sheet = null;
???????????????workbook = null;
???????????????fileStream.Close();
???????????????fileStream.Dispose();
???????????}
???????????catch (Exception ex)
???????????{
???????????????throw;
???????????}
???????????return ds;
???????}

3.调用方法:

DataSet ds = ImportExcel(“excel文件路径“);

.net导入excel文件到dataset

原文地址:https://www.cnblogs.com/lymkpds/p/9108146.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved