zoukankan      html  css  js  c++  java
  • NPOI导出EXCEL部分样式不起作用

    在使用NPOI导出excel的时候,设置cell样式,数据量多余6条之后,在后面几条数据没有样式(边框,对其,换行等)。

    原因是设置CellStyle的时候把CreateCellStyle放在循环列集合里边,原版代码有问题的代码

    HSSFRow dataRow = (HSSFRow)sheet.CreateRow(rowIndex);
    foreach (DataColumn column in dtSource.Columns)
    {
        HSSFCell newCell = (HSSFCell)dataRow.CreateCell(column.Ordinal);
        string drValue = row[column].ToString();
        switch (column.DataType.ToString())
        {
            case "System.String":
                {
                    HSSFCellStyle strStyle = (HSSFCellStyle)workbook.CreateCellStyle();
                    strStyle.BorderBottom = BorderStyle.Thin;
                    strStyle.BorderTop = BorderStyle.Thin;
                    strStyle.BorderLeft = BorderStyle.Thin;
                    strStyle.BorderRight = BorderStyle.Thin;
                    //strStyle.VerticalAlignment = VerticalAlignment.Center;
                    strStyle.Alignment = HorizontalAlignment.Left;
                    switch (detail.Format)
                    {
                        case ExportFormat.Normal:
                            //strStyle.Alignment = HorizontalAlignment.Center;
                            strStyle.Alignment = HorizontalAlignment.Left;
                            break;
                        case ExportFormat.Formatted:
                            strStyle.WrapText = true;
                            break;
                    }
                    newCell.SetCellValue(drValue);
                    newCell.CellStyle = strStyle;
                    break;
                }
        }
    }
    

     解决问题,需要将该样式提取到foreach外面就可以解决了

    HSSFRow dataRow = (HSSFRow)sheet.CreateRow(rowIndex);
    HSSFCellStyle strStyle = (HSSFCellStyle)workbook.CreateCellStyle();
    strStyle.BorderBottom = BorderStyle.Thin;
    strStyle.BorderTop = BorderStyle.Thin;
    strStyle.BorderLeft = BorderStyle.Thin;
    strStyle.BorderRight = BorderStyle.Thin;
    foreach (DataColumn column in dtSource.Columns)
    {
        HSSFCell newCell = (HSSFCell)dataRow.CreateCell(column.Ordinal);
        string drValue = row[column].ToString();
        switch (column.DataType.ToString())
        {
            case "System.String":
                {
                    //strStyle.VerticalAlignment = VerticalAlignment.Center;
                    strStyle.Alignment = HorizontalAlignment.Left;
                    switch (detail.Format)
                    {
                        case ExportFormat.Normal:
                            //strStyle.Alignment = HorizontalAlignment.Center;
                            strStyle.Alignment = HorizontalAlignment.Left;
                            break;
                        case ExportFormat.Formatted:
                            strStyle.WrapText = true;
                            break;
                    }
                    newCell.SetCellValue(drValue);
                    newCell.CellStyle = strStyle;
                    break;
                }
        }
    }
    

      

  • 相关阅读:
    DBMS_SCHEDULER 的使用
    Android 鲜为人知的 8 个小秘密
    你正在使用的移动电话已经 40 岁
    HDU1056:HangOver
    Firefox OS 源码泄露!!!
    上网本 硬盘安装linux 最揪心的回忆
    103 Stacking Boxes
    ip2long之后有什么好处?
    mysql怎么创建,删除,查看索引?
    用mysql查询某字段是否有索引
  • 原文地址:https://www.cnblogs.com/zbfamily/p/9408604.html
Copyright © 2011-2022 走看看