NPOI setting different cell format(NPOI设置不同的单元格格式)
本文介绍了NPOI设置不同的单元格格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在设置每个单元格的不同格式时遇到问题,我想将数字格式设置为千分隔符和带3位小数的千分隔符,当数字不是整数时,这是我的代码,我认为问题看起来像是FOR循环中的最后一个格式设置 因此,输出应如下所示 12345->12345;425->425;41,2->41,2;4578,25->4578,25
short doubleFormat = hssfOutputWorkBook.CreateDataFormat().GetFormat("#,##0.###");
short intFormat = hssfOutputWorkBook.CreateDataFormat().GetFormat("#,##0");
for (i = 0; i <= unorderedSheet.LastRowNum; i++)
{
NPOI.SS.UserModel.IRow newRow = orderedSheet.CreateRow(i);
NPOI.SS.UserModel.IRow oldRow = unorderedSheet.GetRow(i);
if (oldRow != null)
{
foreach (ICell oldCell in oldRow.Cells)
{
ICell newCell = newRow.CreateCell(mapping[n]);
switch (oldCell.CellType)
{
case CellType.NUMERIC:
newCell.SetCellType(CellType.NUMERIC);
newCell.SetCellValue(oldCell.NumericCellValue);
if (numberHasDecimals(oldCell.NumericCellValue))
{
newCell.CellStyle.DataFormat = doubleFormat;
}
else
{
newCell.CellStyle.DataFormat = intFormat;
}
break;
default:
newCell.SetCellValue(oldCell.ToString());
break;
}}}}
推荐答案
static ICellStyle _doubleCellStyle = null;
static ICellStyle _intCellStyle = null;
static void SettingDifferentCellFormat( HSSFWorkbook hssfOutputWorkBook, ISheet orderedSheet, ISheet unorderedSheet ) {
//short doubleFormat = hssfOutputWorkBook.CreateDataFormat().GetFormat( "#,##0.###" );
//short intFormat = hssfOutputWorkBook.CreateDataFormat().GetFormat( "#,##0" );
if ( _doubleCellStyle == null ) {
_doubleCellStyle = hssfOutputWorkBook.CreateCellStyle();
_doubleCellStyle.DataFormat = hssfOutputWorkBook.CreateDataFormat().GetFormat( "#,##0.###" );
}
if ( _intCellStyle == null ) {
_intCellStyle = hssfOutputWorkBook.CreateCellStyle();
_intCellStyle.DataFormat = hssfOutputWorkBook.CreateDataFormat().GetFormat( "#,##0" );
}
for ( int i = 0; i <= unorderedSheet.LastRowNum; i++ ) {
NPOI.SS.UserModel.IRow newRow = orderedSheet.CreateRow( i );
NPOI.SS.UserModel.IRow oldRow = unorderedSheet.GetRow( i );
const int mapping_n_ = 0;
if ( oldRow != null ) {
foreach ( ICell oldCell in oldRow.Cells ) {
ICell newCell = newRow.CreateCell( mapping_n_ );
bool numberHasDecimals = true;
switch ( oldCell.CellType ) {
case CellType.NUMERIC:
newCell.SetCellType( CellType.NUMERIC );
newCell.SetCellValue( oldCell.NumericCellValue );
if ( numberHasDecimals ) {
//newCell.CellStyle.DataFormat = doubleFormat;
newCell.CellStyle = _doubleCellStyle;
} else {
//newCell.CellStyle.DataFormat = intFormat;
newCell.CellStyle = _intCellStyle;
}
break;
default:
newCell.SetCellValue( oldCell.ToString() );
break;
}
}
}
}
}//SettingDifferentCellFormat
这篇关于NPOI设置不同的单元格格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:NPOI设置不同的单元格格式


基础教程推荐
猜你喜欢
- 全局 ASAX - 获取服务器名称 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01