npoi

NPOI - Get excel row count to check if it is empty

谁说胖子不能爱 提交于 2019-12-23 09:31:49
问题 I'm reading an xlsx file using NPOI lib, with C# . I need to extract some of the excel columns and save the extracted values into some kind of data structure. I can successfully read the file and get all the values from the 2nd (the first one contains only headers) to the last row with the following code: ... workbook = new XSSFWorkbook(fs); sheet = (XSSFSheet)workbook.GetSheetAt(0); .... int rowIndex = 1; //--- SKIP FIRST ROW (index == 0) AS IT CONTAINS TEXT HEADERS while (sheet.GetRow

How to get the Excel Cell value by address (A1, A2) using NPOI

一曲冷凌霜 提交于 2019-12-22 08:05:14
问题 I have an Excel Cell Address like A1, A2. How do I access this cell programmatically using NPOI framework in C#? Some Java POI example code I found: CellReference cr = new CellReference("A1"); row = mySheet.getRow(cr.getRow()); cell = row.getCell(cr.getCol()); 回答1: The Java code you found translates 1:1 into C#: First you convert the cell description (A1) into a CellReference Use the Row and Col from that CellReference to lookup the actual cell. Here is some sample code var workbook = new

How to get the Excel Cell value by address (A1, A2) using NPOI

亡梦爱人 提交于 2019-12-22 08:05:10
问题 I have an Excel Cell Address like A1, A2. How do I access this cell programmatically using NPOI framework in C#? Some Java POI example code I found: CellReference cr = new CellReference("A1"); row = mySheet.getRow(cr.getRow()); cell = row.getCell(cr.getCol()); 回答1: The Java code you found translates 1:1 into C#: First you convert the cell description (A1) into a CellReference Use the Row and Col from that CellReference to lookup the actual cell. Here is some sample code var workbook = new

winform npoi 将execl转换成datatable,导入数据库

最后都变了- 提交于 2019-12-22 07:06:30
直接在vs中用nuget安装npoi NPOI 是 POI 项目的 .NET 版本。POI是一个开源的Java读写Excel、WORD等微软OLE2组件文档的项目。使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 WORD/EXCEL 文档进行读写。 优点:读取Excel速度较快,读取方式操作灵活性 导入数据库的代码我没贴,看你需要怎么导入,一条一条导入,还是批量导入咯。都可以,反正datatable已经得到了. //获取execl信息 public DataTable getExeclinfo() { DataTable dt = new DataTable(); //打开文件,获取execl文件 System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog(); if (fd.ShowDialog() == DialogResult.OK) { if (fd.FileName.Contains(".xls") && fd.FileName.Contains(".xlsx")) { fileName = fd.FileName; dt = ExcelToDataTable("sheet1", true); } else { MessageBox.Show("请选择execl文件"); } }

MemoryStream seems be closed after NPOI workbook.write?

旧巷老猫 提交于 2019-12-20 16:24:14
问题 I am using NPOI to convert DataTable to Excel in a ASP.NET Web API project. But the I got nothing from the response. Here's my code: public HttpResponseMessage GetExcelFromDataTable(DataTable dt) { IWorkbook workbook = new XSSFWorkbook(); // create *.xlsx file, use HSSFWorkbook() for creating *.xls file. ISheet sheet1 = workbook.CreateSheet(); IRow row1 = sheet1.CreateRow(0); for (int i = 0; dt.Columns.Count > i; i++) { row1.CreateCell(i).SetCellValue(dt.Columns[i].ColumnName); } for (int i =

C# NPOI set cell style to Text / string 1-19 is formatted as a date / disable any formating

夙愿已清 提交于 2019-12-20 07:28:45
问题 I am creating an excel, when i write some values example 1-19, when I open the excel doc, i see 1-19, but if i click on it then excel tries to format it as a date IS THERE A WAY to force the sheet to not use any formulas or formatting? I have checked and the dataformat is string. private void Test1(ref ISheet worksheet, string[] array, IWorkbook workbook, int iRow, XSSFFont font2) { var format = HSSFDataFormat.GetBuiltinFormats(); ICellStyle _TextCellStyle = workbook.CreateCellStyle();

NPOI操作Excel

无人久伴 提交于 2019-12-20 05:25:49
一、引言 昨天公司EDI导入功能出现问题,经排查是由于引用Com组件Microsoft.Office.Interop.Excel.dll,操作Excel引起,可是现在客户电脑压根都没装Office,还倒个鬼数据啊(画个圈圈xx下之前写这个功能的人),一次重构操作Excel功能之旅由此开始。。。 二、NPOI操作Excel 1.整个Excel(工作薄):WorkBook,包含的页(工作表):Sheet, 行:Row, 单元格:Cell。 2.HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xls ; XSSFWorkbook:是操作Excel2007及以上的版本,扩展名是.xlsx。 3.NPOI组件下载地址:https://npoi.codeplex.com/ 4.如果是.NET Framwork环境,请将下载的npoi组件都引用一下,如果是.NET Core环境,就在NuGet程序包中搜索安装 DotNetCore.NPOI 这个程序包即可 5.NPOI源码地址:https://github.com/tonyqus/npoi 下面是NPOI读写Excel的例子,ExcelHelper是自己封装的类库,包括创建Excel,Excel写入txt,Excel写入DataTable,DataTable写入Excel等 using System;

.NET Core 2.0 开源Office组件 NPOI

本秂侑毒 提交于 2019-12-20 05:25:23
前言 去年 12 月,我移植了大家所熟知 NPOI 到 .NET Core 版本,这里是当时发的博客,当时得到了很多同学的支持,社区反应也很好,在这里非常感谢当时推荐的朋友们。 去年的那个版本是针对于 .NET Core 1.0 的,从发布截止现在在 NuGet 大概有 2K 多的下载量,说明还是有很多同学在使用 NPOI 的,社区中也得到了很多同学的推广。 但是上一个移植的版本也有诸多缺陷和 bug,在 Github 上也收到了一些 Issue 进行反馈,很多 Bug 可能是移植过程中的bug,但是对于这些 Bug 可能我也无能为力,因为 NPOI 的代码是非常庞大和复杂的。 随着 .NET Core 2.0 的发布,我又重新移植了一遍 NPOI,注意是重新移植而不是从 1.0 版本迁移过来,由于 .NET Standard 2.0 的 API 增加了很多,所以移植过程还算顺利,这次移植应该是最大限度的保持了 NPOI 的原汁原味,敬请客官体验。 什么是 NPOI NPOI 是 构建在POI 3.x 版本之上的一个C#库,NPOI 可以在没有安装Office的情况下对 Word 或 Excel 文档进行读写操作。 POI是一个开源 的Java 读写Excel、WORD等微软OLE2组件文档的项目。 NPOI 由瞿总和他的团队由 Apache POI 移植到 .NET 的

“NPOI” 实现“Excel”导入

老子叫甜甜 提交于 2019-12-19 12:00:29
“NPOI” 可以实现“Excel”文件的导出和导入。这里主要指office2003的“.XSL”文件,而不是office2007的“.XSLX”文件的导入,如果可以兼容office2007了话,还请指教。 一、前台页面文件格式判断 function checkType() { //得到上传文件的值 var fileName = document.getElementById("ful_ImportIn").value; //返回String对象中子字符串最后出现的位置. var seat = fileName.lastIndexOf("."); //返回位于String对象中指定位置的子字符串并转换为小写. var extension = fileName.substring(seat).toLowerCase(); //判断允许上传的文件格式 //var allowed=[".jpg",".gif",".png",".bmp",".jpeg"]; var allowed = [".xls"]; for (var i = 0; i < allowed.length; i++) { if (!(allowed[i] != extension)) { return true; } } alert("不支持" + extension + "格式"); return false; }

How to use NPOI to read Excel spreadsheet that contains empty cells?

孤人 提交于 2019-12-18 14:17:34
问题 When I read Excel worksheet using NPOI, empty cells are skipped. For example, it the row contains A, B, , C and I read it using IRow row = sheet.GetRow(rowNb) then row.Cells[1].ToString() will output B (as expected) but row.Cells[2].ToString() will output C instead of an empty string. Is there a way to keep empty cells? Thanks. 回答1: Try the GetCell method with the MissingCellPolicy : ICell cell = row.GetCell(2, MissingCellPolicy.RETURN_NULL_AND_BLANK); 来源: https://stackoverflow.com/questions