我正在用ASP.net (VS2015 C #)开发一个网站。
我需要导出一个包含大量数据(500000+行和100列)的MySql
表格到excel (xlsx
),格式。
在尝试了许多选项之后,NPOI (v3.20)库允许使用使用流的类型(SXSSFWorkbook
和SXSSFSheet
)进行这种导出。
如果我使用XSSFWorkbook
,我会在填充各行时出现内存不足的情况。
使用SXSSFWorkbook
,我可以用不同的字体和颜色格式化xlsx
,但我在导出的数据类型方面遇到了问题:
我用来格式化数据的代码是:
SXSSFWorkbook wb = NPOI.SS.UserModel.IndexedColors.RoyalBlue.Index;SXSSFWorkbook();SXSSFSheet sh =(SXSSFSheet)wb.CreateSheet(“ 1");sh.SetRandomAccessWindowSize(100);ICellStyle testStyleHeader = wb.CreateCellStyle();ICellStyle testStyleRow = wb.CreateCellStyle();// Header style testStyleHeader.FillForegroundColor = new testStyleHeader.FillPattern = FillPattern.SolidForeground;// Double Style (带两个小数,如453.65) ICellStyle cellStyleDouble =();=()(”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单元格= row.CreateCell(j);//填充标题行cell.SetCellValue("XXXX");cell.CellStyle = testeStyleHeader;j++;cell = row.CreateCell(j);(“YYYY”);=;;单元格= row.CreateCell(j);cell.SetCellValue("ZZZZ");cell.CellStyle = testeStyleHeader;j++;单元格= row.CreateCell(j);cell.SetCellValue("WWWW");cell.CellStyle = testeStyleHeader;j++;单元格= row.CreateCell(j);//填充行int i= 1;//行号IRow row2;//无标题行ICell cell2;//无标题单元格while (dr.Read()) // dr是DataReader { row2 = sh.CreateRow(i);for (int cont = 0;cont < NumColumns;cont++) { if (cont == 0) //此列是日期{…。//日期格式的代码} else if (cont == 3) //两个小数的双精度列…!(取值样本100.45、5654.80等){ ICell cell3 = row2.CreateCell(j,NPOI.SS.UserModel.CellType.Numeric);cell3.CellStyle = cellStyleDouble;cell3.SetCellValue(Convert.ToDouble(drcont));} else {…。// tex格式、int格式等代码}} i++;
}
使用这段代码,在十进制列(cont ==3)中,我得到了一个文本列。
但是,使用相同的代码,如果我声明无流类型:
XSSFWorkbook wb = XSSFWorkbook();XSSFSheet sh =(SSFSheet)wb.CreateSheet(“工作表1");
只有通过这些更改,我才能得到一个完美的数字列3……
对于此行:
cellStyleDouble.DataFormat = wb.CreateDataFormat().GetFormat("0.00");
我尝试过不同的类型:
在某些情况下,我会得到一个数字,但不是所需的格式。
So...streaming类型不允许此格式吗?
发布于 2018-06-02 03:03:06
只需将文化信息更改为en-US即可
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-Us");
ISheet worksheet = Workbook.CreateSheet(dt.TableName);
IRow HeaderRow = worksheet.CreateRow(0);
for (int i = 0; i < dt.Columns.Count; i++)
{
ICell HeaderCell = HeaderRow.CreateCell(i);
HeaderCell.SetCellValue(dt.Columns[i].ColumnName);
}
for (int j = 0; j < dt.Rows.Count; j++)
{
IRow Row = worksheet.CreateRow(j + 1);
for (int i = 0; i < dt.Columns.Count; i++)
{
ICell Cell = Row.CreateCell(i);
if (dt.Columns[i].DataType.IsOfType(typeof(decimal)) && dt.Rows[j][i] != DBNull.Value)
{
Cell.SetCellType(CellType.Numeric);
Cell.SetCellValue((double)dt.Rows[j][i]);
}
else
Cell.SetCellValue(dt.Rows[j][i].ToString());
}
}
Thread.CurrentThread.CurrentCulture = new CultureInfo("pt-Br");
这对我很有效!
发布于 2017-11-03 00:48:25
您可以尝试基于以下代码片段的格式。我正在使用这种方法来格式化电话号码。
XSSFCellStyle currencyCellStyle = (XSSFCellStyle)workbook.CreateCellStyle();
XSSFDataFormat currencyDataFormat = (XSSFDataFormat)workbook.CreateDataFormat();
currrencyCellStyle.SetDataFormat(currencyDataFormat.GetFormat("00000.00")); //Formats: #####.##, 00000##.##
有时在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
https://stackoverflow.com/questions/47063178
复制相似问题