npoi

Npoi简单读写Excel

匿名 (未验证) 提交于 2019-12-02 22:06:11
什么是NPOI ?   简而言之,NPOI就是可以在没有Office的情况下对Word或Excel文档进行读写等操作。 使用方式 :    1、准备NPOI的dll文件     下载链接: https://npoi.codeplex.com/releases   2、将下载的dll文件引入项目中   3、引用命名空间 须知:   1、Excel表格分为:WorkBook(工作薄)-> Sheet(工作表) -> 行:Row 单元格:Cell。   2、NPOI是POI的C#版本,NPOI的行和列的index都是从0开始 代码如下: 1     /// <summary> 2 /// 将DataTable导入到Excel 3 /// </summary> 4 /// <param name="data">要导入的数据</param> 5 /// <param name="filepath">导入的文件路径(包含文件名称)</param> 6 /// <param name="sheename">要导入的表名</param> 7 /// <param name="iscolumwrite">是否写入列名</param> 8 /// <returns>导入Excel的行数</returns> 9 public int DataTableToExcel(DataTable data,

NPOI操作创建Excel

匿名 (未验证) 提交于 2019-12-02 22:06:11
  一、下载NPOI类库     使用Nuget在线搜索NPOI,下载安装   二、代码开撸      var workBook = new HSSFWorkbook(); #region 设置样式 IFont font = workBook.CreateFont(); font.FontHeightInPoints = 11; font.FontName = "宋体"; font.Boldweight = (short)FontBoldWeight.Bold; font.Color = HSSFColor.White.Index; HSSFPalette palette = workBook.GetCustomPalette(); //调色板实例 palette.SetColorAtIndex((short)8, (byte)91, (byte)155, (byte)213);//设置表头背景色 palette.SetColorAtIndex((short)16, (byte)221, (byte)235, (byte)247);//设置内容背景色 palette.SetColorAtIndex((short)32, (byte)155, (byte)194, (byte)230);//设置下边框线颜色 palette.SetColorAtIndex((short)48,

NPOI读取Excel文件

匿名 (未验证) 提交于 2019-12-02 22:06:11
1 public class ExcelOperator 2 { 3 public static List<DataTable> Read(string fileName) 4 { 5 List<DataTable> tables = new List<DataTable>(); 6 if (!File.Exists(fileName)) 7 return tables; 8 var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); 9 IWorkbook workbook = null; 10 if (fileName.IndexOf(".xlsx") > 0) 11 { 12 // 2007版本 13 workbook = new XSSFWorkbook(fs); 14 } 15 else if (fileName.IndexOf(".xls") > 0) 16 { 17 // 2003版本 18 workbook = new HSSFWorkbook(fs); 19 } 20 21 var workbooks = workbook.GetEnumerator(); 22 while (workbooks.MoveNext()) 23 { 24 ISheet sheet = workbooks

NET npoi 保存文件

匿名 (未验证) 提交于 2019-12-02 22:06:11
npoi完整代码: NET npoi帮助类 public static void DataTableToExcel(List<DataTable> dataTables, string filePath) { if (dataTables == null || !dataTables.Any()) throw new Exception("dataTables不能为null"); bool isOldThan2007 = Path.GetExtension(filePath)?.ToLower() == ".xls"; IWorkbook book = dataTables.ToWorkbook(isOldThan2007); using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { book.Write(fs); } } public static IWorkbook ToWorkbook(this List<DataTable> dataTables, bool isOldThan2007) { IWorkbook book = isOldThan2007 ? new HSSFWorkbook() : (IWorkbook)new XSSFWorkbook();

How to download memory stream object via angularJS and webaAPI2

主宰稳场 提交于 2019-12-02 10:22:13
问题 I have a project where I am using NPOI to generate Excel document from my Angular application. I have the calls being made from my angular service to my webapi controller as follows: function exportReportToExcel(report) { return $http.post('reportlibrary/exportReport/', report, { }).then(function (response) { return response.data; }); }; Within the controller I make the following call [HttpPost] public HttpResponseMessage ExportReport([FromBody]DTOs.Report report) { try {

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

瘦欲@ 提交于 2019-12-02 07:33:10
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(); _TextCellStyle.DataFormat = workbook.CreateDataFormat().GetFormat("@"); IRow file = worksheet.CreateRow(iRow)

How to download memory stream object via angularJS and webaAPI2

感情迁移 提交于 2019-12-02 03:53:20
I have a project where I am using NPOI to generate Excel document from my Angular application. I have the calls being made from my angular service to my webapi controller as follows: function exportReportToExcel(report) { return $http.post('reportlibrary/exportReport/', report, { }).then(function (response) { return response.data; }); }; Within the controller I make the following call [HttpPost] public HttpResponseMessage ExportReport([FromBody]DTOs.Report report) { try { IReportPersistenceManager manager = ContainerConfigurator.Instance.Resolve<IReportPersistenceManager>(); MemoryStream ms =

Convert xlsx file to xls using NPOI in c#

孤者浪人 提交于 2019-12-01 10:53:48
I've a excel file in *.xlsx format. I want convert this file into *.xls format in my MVC Web application. In my application hosted server there is no Microsoft.Office package. Please let me know how we can achieve it in NPOI with c#. Thanks in advance. I found the solution based on the Andy's suggestion. Here is the source code which is customized to convert XLSX to XLS. public static class ConvertXLSXToXLS { public static HSSFWorkbook ConvertWorkbookXSSFToHSSF(XSSFWorkbook source) { //Install-Package NPOI -Version 2.0.6 HSSFWorkbook retVal = new HSSFWorkbook(); for (int i = 0; i < source

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

偶尔善良 提交于 2019-11-30 11:21:01
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. Try the GetCell method with the MissingCellPolicy : ICell cell = row.GetCell(2, MissingCellPolicy.RETURN_NULL_AND_BLANK); 来源: https://stackoverflow.com/questions/17475359/how-to-use-npoi-to-read-excel-spreadsheet-that-contains-empty-cells

NPOI SetCellValue method doesn't write data to cells

送分小仙女□ 提交于 2019-11-30 09:57:13
问题 I have some problem with writing data to Excel worksheet cells using NPOI . Here is my code: FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read); hssfworkbook = new HSSFWorkbook(file); ISheet sheet1 = hssfworkbook.GetSheet("Табель"); for (int i = 0; i < table.Tables[0].Rows.Count; i++) { for (int j = 0; j < table.Tables[0].Columns.Count; j++) { if (table.Tables[0].Rows[i][j].ToString() != "0") sheet1.GetRow(i+5).GetCell(j).SetCellValue(table.Tables[0].Rows[i][j].ToString())