首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NPOI DataFormat

NPOI DataFormat
EN

Stack Overflow用户
提问于 2010-08-30 19:22:25
回答 1查看 18.4K关注 0票数 7

我正在导出一个带有NPOIv1.2.3的网格,并且无法使单元格格式正常工作。

我有一个类,它将对象列表导出到XLS文件中。为每个对象创建一行,为每个已配置的属性添加一个单元格。单元格数据格式可以设置在每个属性级别上。

我读过那个you shouldn't create a new style for each cell。我不能硬编码我的风格,因为我的出口商需要支持任何类。相反,我编写了一个小的缓存系统,它只在还没有为当前单元格的格式创建一个新的CellStyle时创建一个新的缓存系统。

不幸的是,这仍然没有解决问题。格式不正确地应用于最终的XLS文件中。在我的测试用例中,XLS中的大多数单元格都使用“日期”格式,尽管只有少数列是日期。但是,第一列正确地使用了自定义格式。没有任何单元格设置为文本,尽管这应该是其中的大多数。

我做错了什么?

下面的"AddRecords“方法用于添加数据行(页眉和页脚行分别添加)。最后一段代码是延迟加载CellStyles的方法。

代码语言:javascript
复制
private void AddRecords( Sheet sheet, IList<T> records )
{
    foreach( var record in records )
    {
        // append row
        var row = sheet.CreateRow ( sheet.LastRowNum + 1 );

        // iterate through all configured columns
        foreach ( var column in GetColumns() )
        {
            // append cell
            Cell cell = row.CreateCell ( row.LastCellNum == -1 ? 0 : row.LastCellNum );

            // get the property value of the column from the record
            object value = GetCellValue ( column, record );

            // extension method that takes an object value and calls the appropriate type-specific SetCellValue overload
            cell.SetCellValue ( value );

            // get format from the column definition ("m/d", "##.###", etc.), or use the default
            string dataFormat = column.DataFormat ?? GetDefaultDataFormat ( value );

            // find/create cell style
            cell.CellStyle = GetCellStyleForFormat( sheet.Workbook, dataFormat );
        }
    }
}

/// <summary>
/// Returns a default format string based on the object type of value.
///
/// http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/BuiltinFormats.html
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private string GetDefaultDataFormat( object value )
{
    if( value == null )
    {
        return "General";
    }

    if( value is DateTime )
    {
        return "m/d";
    }

    if( value is bool )
    {
        return "[=0]\"Yes\";[=1]\"No\"";
    }

    if( value is byte || value is ushort || value is short ||
         value is uint || value is int || value is ulong || value is long )
    {
        return "0";
    }

    if( value is float || value is double )
    {
        return "0.00";
    }

    // strings and anything else should be text
    return "text";
}

private readonly Dictionary<string, CellStyle> _cellStyleCache = new Dictionary < string, CellStyle > ();

private CellStyle GetCellStyleForFormat( Workbook workbook, string dataFormat )
{
    if( !_cellStyleCache.ContainsKey ( dataFormat ) )
    {
        var newDataFormat = workbook.CreateDataFormat ();
        var style = workbook.CreateCellStyle ();
        style.DataFormat = newDataFormat.GetFormat ( dataFormat );

        _cellStyleCache[dataFormat] = style;
    }

    return _cellStyleCache[dataFormat];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-08-30 19:54:53

看起来这个问题与创建与内置格式相匹配的新格式有关。我改变了我的延迟加载方法,如果可用的话使用内置格式,并且我的最后一个XLS中的单元格格式现在都是正确的。

代码语言:javascript
复制
if( !_cellStyleCache.ContainsKey ( dataFormat ) )
{
    var style = workbook.CreateCellStyle ();

    // check if this is a built-in format
    var builtinFormatId = HSSFDataFormat.GetBuiltinFormat ( dataFormat );

    if( builtinFormatId != - 1)
    {
        style.DataFormat = builtinFormatId;
    }
    else
    {
        // not a built-in format, so create a new one
        var newDataFormat = workbook.CreateDataFormat ();
        style.DataFormat = newDataFormat.GetFormat ( dataFormat );
    }

    _cellStyleCache[dataFormat] = style;
}
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3603516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档