NPOI:使用SXSSFWorkbook格式化为十进制的Excel列?
我正在用ASP.net(VS2015 C#)开发一个网站。
我需要使用格式将大量数据(500000行和100列)的MySql
表导出为excel( xlsx
)。
在尝试了很多选项之后,NPOI(v 3.20)库允许使用使用stream式传输的types( SXSSFWorkbook
& SXSSFSheet
)进行SXSSFSheet
。
如果我使用XSSFWorkbook
我得到和内存填满行。
使用SXSSFWorkbook
我已经能够使用不同的字体和颜色来格式化xlsx
,但是我遇到了导出的数据types的问题:
- datetypes确定
- Inttypes确定
- 文字确定
- 小数input像100.35 – >问题,我得到一个文本列。 我需要一个100,35的输出。
我用来格式化数据的代码是:
SXSSFWorkbook wb = new SXSSFWorkbook(); SXSSFSheet sh = (SXSSFSheet)wb.CreateSheet("Sheet 1"); sh.SetRandomAccessWindowSize(100); ICellStyle testStyleHeader = wb.CreateCellStyle(); ICellStyle testStyleRow = wb.CreateCellStyle(); // Header Style testStyleHeader.FillForegroundColor = NPOI.SS.UserModel.IndexedColors.RoyalBlue.Index; testStyleHeader.FillPattern = FillPattern.SolidForeground; // Double style (with 2 decimals like 453.65) ICellStyle cellStyleDouble = wb.CreateCellStyle(); cellStyleDouble.DataFormat = wb.CreateDataFormat().GetFormat("0.00"); // Font XSSFFont hFontBlack = (XSSFFont)wb.CreateFont(); hFontBlack.FontHeightInPoints = 11; hFontBlack.FontName = "Calibri"; hFontBlack.Color = NPOI.SS.UserModel.IndexedColors.Black.Index; testStyleHeader.SetFont(hFontBlack); IRow row = sh.CreateRow(0); int j = 0; ICell cell = row.CreateCell(j); // Fill Header row cell.SetCellValue("XXXX"); cell.CellStyle = testeStyleHeader; j++; cell = row.CreateCell(j); cell.SetCellValue("YYYY"); cell.CellStyle = testeStyleHeader; j++; cell = row.CreateCell(j); cell.SetCellValue("ZZZZ"); cell.CellStyle = testeStyleHeader; j++; cell = row.CreateCell(j); cell.SetCellValue("WWWW"); cell.CellStyle = testeStyleHeader; j++; cell = row.CreateCell(j); // Fill Rows int i = 1; // row Number IRow row2; // No Header Row ICell cell2; // No Header cell while (dr.Read()) // dr is the DataReader { row2 = sh.CreateRow(i); for (int cont = 0; cont < NumColumns; cont++) { if (cont == 0) // This column is a date { …. // code for date format } else if (cont == 3) // Double column with 2 decimals¡! (values samples 100.45 5654.80 etc.) { ICell cell3 = row2.CreateCell(j, NPOI.SS.UserModel.CellType.Numeric); cell3.CellStyle = cellStyleDouble; cell3.SetCellValue(Convert.ToDouble(dr[cont])); } else {…. // code for tex format, int format etc. } } i++;
}
用这个代码,在十进制列(cont == 3)中,我得到一个文本列。
但是,使用相同的代码,如果我声明没有streamtypes:
XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sh = (SSFSheet)wb.CreateSheet("Sheet 1");
只有这个变化,我得到一个完美的数字列3 …
对于这一行:
cellStyleDouble.DataFormat = wb.CreateDataFormat()。GetFormat(“0.00”);
我曾尝试过不同的types:
- “##”
- “#,## 0.000”
- “###”
- 等等…
在某些情况下,我得到一个数字,但不是所需的格式。
所以…streamtypes不允许这种格式?
你可以尝试基于下面的代码片段的格式。 我正在使用这种方法来格式化电话号码。
XSSFCellStyle currencyCellStyle = (XSSFCellStyle)workbook.CreateCellStyle(); XSSFDataFormat currencyDataFormat = (XSSFDataFormat)workbook.CreateDataFormat(); currrencyCellStyle.SetDataFormat(currencyDataFormat.GetFormat("00000.00")); //Formats: #####.##, 00000##.##
有时很难find确切的格式在NPOI :)。 请尝试下面的方法
ICellStyle CellStyle = workbook.CreateCellStyle(); CellStyle.DataFormat = workbook.CreateDataFormat().GetFormat("number"); // or Number
要么
CellStyle cellStyle = wb.createCellStyle(); cellStyle.setDataFormat(wb.getCreationHelper().createDataFormat().getFormat("#.#")); // or #####.## or number