其他分享
首页 > 其他分享> > NPOI 用法

NPOI 用法

作者:互联网

            HSSFWorkbook workbook = new HSSFWorkbook();
            string sheetName = "整改对比";
            HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet(sheetName);
            HSSFRow row1 = (HSSFRow)sheet.CreateRow(0);
            row1.Height = 1060;
            HSSFCellStyle cellStyle = (HSSFCellStyle)workbook.CreateCellStyle();
            cellStyle.BorderBottom = BorderStyle.Thin;//下边框
            cellStyle.BorderTop = BorderStyle.Thin;//上边框
            cellStyle.BorderLeft = BorderStyle.Thin;//左边框
            cellStyle.BorderRight = BorderStyle.Thin; //右边框
            cellStyle.Alignment = HorizontalAlignment.Center;//居中
            cellStyle.VerticalAlignment = VerticalAlignment.Center;
            HSSFFont hSSFFontHeader = (HSSFFont)workbook.CreateFont();
            hSSFFontHeader.FontName = "宋体";
            hSSFFontHeader.FontHeightInPoints = 11;
            cellStyle.SetFont(hSSFFontHeader);


            ICellStyle cellStyleFill = workbook.CreateCellStyle();//创建样式 
            cellStyleFill.FillPattern = FillPattern.SolidForeground;//填充模式 
            cellStyleFill.FillBackgroundColor = HSSFColor.Black.Index; //设置填充颜色

            HSSFCellStyle cellStyleHeader = (HSSFCellStyle)workbook.CreateCellStyle(); 
            cellStyleHeader.BorderBottom = BorderStyle.Thin;//下边框 
            cellStyleHeader.BorderTop = BorderStyle.Thin;//上边框 
            cellStyleHeader.BorderLeft = BorderStyle.Thin;//左边框 
            cellStyleHeader.BorderRight = BorderStyle.Thin; //右边框 
            cellStyleHeader.Alignment = HorizontalAlignment.Center;//水平居中 
            cellStyleHeader.VerticalAlignment = VerticalAlignment.Center; //垂直居中
            hSSFFontHeader = (HSSFFont)workbook.CreateFont(); //创建字体
            hSSFFontHeader.FontName = "仿宋"; 
            hSSFFontHeader.FontHeightInPoints = 22; 
            hSSFFontHeader.IsBold = true; //加粗
            cellStyleHeader.SetFont(hSSFFontHeader); //样式设置字体
            cellStyleHeader.FillForegroundColor = HSSFColor.Black.Index; 
            HSSFCell cellHeader = (HSSFCell)row1.CreateCell(0); 
            CellRangeAddress region = new CellRangeAddress(0, 0, 0, 4); //设置合并单元格范围
            sheet.AddMergedRegion(region); //合并单元格
     sheet.SetEnclosedBorderOfRegion(region, BorderStyle.Thin, NPOI.HSSF.Util.HSSFColor.Black.Index);//合并单元格设置样式
            cellHeader.CellStyle = cellStyleHeader; //单元格设定样式
            cellHeader.SetCellValue("33333");//单元格设置值

    #region 插入图片
    byte[] bytes = File.ReadAllBytes(files[i]);
    int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG);
    HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
    HSSFClientAnchor anchor = new HSSFClientAnchor(23, 5, 1000, 250, 0, i / 4 * 6 + 3, 0, i / 4 * 6 + 5);
    patriarch.CreatePicture(anchor, pictureIdx);

    #endregion

  

  

标签:cellStyle,NPOI,用法,cellStyleHeader,Thin,hSSFFontHeader,workbook,BorderStyle
来源: https://www.cnblogs.com/hdm188717/p/14838123.html