我有一个使用NPOI库生成Excel文件的功能。女巫在大多数情况下工作正常只是风格问题他们似乎功能狡猾。目前我的重点是数字格式:
我希望所有小数都显示为:例如12.156.235,33
根据google上的研究,应该这样做的格式应该是:"#,## 0.00"但值显示为:12156235,33,单元格类型设置为常规而不是数字
看起来很多人都有这方面的问题。希望有一个解决方案可以解决我想要做的其他事情,我使用NPOI库进行管理,现在不打算切换。
程序:
IDataFormat dataFormat;
public string GenerateExcel(ExcelData data)
{
var dateFormat = dataFormat.GetFormat("dd.MM.yyyy HH:mm:ss");
var decimalFormat = dataFormat.GetFormat("#,##0.00");
string xlsPath, xlsFile, xlsName;
xlsPath = Environment.GetEnvironmentVariable("TEMP");
xlsName = Path.GetRandomFileName().Replace(".", ""); //DateTime.Now.ToString("yyyy-MM-dd");
xlsName += ".xlsx";
xlsFile = Path.Combine(xlsPath, xlsName);
if (File.Exists(xlsFile))
File.Delete(xlsFile);
workbook = new XSSFWorkbook();
dataFormat = workbook.CreateDataFormat();
XSSFCellStyle style = (XSSFCellStyle)workbook.CreateCellStyle();
NPOI.SS.UserModel.ICell cell;
Dictionary<int, int> maxColSize = new Dictionary<int, int>();
string title;
int rr;
foreach (var sheet in data.Sheets)
{
maxColSize.Clear();
if(sheet.Hidden != null)
foreach (var h in sheet.Hidden)
{
sheet.HideRow(h);
}
worksheet = (XSSFSheet)workbook.CreateSheet(sheet.Title);
rr = 0;
if (sheet.Titles!=null && sheet.Titles.Length > 0)
{
worksheet.CreateRow(rr);
rr++;
for (int t = 0; t < sheet.Titles.Length; t++)
{
title = (string)sheet.Titles[t].Title;
maxColSize.Add(t, title.Length);
cell = worksheet.GetRow(0).CreateCell(t, NPOI.SS.UserModel.CellType.String);
cell.SetCellValue(title);
if (sheet.Titles[t].Style != null)
cell.CellStyle = GetCellStyle(sheet.Titles[t].Style);
}
}
int cols = 0;
int cc = 0;
IRow row = null;
for (int r = 0; r < sheet.Rows.Length; r++)
{
if (sheet.Rows[r].row > -1)
{
rr = sheet.Rows[r].row;
row = worksheet.GetRow(sheet.Rows[r].row);
}
else
{
//rr = r;
row = worksheet.GetRow(rr);
}
if (row == null)
{
row = worksheet.CreateRow(rr);
}
if(sheet.Rows[r].Cells.Length > cols)
cols = sheet.Rows[r].Cells.Length;
for (int c = 0; c < sheet.Rows[r].Cells.Length;c++ )
{
cc = c;
if (sheet.Rows[r].Cells[c].column > -1)
{
cc = sheet.Rows[r].Cells[c].column;
}
if (!maxColSize.Keys.Contains(cc))
maxColSize.Add(cc, 0);
switch (sheet.Rows[r].Cells[c].DataType)
{
case "DateTime":
cell = row.CreateCell(cc, NPOI.SS.UserModel.CellType.Numeric);
cell.SetCellValue((DateTime)sheet.Rows[r].Cells[c].Value);
cell.CellStyle.DataFormat = dateFormat;
if (maxColSize[cc] < cell.NumericCellValue.ToString().Length)
maxColSize[cc] = cell.NumericCellValue.ToString().Length;
break;
case "Numeric":
case "Decimal":
cell = row.CreateCell(cc, NPOI.SS.UserModel.CellType.Numeric);
cell.SetCellValue(Convert.ToDouble(sheet.Rows[r].Cells[c].Value)); //sheet.Rows[r].Cells[c].Value
cell.CellStyle.DataFormat = decimalFormat;
if (maxColSize[cc] < cell.NumericCellValue.ToString().Length)
maxColSize[cc] = cell.NumericCellValue.ToString().Length;
break;
case "Integer":
cell = row.CreateCell(cc, NPOI.SS.UserModel.CellType.Numeric);
cell.SetCellValue(Convert.ToInt32(sheet.Rows[r].Cells[c].Value));
cell.CellStyle.DataFormat = dataFormat.GetFormat("0");
if (maxColSize[cc] < cell.NumericCellValue.ToString().Length)
maxColSize[cc] = cell.NumericCellValue.ToString().Length;
break;
case "Date":
cell = row.CreateCell(cc, NPOI.SS.UserModel.CellType.Numeric);
cell.SetCellValue((DateTime)sheet.Rows[r].Cells[c].Value);
cell.CellStyle.DataFormat = dataFormat.GetFormat("dd.MM.yyyy");
if (maxColSize[cc] < cell.NumericCellValue.ToString().Length)
maxColSize[cc] = cell.NumericCellValue.ToString().Length;
break;
case "Boolean":
cell = row.CreateCell(cc, NPOI.SS.UserModel.CellType.Boolean);
cell.SetCellValue(Convert.ToBoolean(sheet.Rows[r].Cells[c].Value));
if (maxColSize[cc] < cell.BooleanCellValue.ToString().Length)
maxColSize[cc] = cell.StringCellValue.ToString().Length;
break;
case "Formula":
cell = row.CreateCell(cc, NPOI.SS.UserModel.CellType.Formula);
cell.SetCellFormula((string)(sheet.Rows[r].Cells[c].Value));
if (maxColSize[cc] < cell.BooleanCellValue.ToString().Length)
maxColSize[cc] = cell.StringCellValue.ToString().Length;
break;
default:
cell = row.CreateCell(cc, NPOI.SS.UserModel.CellType.String);
cell.SetCellValue((string)sheet.Rows[r].Cells[c].Value);
if (maxColSize[cc] < cell.StringCellValue.Length)
maxColSize[cc] = cell.StringCellValue.Length;
break;
}
if (sheet.Rows[r].Cells[c].Style != null)
{
var st = GetCellStyle(sheet.Rows[r].Cells[c].Style);
cell.CellStyle = st;
}
}
if (sheet.Rows[r].row == -1)
rr++;
if(sheet.Merge != null)
foreach (var merge in sheet.Merge)
{
worksheet.AddMergedRegion( new NPOI.SS.Util.CellRangeAddress(merge.FirstRow,merge.LastRow, merge.FirstColumn, merge.LastColumn));
}
}
foreach (var col in maxColSize)
{
worksheet.SetColumnWidth(col.Key, ((int)(col.Value * 1.14388)) * 256);
}
XSSFFormulaEvaluator.EvaluateAllFormulaCells(workbook);
}
using (var fs = new FileStream(xlsFile, FileMode.Create, FileAccess.Write))
{
workbook.Write(fs);
}
return xlsFile;
}