NPOI CellStyle 设置

    public class ExcelNPOIUnit
    {
        public static void SetCell(IWorkbook workbook, ISheet sheet,
             IRow row, int createCellIndex, object cellContent, CellType cellType, HorizontalAlignment alignment)
        {
            IDataFormat celldataformat = workbook.CreateDataFormat();
            IFont font = workbook.CreateFont();
            font.FontName = "Calibri";

            ICell cell = row.FirstOrDefault(n => n.ColumnIndex == createCellIndex);
            if (cell == null)
                cell = row.CreateCell(createCellIndex);

            cell.CellStyle.SetFont(font);
            cell.CellStyle.BorderLeft = BorderStyle.Thin;
            cell.CellStyle.BorderRight = BorderStyle.Thin;
            cell.CellStyle.BorderTop = BorderStyle.Thin;
            cell.CellStyle.BorderBottom = BorderStyle.Thin;

            //在这里设置会影响全部单元格
            //cell.CellStyle.Alignment = alignment;

            double tmp = -1;
            if (cellType == CellType.Numeric && double.TryParse(cellContent.ToString(), out tmp))
            {
                //必须在这里这样设置,才能对当前单元格有效
                ICellStyle cellstyle = workbook.CreateCellStyle();
                cellstyle.CloneStyleFrom(cell.CellStyle);
                cellstyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,##0");
                cellstyle.Alignment = alignment;
                cell.CellStyle = cellstyle;

                cell.SetCellValue(tmp);
            }
            else
            {
                //必须在这里这样设置,才能对当前单元格有效
                ICellStyle cellstyle = workbook.CreateCellStyle();
                cellstyle.CloneStyleFrom(cell.CellStyle);
                cellstyle.Alignment = alignment;
                cell.CellStyle = cellstyle;
                cell.SetCellValue(cellContent.ToString());
            }
        }

        public static void SaveSheet(string fullname, ISheet sheet)
        {
            using (FileStream writefile = new FileStream(fullname, FileMode.Create, FileAccess.Write))
            {
                sheet.Workbook.Write(writefile);
            }
        }
        public static IWorkbook GetWorkbook(string fullname)
        {
            IWorkbook workbook = null;

            if (fullname.ToLower().EndsWith(".xls"))
            {
                using (FileStream fs = new FileStream(fullname, FileMode.Open, FileAccess.Read))
                {
                    workbook = new HSSFWorkbook(fs);
                }
            }
            else
            {
                using (FileStream fs = new FileStream(fullname, FileMode.Open, FileAccess.Read))
                {
                    workbook = new XSSFWorkbook(fs);
                }
            }

            return workbook;
        }

    }

var wk = ExcelNPOIUnit.GetWorkbook(logname);

var sheet = wk.GetSheet("Sheet1");

int rowindex = 2;
int cellindex = 0;
foreach (var item in list)
{
IRow row = sheet.CreateRow(rowindex);

ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.CreateDate, CellType.String, HorizontalAlignment.Center);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.TotalFilesReceived, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.TotalPagesReceived, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.ShippedFiles, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.ShippedPages, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.PendingFiles, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.PendingPages, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.NofFiles_Priority, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.NoofPgs_Priority, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.NoofFiles_NoPriority, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.NoofPgs_NoPriority, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.NoofFiles_Today, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.NoofPgs_Today, CellType.Numeric, HorizontalAlignment.Right);
ExcelNPOIUnit.SetCell(wk, sheet, row, cellindex++, item.Remarks, CellType.Numeric, HorizontalAlignment.Left);
rowindex++;
cellindex = 0;
}

ExcelNPOIUnit.SaveSheet(logname, sheet);

 

转载于:https://www.cnblogs.com/yipeng-yu/p/8392256.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值