public override void AdjustCellSize(int width, int height, SizeF needSizeF, I3ReportCellStyle style, I3ReportCell cell, I3MergeRange range, I3ReportData reportData, bool prepareNarrow) { I3ReportImageCell imageCell = cell as I3ReportImageCell; int doubleSplit = ImageSplit * 2; if (needSizeF.Width > width - doubleSplit || needSizeF.Height > height - doubleSplit) { switch (style.AdjustSize) { case I3AdjustSize.扩大单元格: #region 扩大单元格 if (needSizeF.Width > width - doubleSplit) { float pro = (needSizeF.Width + doubleSplit) / width; for (int i = range.StartCol; i <= range.EndCol; i++) { reportData.Cols[i].Width = (int)(reportData.Cols[i].Width * pro); } } if (needSizeF.Height > height - doubleSplit) { float pro = (needSizeF.Height + doubleSplit) / height; for (int i = range.StartRow; i <= range.EndRow; i++) { reportData[i].Height = (int)(reportData[i].Height * pro); } } cell.StringTrimming = StringTrimming.None; #endregion break; case I3AdjustSize.缩小内容: #region 缩小内容 if (prepareNarrow) { //先使宽度一致 imageCell.CalWidth = width - doubleSplit; imageCell.CalHeight = imageCell.Height * imageCell.CalWidth / imageCell.Width; //如果高度太大,则使高度一致 if (imageCell.CalHeight > height - doubleSplit) { imageCell.CalHeight = height - doubleSplit; imageCell.CalWidth = imageCell.Width * imageCell.CalHeight / imageCell.Height; } } #endregion break; default: //都不变,调整为图片不按比例缩小 #region 图片不按比例缩小 imageCell.CalWidth = width - doubleSplit; imageCell.CalHeight = height - doubleSplit; #endregion break; } } }
public override SizeF CalCellNeedSize(int orgWidth, int orgHeight, I3ReportCellStyle style, I3ReportCell cell) { SizeF sizeF = new SizeF(); I3ReportImageCell imageCell = cell as I3ReportImageCell; sizeF.Width = imageCell.Width; sizeF.Height = imageCell.Height; return(sizeF); }
/// <summary> /// 获取或创建带图片的单元格 /// </summary> /// <param name="row"></param> /// <param name="col"></param> /// <returns></returns> public I3ReportImageCell GetOrCreateImageItem(int row, int col) { I3ReportImageCell item = GetCellItem(row, col) as I3ReportImageCell; if (item == null) { item = new I3ReportImageCell(row, col); this[row][col] = item; } return(item); }
public override RectangleF DrawContent(Graphics g, float scale, I3ReportData reportData, I3ReportCell cell, System.Drawing.RectangleF rect, I3ReportCellStyle style, I3PrintArea area, bool draw) { I3ReportImageCell imageCell = cell as I3ReportImageCell; if (imageCell.ImageData == null) { return(RectangleF.Empty); } try { using (MemoryStream stream = new MemoryStream(imageCell.ImageData)) { using (Bitmap bitmap = new Bitmap(stream)) { float width = imageCell.CalWidth > 0 ? imageCell.CalWidth : imageCell.Width; width *= scale; float heigth = imageCell.CalHeight > 0 ? imageCell.CalHeight : imageCell.Height; heigth *= scale; RectangleF destRect = new RectangleF((float)rect.X + (float)rect.Width / 2F - (float)width / 2F, (float)rect.Y + (float)rect.Height / 2F - (float)heigth / 2F, width, heigth); RectangleF srcRect = new RectangleF(0F, 0F, (float)bitmap.Width, (float)bitmap.Height); if (draw && !destRect.IsEmpty) { g.DrawImage(bitmap, destRect, srcRect, GraphicsUnit.Pixel); } return(destRect); } } } catch { RectangleF destRect = new RectangleF((float)rect.X + (float)rect.Width / 2F - (float)100 / 2F, (float)rect.Y + (float)rect.Height / 2F - (float)100 / 2F, 100, 100); return(destRect); } }
/// <summary> /// 转换为图片单元格,复制所有属性(目的是为了在ReportData经过json转换后,找回Cell中丢失的单元格类型信息,要求I3ReportImageCell代码中的属性,全部写在I3ReportCell中) /// </summary> /// <param name="row"></param> /// <param name="col"></param> /// <returns></returns> public I3ReportImageCell ConvertToImageItem(int row, int col) { I3ReportCell old = GetCellItem(row, col); if (old != null && old is I3ReportImageCell) { return(old as I3ReportImageCell); } I3ReportImageCell item = null; if (old == null) { item = new I3ReportImageCell(row, col); } else { string json = I3JsonConvert.ToJson(old); item = (I3ReportImageCell)I3JsonConvert.FromJson(json, typeof(I3ReportImageCell)); } this[row][col] = item; return(item); }
private void exportAreaToSheet(I3PrintArea area, HSSFWorkbook workbook, ISheet sheet, ref int rowCount, Dictionary <string, HSSFCellStyle> styleDic, Dictionary <string, IFont> fontDic) { HSSFCellStyle emptyStyle = createEmptyStyle(workbook); //导出行、单元格 foreach (int rowIndex in area.AllRows) { //设置行高 rowCount++; IRow row = sheet.CreateRow(rowCount - 1); I3ReportRow rowData = area.ReportData.Rows[rowIndex]; if (rowData.PageBreak) //分页符 { sheet.SetRowBreak(rowCount - 1); } double dh = (double)rowData.Height * (double)15.305838; dh = dh * (double)1.1; int height = (int)Math.Ceiling(dh); row.Height = (short)height; //设置文本 int colIndex = -1; foreach (I3ReportCell cellData in rowData.Cells) { colIndex++; ICell cell = row.CreateCell(colIndex); bool hasReturnInText = false; if (cellData.MergState != I3MergeState.Merged) { hasReturnInText = !string.IsNullOrEmpty(cellData.Text) && cellData.Text.IndexOf("{r}") >= 0; if (cellData is I3ReportImageCell) { I3ReportImageCell imageCell = (I3ReportImageCell)cellData; byte[] bytes = imageCell.ImageData; if (bytes != null && bytes.Length > 0) { try { int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG); HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch(); // 插图片的位置 HSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2) 后面再作解释 int row1 = rowCount - 1; int row2 = rowCount - 1; int col1 = colIndex; int col2 = colIndex; if (cellData.MergState == I3MergeState.FirstCell) { I3MergeRange mr = area.ReportData.GetMergeRange(cellData.Row, cellData.Col); row2 = rowCount - 1 + mr.EndRow - mr.StartRow; col2 = mr.EndCol; } HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, col1, row1, col2, row2); //把图片插到相应的位置 HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx); } catch { } } } else { string text = string.IsNullOrEmpty(cellData.Text) ? "" : cellData.Text.Replace("{r}", "\r\n"); //特殊符号....使用替换符如{1级钢筋}{WL}{屈特比} cell.SetCellValue(text); } } //设置样式 int lastRowIndex = area.AllRows[area.AllRows.Length - 1]; bool isLastRow = rowData.Row == lastRowIndex; bool isLastCol = cellData.Col == area.ReportData.Cols.Length - 1; setCellStyle(workbook, sheet, cell, area, cellData, styleDic, fontDic, hasReturnInText, isLastRow, isLastCol, emptyStyle); //设置合并 if (cellData.MergState == I3MergeState.FirstCell) { I3MergeRange mr = area.ReportData.GetMergeRange(cellData.Row, cellData.Col); CellRangeAddress ra = new CellRangeAddress(rowCount - 1, rowCount - 1 + mr.EndRow - mr.StartRow, mr.StartCol, mr.EndCol); sheet.AddMergedRegion(ra); } } } }