/// <summary>
        /// 添加图片
        /// </summary>
        /// <param name="wb">工作簿对象</param>
        /// <param name="sheet">工作表对象</param>
        /// <param name="pics">图片列表</param>
        public void AddPicture(IWorkbook wb, ISheet sheet, List <ExcelPictureModel> pics)
        {
            var pictureIdx = 0;
            var pic        = new ExcelPictureModel();

            for (int i = 0; i < pics.Count; i++)
            {
                pic        = pics[i];
                pictureIdx = wb.AddPicture(pic.Datas, pic.PicType);
                HSSFPatriarch    patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
                HSSFClientAnchor anchor    = new HSSFClientAnchor(0, 0, pic.Weight, pic.Height, pic.StartCol, pic.StartRow, pic.EndCol, pic.EndRow);
                HSSFPicture      pict      = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);

                if (pic.IsResize)
                {
                    pict.Resize();
                }
            }
        }
Exemple #2
0
        public static Dictionary <string, string> printTypeDict = new Dictionary <string, string>();//打印类型字典(Key:打印类型,Value:相应打印类型的模板ExceJSON数据)

        /// <summary>
        /// 通过模板文件获取单元格集合
        /// </summary>
        /// <param name="workbook"></param>
        /// <param name="qrCode">二维码Code</param>
        /// <returns></returns>
        public static List <ExcelCellModel> GetCellListByTemplateFile(XSSFWorkbook workbook)
        {
            XSSFSheet             sheet    = (XSSFSheet)workbook.GetSheetAt(0);
            List <ExcelCellModel> dataList = null;
            ExcelCellModel        data     = null;
            IRow              row          = null;
            ICell             cell         = null;
            ExcelPictureModel pictureModel = null;
            var columnWith = 0;

            BorderStyle[] border = new BorderStyle[4];
            try
            {
                //总行数
                int rowCount = sheet.LastRowNum;
                if (rowCount > 0)
                {
                    dataList = new List <ExcelCellModel>();
                    IRow firstRow  = sheet.GetRow(0);      //第一行
                    int  cellCount = firstRow.LastCellNum; //总列数

                    //填充行
                    for (int i = 0; i <= rowCount; ++i)
                    {
                        //清空值
                        border = new BorderStyle[4];

                        row = sheet.GetRow(i);
                        if (row == null)
                        {
                            continue;
                        }
                        for (int j = row.FirstCellNum; j < cellCount; ++j)
                        {
                            cell = row.GetCell(j);
                            if (cell == null)
                            {
                            }
                            else
                            {
                                #region cell赋值

                                //四个边框按照 上下左右
                                border[0]  = cell.CellStyle.BorderTop;
                                border[1]  = cell.CellStyle.BorderBottom;
                                border[2]  = cell.CellStyle.BorderLeft;
                                border[3]  = cell.CellStyle.BorderRight;
                                columnWith = sheet.GetColumnWidth(j);
                                data       = new ExcelCellModel()
                                {
                                    rowNum              = cell.RowIndex,
                                    cellNum             = cell.ColumnIndex,
                                    cellWidth           = columnWith,
                                    cellHeight          = row.Height,
                                    fontSize            = cell.CellStyle.GetFont(workbook).FontHeightInPoints,
                                    wrapText            = cell.CellStyle.WrapText,
                                    borderLine          = border,
                                    Boldweight          = cell.CellStyle.GetFont(workbook).IsBold,
                                    verticalAlignment   = cell.CellStyle.VerticalAlignment,
                                    horizontalAlignment = cell.CellStyle.Alignment,
                                    fontName            = cell.CellStyle.GetFont(workbook).FontName,
                                    //cellColor=cell.CellStyle.GetFont(workbook).Color
                                    location = "",
                                    // regionCell=""
                                };
                                //CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,)
                                switch (cell.CellType)
                                {
                                case CellType.Blank:
                                    data.cellValue = "";
                                    break;

                                case CellType.Numeric:
                                    short format = cell.CellStyle.DataFormat;
                                    //对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理
                                    if (format == 14 || format == 31 || format == 57 || format == 58)
                                    {
                                        data.cellValue = cell.DateCellValue.ToString();
                                    }
                                    else
                                    {
                                        data.cellValue = cell.NumericCellValue.ToString();
                                    }
                                    break;

                                case CellType.String:
                                    data.cellValue = cell.StringCellValue;
                                    break;
                                }


                                //二维码单独处理
                                if (data.cellValue == PrintStruct.QRCODE)
                                {
                                    pictureModel = new ExcelPictureModel()
                                    {
                                        endColNum = data.cellNum,
                                        endRowNum = data.rowNum,
                                        qrCode    = PrintStruct.QRCODE,
                                        CodeType  = PrintStructFlag.QRCODE
                                    };
                                    data.excelPictureModel = pictureModel;
                                }

                                //一维码单独处理
                                if (data.cellValue == PrintStruct.BARCODE)
                                {
                                    pictureModel = new ExcelPictureModel()
                                    {
                                        endColNum = data.cellNum,
                                        endRowNum = data.rowNum,
                                        qrCode    = PrintStruct.BARCODE,
                                        CodeType  = PrintStructFlag.BARCODE
                                    };
                                    data.excelPictureModel = pictureModel;
                                }

                                #endregion
                                dataList.Add(data);
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return(dataList);
        }