NPOI操作excel--以及组件转换成Pdf

1.新建workbook  不同版本,个人建议07以上为准。

public static IWorkbook Create(ExcelVersion version = ExcelVersion.Excel07)
        {
            IWorkbook workbook = null;

            switch (version)
            {
                case ExcelVersion.Excel03:
                    workbook = new HSSFWorkbook();
                    break;
                case ExcelVersion.Excel07:
                    workbook = new XSSFWorkbook();
                    break;
                default:
                    workbook = new XSSFWorkbook();
                    break;
            }

            return workbook;
        }

 模板也是可以创建的。 

   using (FileStream file = new FileStream(template, FileMode.Open, FileAccess.Read))
            {
                IWorkbook workbook = WorkbookFactory.Create(file);
                return workbook;
            }

workbook 可以创建多个sheet

   var sheet = workbook.CreateSheet("invoice");  //参数是sheet name

  sheet里面创建行

  IRow headRow = sheet.CreateRow(rowIndex);     //参数是int 型行号,默认从0开始

  行里创建单元格

Row.CreateCell(0).SetCellValue("测试"); // 单元格赋值

  2。是我本人遇到的一些样式问题:

a.设置列宽

 int[] columnWidth = new int[] { 5, 20, 20, 10, 8, 8, 8, 8 };
            // 单位是1/256个字符宽度
            if (columnWidth != null)
            {
                for (int i = 0; i < columnWidth.Length; i++)
                {
                    sheet.SetColumnWidth(i, 256 * columnWidth[i]);
                }
            }
       

b.设置边框 (都是以单元格为单位的) 

//边框样式
            ICellStyle style = workbook.CreateCellStyle();
            style.BorderBottom = BorderStyle.Hair;
            style.BorderLeft = BorderStyle.Hair;
            style.BorderRight = BorderStyle.Hair;
            style.BorderTop = BorderStyle.Hair;

  //字体
            NPOI.SS.UserModel.IFont font = workbook.CreateFont();
            font.FontHeightInPoints = 8;   //8号字 字号
            font.FontName = "SimSun";   //宋体 字体
            style.SetFont(font);

//加背景色
            style.FillPattern = FillPattern.SolidForeground;
            style.FillForegroundColor = HSSFColor.Teal.Index;  //HSSFColor有颜色对照表可以参考的

上面只是一个style变量。
  row.GetCell(0).CellStyle = style;
//某个单元格设置样式

  c.合并单元格

  sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 0, 4));
//参数是firstRow index,LastRowIndex,firstColumnIndex,LastColumnIndex

  d.设置行高和换行

  row.Height = 9 * 20;  //一定要乘以20,具体原因不记得了  设置 行高

//针对内容过长,需要换行,防止excel内容被遮挡。
// 获取当前列宽度;
            int columnWidth = sheet.GetColumnWidth(columnIndex) / 256;
            //当前行
            var currentRow = sheet.GetRow(rowIndex);
            //当前单元格
            var currentCell = currentRow.GetCell(columnIndex);
            //当前单元格内容的长度
            int length = currentCell.ToString().Length;

            currentRow.Height = 20 * 20;
                //内容换行
            StringBuilder stringBuilder = new StringBuilder(currentRow.Cells[columnIndex].StringCellValue.Substring(0, 13) + "
");
            var appendString = currentRow.Cells[columnIndex].StringCellValue.Substring(13, length - 13);
             stringBuilder.Append(appendString);
                //设置换行

            ICellStyle wrapStyle = workbook.CreateCellStyle();
                wrapStyle.WrapText = true;
            currentRow.Cells[columnIndex].CellStyle = wrapStyle;        

  

 最后加入图片:

  byte[] bytes = File.ReadAllBytes(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                VendorContext.Current.SealUrl));
            int pictureIndex = workbook.AddPicture(bytes, PictureType.PNG);
            var patriarch = sheet.CreateDrawingPatriarch();
            XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 1023, 0, 1, rowIndex - 1, 3, rowIndex + 2);
            var pict = patriarch.CreatePicture(anchor, pictureIndex);
            pict.Resize();


//这块图片可能拉伸,注意XSSFClientAnchor的参数(大概是x y坐标之类的,反正多测试)

  

  

改变自己,成长自己
原文地址:https://www.cnblogs.com/xxh-2014/p/11208699.html