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

在C#中使用NPOI设置单元格的黑色边框,你可以通过`BorderStyle`枚举和`Border`对象来完成。以下是在HSSF和EPPlus库中分别设置黑色边框的方式: ### 方式一:HSSF(低级API) ```csharp using org.apache.poi.hssf.usermodel; // 创建单元格 HSSFCell cell = row.createCell(cellIndex); // 设置边框 HSSFCellStyle style = workbook.CreateCellStyle(); style.BorderTop = HSSFCellStyle.BorderStyle.THIN; // 设置顶部边框 style.BorderBottom = HSSFCellStyle.BorderStyle.THIN; // 设置底部边框 style.BorderLeft = HSSFCellStyle.BorderStyle.THIN; // 左侧边框 style.BorderRight = HSSFCellStyle.BorderStyle.THIN; // 右侧边框 style.BorderSide = BorderStyle.BorderSide.All; // 设置所有边框 style.BorderColor = HSSFColor.BLACK.Index; // 设置边框颜色为黑色 cell.CellStyle = style; ``` ### 方式二:EPPlus(高级API) ```csharp using OfficeOpenXml; // 创建单元格范围 var cellRange = worksheet.Cells[cellAddress]; // 设置边框 cellRange.Border = ExcelBorderStyle.Thin; // 设置所有边框为细线 cellRange.LeftBorder.Color = System.Drawing.Color.Black; // 设置左侧边框为黑色 cellRange.RightBorder.Color = System.Drawing.Color.Black; // 设置右侧边框为黑色 cellRange.TopBorder.Color = System.Drawing.Color.Black; // 设置上部边框为黑色 cellRange.BottomBorder.Color = System.Drawing.Color.Black; // 设置下部边框为黑色 ``` ### 方式三:XSSF(基于XML的低级API) ```csharp using NPOI.XSSF用户模型; // 创建单元格 IXSSFCell cell = (IXSSFCell)row.CreateCell(cellIndex); // 创建样式并设置边框 IXSSFCellStyle style = (IXSSFCellStyle)workbook.CreateCellStyle(); style.BorderTop = NPOI.SS.UserModel.BorderStyleThin; // 设置顶部边框 style.BorderBottom = NPOI.SS.UserModel.BorderStyleThin; // 设置底部边框 style.BorderLeft = NPOI.SS.UserModel.BorderStyleThin; // 设置左侧边框 style.BorderRight = NPOI.SS.UserModel.BorderStyleThin; // 设置右侧边框 style.BorderBottomColor = NPOIColor.Black; // 设置边框颜色为黑色 cell.CellStyle = style; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值