c#用npoi读取excel
来源:互联网 发布:网络销售的技巧和话术 编辑:程序博客网 时间:2024/06/05 19:50
下载NPOI的library http://npoi.codeplex.com/downloads/get/872547
然后在vs2012中添加引用NPOI.dll
读取excel中的一个表格的内容并且返回datatable类型
public DataTable GetExcelSheet(string filename,int sheetloc) //获取excel中工作薄的表,返回datatable { HSSFWorkbook workbook = new HSSFWorkbook(new FileStream(filename, FileMode.Open));//打开excel HSSFSheet sheet =(HSSFSheet) workbook.GetSheetAt(sheetloc); //获取sheet DataTable data = new DataTable(); HSSFRow headerrow = (HSSFRow)sheet.GetRow(0); //获取sheet1的第一行 int cellcount = headerrow.LastCellNum;//获取列数 int rowCount = sheet.LastRowNum-1;//获取数据行数 for (int i = headerrow.FirstCellNum; i < cellcount; i++) { DataColumn datacolumn = new DataColumn(headerrow.GetCell(i).StringCellValue); data.Columns.Add(datacolumn);//添加datatable的列数 } for (int i = sheet.FirstRowNum + 1; i < rowCount; i++) //填充数据 { DataRow datarow=data.NewRow(); //创建新行 HSSFRow row=(HSSFRow)sheet.GetRow(i); //获取当前行数 for (int j = headerrow.FirstCellNum; j < cellcount; j++) { if (row.GetCell(j)!= null)//判断是否为空 { datarow[j] = row.GetCell(j).ToString();//往该datatable的这行添加列内容 } } data.Rows.Add(datarow); } workbook= null; sheet = null; return data;//返回datatable }
0 0
- c#用npoi读取excel
- C#用NPOI读取excel
- c# NPOI EXcel读取
- asp.net C# NPOI 读取 Excel 文件
- C# NPOI 读取excel到DataTable
- C#使用NPOI读取excel模板,并导出excel
- NPOI读取Excel
- NPOI读取excel数据
- NPOI读取excel
- NPOI读取Excel
- C#NPOI读取Excel
- Npoi导出Excel、读取Excel
- C# 用NPOI保存为Excel文件
- C# NPOI 操作excel
- C# NPOI 操作excel
- C# NPOI导入Excel
- C# NPOI读写Excel
- C# NPOI 导出excel
- div页面居中
- Windows 下pip安装以及virtualenv环境搭建
- LeetCode--Excel Sheet Column Title
- 不恰当使用线程池处理 MQ 消息引起的故障
- c3p0连接池配置
- c#用npoi读取excel
- 【curl】error while loading shared libraries: libpcre.so.1
- 【整理】uclibc,eglibc,glibc之间的区别和联系
- GTK入门学习:信号与回调函数
- __declspec(dllimport)的理解
- iOS之KVC使用
- XML 中的CDATA
- div在父级元素中浮动定位
- 从最后一个acitvity跳转到第一个退出多个acitvity