コード例 #1
0
 /// <summary>
 /// 添加图片
 /// </summary>
 /// <param name="sheet"></param>
 /// <param name="picInfo"></param>
 public static void AddPicture(this ISheet sheet, PictureInfo picInfo)
 {
     var pictureIdx = sheet.Workbook.AddPicture(picInfo.PictureData, PictureType.PNG);
     var anchor = sheet.Workbook.GetCreationHelper().CreateClientAnchor();
     anchor.Col1 = picInfo.MinCol;
     anchor.Col2 = picInfo.MaxCol;
     anchor.Row1 = picInfo.MinRow;
     anchor.Row2 = picInfo.MaxRow;
     anchor.Dx1 = picInfo.PicturesStyle.AnchorDx1;
     anchor.Dx2 = picInfo.PicturesStyle.AnchorDx2;
     anchor.Dy1 = picInfo.PicturesStyle.AnchorDy1;
     anchor.Dy2 = picInfo.PicturesStyle.AnchorDy2;
     anchor.AnchorType = 2;
     var drawing = sheet.CreateDrawingPatriarch();
     var pic = drawing.CreatePicture(anchor, pictureIdx);
     if (sheet is HSSFSheet)
     {
         var shape = (HSSFShape)pic;
         shape.FillColor = picInfo.PicturesStyle.FillColor;
         shape.IsNoFill = picInfo.PicturesStyle.IsNoFill;
         shape.LineStyle = picInfo.PicturesStyle.LineStyle;
         shape.LineStyleColor = picInfo.PicturesStyle.LineStyleColor;
         shape.LineWidth = (int)picInfo.PicturesStyle.LineWidth;
     }
     else if (sheet is XSSFSheet)
     {
         var shape = (XSSFShape)pic;
         shape.FillColor = picInfo.PicturesStyle.FillColor;
         shape.IsNoFill = picInfo.PicturesStyle.IsNoFill;
         shape.LineStyle = picInfo.PicturesStyle.LineStyle;
         //shape.LineStyleColor = picInfo.PicturesStyle.LineStyleColor;
         shape.LineWidth = picInfo.PicturesStyle.LineWidth;
     }
 }
コード例 #2
0
        private static void CreateRowItem(this ISheet target, IWorkbook workbook, DataTable dataSource)
        {
            IRow row = null;
            ICell cell = null;
            ICellStyle cellStyle = null;
            IDrawing drawing = null;
            IPicture picture = null;

            cellStyle = workbook.CreateCellStyle();
            cellStyle.Alignment = HorizontalAlignment.CENTER;
            cellStyle.VerticalAlignment = VerticalAlignment.CENTER;

            for (int rowIndex = 0; rowIndex < dataSource.Rows.Count; rowIndex++)
            {
                row = target.CreateRow(rowIndex + 1);

                for (int columnIndex = 0; columnIndex < dataSource.Columns.Count; columnIndex++)
                {
                    cell = row.CreateCell(columnIndex);

                    if (dataSource.Columns[columnIndex].DataType == typeof(byte[]))
                    {
                        byte[] image = dataSource.Rows[rowIndex][columnIndex] as byte[];

                        if (image != null && image.Length > 0)
                        {
                            int pictureIndex = workbook.AddPicture(dataSource.Rows[rowIndex][columnIndex] as byte[], PictureType.JPEG);

                            drawing = target.CreateDrawingPatriarch();
                            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, columnIndex, rowIndex + 1, columnIndex, rowIndex + 1);
                            picture = drawing.CreatePicture(anchor, pictureIndex);
                            picture.Resize();
                        }
                    }

                    else
                    {
                        cell.SetCellValue(dataSource.Rows[rowIndex][columnIndex].ToString());
                    }

                    cell.CellStyle = cellStyle;
                }
            }
        }