diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/ExcelExportServer.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/ExcelExportServer.java index 219187d..8756232 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/ExcelExportServer.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/ExcelExportServer.java @@ -56,7 +56,7 @@ public class ExcelExportServer extends ExcelExportBase { private int createHeaderAndTitle(ExportParams entity, Sheet sheet, Workbook workbook, List excelParams) { - int rows = 0, feildWidth = getFieldWidth(excelParams); + int rows = 0, feildWidth = getFieldLength(excelParams); if (entity.getTitle() != null) { rows += createHeaderRow(entity, sheet, workbook, feildWidth); } @@ -250,20 +250,4 @@ public class ExcelExportServer extends ExcelExportBase { } - /** - * 判断表头是只有一行还是两行 - * - * @param excelParams - * @return - */ - private int getRowNums(List excelParams) { - for (int i = 0; i < excelParams.size(); i++) { - if (excelParams.get(i).getList() != null - && StringUtils.isNotBlank(excelParams.get(i).getName())) { - return 2; - } - } - return 1; - } - } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/base/ExcelExportBase.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/base/ExcelExportBase.java index 4e9147d..15d9862 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/base/ExcelExportBase.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/base/ExcelExportBase.java @@ -312,20 +312,6 @@ public abstract class ExcelExportBase extends ExportBase { } } - /** - * 获取导出报表的字段总长度 - * - * @param excelParams - * @return - */ - public int getFieldWidth(List excelParams) { - int length = -1;// 从0开始计算单元格的 - for (ExcelExportEntity entity : excelParams) { - length += entity.getList() != null ? entity.getList().size() : 1; - } - return length; - } - /** * 获取图片类型,设置图片插入类型 * diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/base/ExportBase.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/base/ExportBase.java index a61d6cb..8cc4649 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/base/ExportBase.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/base/ExportBase.java @@ -62,8 +62,8 @@ public class ExportBase { * @throws Exception */ private ExcelExportEntity createExcelExportEntity(Field field, String targetId, - Class pojoClass, List getMethods) - throws Exception { + Class pojoClass, + List getMethods) throws Exception { Excel excel = field.getAnnotation(Excel.class); ExcelExportEntity excelEntity = new ExcelExportEntity(); excelEntity.setType(excel.type()); @@ -121,12 +121,15 @@ public class ExportBase { ParameterizedType pt = (ParameterizedType) field.getGenericType(); Class clz = (Class) pt.getActualTypeArguments()[0]; List list = new ArrayList(); - getAllExcelField(exclusions, StringUtils.isNotEmpty(excel.id()) ? excel.id() - : targetId, PoiPublicUtil.getClassFields(clz), list, clz, null); + getAllExcelField(exclusions, + StringUtils.isNotEmpty(excel.id()) ? excel.id() : targetId, + PoiPublicUtil.getClassFields(clz), list, clz, null); excelEntity = new ExcelExportEntity(); - excelEntity.setName(PoiPublicUtil.getValueByTargetId(excel.name(), targetId,null)); - excelEntity.setOrderNum(Integer.valueOf(PoiPublicUtil.getValueByTargetId(excel.orderNum(), targetId,"0"))); - excelEntity.setMethod(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName())); + excelEntity.setName(PoiPublicUtil.getValueByTargetId(excel.name(), targetId, null)); + excelEntity.setOrderNum(Integer + .valueOf(PoiPublicUtil.getValueByTargetId(excel.orderNum(), targetId, "0"))); + excelEntity + .setMethod(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName())); excelEntity.setList(list); excelParams.add(excelEntity); } else { @@ -136,9 +139,10 @@ public class ExportBase { } newMethods.add(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName())); ExcelEntity excel = field.getAnnotation(ExcelEntity.class); - getAllExcelField(exclusions, StringUtils.isNotEmpty(excel.id()) ? excel.id() - : targetId, PoiPublicUtil.getClassFields(field.getType()), excelParams, - field.getType(), newMethods); + getAllExcelField(exclusions, + StringUtils.isNotEmpty(excel.id()) ? excel.id() : targetId, + PoiPublicUtil.getClassFields(field.getType()), excelParams, field.getType(), + newMethods); } } } @@ -211,19 +215,19 @@ public class ExportBase { excelEntity.setMergeVertical(excel.mergeVertical()); excelEntity.setMergeRely(excel.mergeRely()); excelEntity.setReplace(excel.replace()); - excelEntity.setOrderNum(Integer.valueOf(PoiPublicUtil.getValueByTargetId(excel.orderNum(), targetId, "0"))); + excelEntity.setOrderNum( + Integer.valueOf(PoiPublicUtil.getValueByTargetId(excel.orderNum(), targetId, "0"))); excelEntity.setWrap(excel.isWrap()); excelEntity.setExportImageType(excel.imageType()); excelEntity.setSuffix(excel.suffix()); excelEntity.setDatabaseFormat(excel.databaseFormat()); - excelEntity.setFormat(StringUtils.isNotEmpty(excel.exportFormat()) ? excel.exportFormat() - : excel.format()); + excelEntity.setFormat( + StringUtils.isNotEmpty(excel.exportFormat()) ? excel.exportFormat() : excel.format()); excelEntity.setStatistics(excel.isStatistics()); excelEntity.setHyperlink(excel.isHyperlink()); excelEntity.setMethod(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName())); } - /** * 多个反射获取值 * @@ -252,12 +256,12 @@ public class ExportBase { public short getRowHeight(List excelParams) { double maxHeight = 0; for (int i = 0; i < excelParams.size(); i++) { - maxHeight = maxHeight > excelParams.get(i).getHeight() ? maxHeight : excelParams.get(i) - .getHeight(); + maxHeight = maxHeight > excelParams.get(i).getHeight() ? maxHeight + : excelParams.get(i).getHeight(); if (excelParams.get(i).getList() != null) { for (int j = 0; j < excelParams.get(i).getList().size(); j++) { - maxHeight = maxHeight > excelParams.get(i).getList().get(j).getHeight() ? maxHeight - : excelParams.get(i).getList().get(j).getHeight(); + maxHeight = maxHeight > excelParams.get(i).getList().get(j).getHeight() + ? maxHeight : excelParams.get(i).getList().get(j).getHeight(); } } } @@ -299,7 +303,7 @@ public class ExportBase { } } } - + /** * 添加Index列 * @param entity @@ -314,4 +318,34 @@ public class ExportBase { return exportEntity; } + /** + * 获取导出报表的字段总长度 + * + * @param excelParams + * @return + */ + public int getFieldLength(List excelParams) { + int length = -1;// 从0开始计算单元格的 + for (ExcelExportEntity entity : excelParams) { + length += entity.getList() != null ? entity.getList().size() : 1; + } + return length; + } + + /** + * 判断表头是只有一行还是两行 + * + * @param excelParams + * @return + */ + public int getRowNums(List excelParams) { + for (int i = 0; i < excelParams.size(); i++) { + if (excelParams.get(i).getList() != null + && StringUtils.isNotBlank(excelParams.get(i).getName())) { + return 2; + } + } + return 1; + } + } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/pdf/PdfExportUtil.java b/easypoi-base/src/main/java/org/jeecgframework/poi/pdf/PdfExportUtil.java index 849a0bc..42e83b3 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/pdf/PdfExportUtil.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/pdf/PdfExportUtil.java @@ -46,7 +46,7 @@ public class PdfExportUtil { */ public static Document exportPdf(ExportParams entity, Class pojoClass, Collection dataSet, OutputStream outStream) { - return new PdfExportServer(outStream).createTable(entity, pojoClass, dataSet); + return new PdfExportServer(outStream).createPdf(entity, pojoClass, dataSet); } /** @@ -62,7 +62,7 @@ public class PdfExportUtil { Collection> dataSet, OutputStream outStream) { - return new PdfExportServer(outStream).createPdfForMap(entity, entityList, dataSet); + return new PdfExportServer(outStream).createPdfByExportEntity(entity, entityList, dataSet); } } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/pdf/export/PdfExportServer.java b/easypoi-base/src/main/java/org/jeecgframework/poi/pdf/export/PdfExportServer.java index 1cbae9d..7ddf2c0 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/pdf/export/PdfExportServer.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/pdf/export/PdfExportServer.java @@ -22,8 +22,8 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; -import java.util.Map; +import org.apache.commons.lang3.StringUtils; import org.jeecgframework.poi.excel.annotation.ExcelTarget; import org.jeecgframework.poi.excel.entity.ExportParams; import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; @@ -34,10 +34,14 @@ import org.slf4j.LoggerFactory; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; +import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Font.FontFamily; +import com.itextpdf.text.PageSize; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.BaseFont; +import com.itextpdf.text.pdf.PdfPCell; +import com.itextpdf.text.pdf.PdfPRow; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; @@ -54,7 +58,7 @@ public class PdfExportServer extends ExportBase { public PdfExportServer(OutputStream outStream) { try { - document = new Document(); + document = new Document(PageSize.A4, 36, 36, 24, 36); PdfWriter.getInstance(document, outStream); document.open(); } catch (Exception e) { @@ -69,7 +73,7 @@ public class PdfExportServer extends ExportBase { * @param dataSet * @return */ - public Document createTable(ExportParams entity, Class pojoClass, Collection dataSet) { + public Document createPdf(ExportParams entity, Class pojoClass, Collection dataSet) { try { List excelParams = new ArrayList(); if (entity.isAddIndex()) { @@ -81,6 +85,19 @@ public class PdfExportServer extends ExportBase { String targetId = etarget == null ? null : etarget.value(); getAllExcelField(entity.getExclusions(), targetId, fileds, excelParams, pojoClass, null); + createPdfByExportEntity(entity, excelParams, dataSet); + } catch (Exception e) { + LOGGER.error(e.getMessage(), e); + } finally { + document.close(); + } + return document; + } + + public Document createPdfByExportEntity(ExportParams entity, + List excelParams, + Collection dataSet) { + try { sortAllParams(excelParams); //设置各个列的宽度 float[] widths = getCellWidths(excelParams); @@ -96,10 +113,10 @@ public class PdfExportServer extends ExportBase { createCells(table, t, excelParams, rowHeight); } document.add(table); + } catch (DocumentException e) { + LOGGER.error(e.getMessage(), e); } catch (Exception e) { LOGGER.error(e.getMessage(), e); - } finally { - document.close(); } return document; } @@ -108,7 +125,6 @@ public class PdfExportServer extends ExportBase { short rowHeight) throws Exception { ExcelExportEntity entity; int maxHeight = 1, cellNum = 0; - //int indexKey = createIndexCell(row, index, excelParams.get(0)); //cellNum += indexKey; for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { entity = excelParams.get(k); @@ -127,8 +143,10 @@ public class PdfExportServer extends ExportBase { } else { Object value = getCellValue(entity, t); if (entity.getType() == 1) { - table.addCell(value == null ? "" : value.toString()); + createStringCell(table, value == null ? "" : value.toString(), + (int) (entity.getHeight() * 2.5)); } else { + } } } @@ -155,11 +173,116 @@ public class PdfExportServer extends ExportBase { private void createHeaderAndTitle(ExportParams entity, PdfPTable table, List excelParams) throws DocumentException { - for (int i = 0; i < excelParams.size(); i++) { - table.addCell(new Phrase(excelParams.get(i).getName(), getFont())); + int feildWidth = getFieldLength(excelParams); + if (entity.getTitle() != null) { + createHeaderRow(entity, table, feildWidth); + } + createTitleRow(entity, table, excelParams); + } + + /** + * 创建表头 + * + * @param title + * @param index + */ + private int createTitleRow(ExportParams title, PdfPTable table, + List excelParams) { + int rows = getRowNums(excelParams); + int cellIndex = 0; + PdfPCell iCell = null; + int currentRows = table.getLastCompletedRowIndex(), + filedLength = getFieldLength(excelParams); + //先创建空白的Cell 然后去循环 + for (int i = 0; i < rows; i++) { + for (int j = 0; j <= filedLength; j++) { + createStringCell(table, "", 25); + } + } + PdfPRow listRow = null; + if (rows == 2) { + listRow = table.getRow(currentRows + 2); + } + + PdfPRow row = table.getRow(currentRows + 1); + + for (int i = 0, exportFieldTitleSize = excelParams.size(); i < exportFieldTitleSize; i++) + + { + ExcelExportEntity entity = excelParams.get(i); + if (StringUtils.isNotBlank(entity.getName())) { + iCell = setStringCell(row.getCells()[cellIndex], entity.getName(), + (int) entity.getHeight()); + } + if (entity.getList() != null) { + List sTitel = entity.getList(); + if (StringUtils.isNotBlank(entity.getName())) { + iCell.setColspan(sTitel.size()); + } + for (int j = 0, size = sTitel.size(); j < size; j++) { + setStringCell( + rows == 2 ? listRow.getCells()[cellIndex] : row.getCells()[cellIndex], + sTitel.get(j).getName(), (int) sTitel.get(j).getHeight()); + cellIndex++; + } + cellIndex--; + } else if (rows == 2) { + iCell.setRowspan(2); + //把合并后的边框去掉 + iCell.setBorderWidthBottom(0); + listRow.getCells()[cellIndex].setBorderWidthTop(0); + } + cellIndex++; + } + return rows; + + } + + private void createHeaderRow(ExportParams entity, PdfPTable table, int feildLength) { + + createStringCell(table, entity.getTitle(), entity.getTitleHeight() / 20, feildLength + 1, + 1); + if (entity.getSecondTitle() != null) { + PdfPCell iCell = new PdfPCell(new Phrase(entity.getSecondTitle(), getFont())); + iCell.setHorizontalAlignment(Element.ALIGN_RIGHT); + iCell.setVerticalAlignment(Element.ALIGN_CENTER); + iCell.setFixedHeight(entity.getSecondTitleHeight() / 20); + iCell.setColspan(feildLength + 1); + table.addCell(iCell); } } + private PdfPCell createStringCell(PdfPTable table, String value, int height, int colspan, + int rowspan) { + PdfPCell iCell = new PdfPCell(new Phrase(value, getFont())); + iCell.setHorizontalAlignment(Element.ALIGN_CENTER); + iCell.setVerticalAlignment(Element.ALIGN_MIDDLE); + iCell.setFixedHeight(height); + if (colspan > 1) { + iCell.setColspan(colspan); + } + if (rowspan > 1) { + iCell.setRowspan(rowspan); + } + table.addCell(iCell); + return iCell; + } + + private PdfPCell createStringCell(PdfPTable table, String value, int height) { + PdfPCell iCell = new PdfPCell(new Phrase(value, getFont())); + iCell.setHorizontalAlignment(Element.ALIGN_CENTER); + iCell.setVerticalAlignment(Element.ALIGN_MIDDLE); + iCell.setFixedHeight(height); + table.addCell(iCell); + return iCell; + } + + private PdfPCell setStringCell(PdfPCell iCell, String value, int height) { + iCell.setPhrase(new Phrase(value, getFont())); + iCell.setFixedHeight(height); + return iCell; + } + private Font getFont() { try { //用以支持中文 @@ -176,10 +299,4 @@ public class PdfExportServer extends ExportBase { return font; } - public Document createPdfForMap(ExportParams entity, List entityList, - Collection> dataSet) { - - return document; - } - }