@@ -56,7 +56,7 @@ public class ExcelExportServer extends ExcelExportBase { | |||||
private int createHeaderAndTitle(ExportParams entity, Sheet sheet, Workbook workbook, | private int createHeaderAndTitle(ExportParams entity, Sheet sheet, Workbook workbook, | ||||
List<ExcelExportEntity> excelParams) { | List<ExcelExportEntity> excelParams) { | ||||
int rows = 0, feildWidth = getFieldWidth(excelParams); | |||||
int rows = 0, feildWidth = getFieldLength(excelParams); | |||||
if (entity.getTitle() != null) { | if (entity.getTitle() != null) { | ||||
rows += createHeaderRow(entity, sheet, workbook, feildWidth); | rows += createHeaderRow(entity, sheet, workbook, feildWidth); | ||||
} | } | ||||
@@ -250,20 +250,4 @@ public class ExcelExportServer extends ExcelExportBase { | |||||
} | } | ||||
/** | |||||
* 判断表头是只有一行还是两行 | |||||
* | |||||
* @param excelParams | |||||
* @return | |||||
*/ | |||||
private int getRowNums(List<ExcelExportEntity> 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; | |||||
} | |||||
} | } |
@@ -312,20 +312,6 @@ public abstract class ExcelExportBase extends ExportBase { | |||||
} | } | ||||
} | } | ||||
/** | |||||
* 获取导出报表的字段总长度 | |||||
* | |||||
* @param excelParams | |||||
* @return | |||||
*/ | |||||
public int getFieldWidth(List<ExcelExportEntity> excelParams) { | |||||
int length = -1;// 从0开始计算单元格的 | |||||
for (ExcelExportEntity entity : excelParams) { | |||||
length += entity.getList() != null ? entity.getList().size() : 1; | |||||
} | |||||
return length; | |||||
} | |||||
/** | /** | ||||
* 获取图片类型,设置图片插入类型 | * 获取图片类型,设置图片插入类型 | ||||
* | * | ||||
@@ -62,8 +62,8 @@ public class ExportBase { | |||||
* @throws Exception | * @throws Exception | ||||
*/ | */ | ||||
private ExcelExportEntity createExcelExportEntity(Field field, String targetId, | private ExcelExportEntity createExcelExportEntity(Field field, String targetId, | ||||
Class<?> pojoClass, List<Method> getMethods) | |||||
throws Exception { | |||||
Class<?> pojoClass, | |||||
List<Method> getMethods) throws Exception { | |||||
Excel excel = field.getAnnotation(Excel.class); | Excel excel = field.getAnnotation(Excel.class); | ||||
ExcelExportEntity excelEntity = new ExcelExportEntity(); | ExcelExportEntity excelEntity = new ExcelExportEntity(); | ||||
excelEntity.setType(excel.type()); | excelEntity.setType(excel.type()); | ||||
@@ -121,12 +121,15 @@ public class ExportBase { | |||||
ParameterizedType pt = (ParameterizedType) field.getGenericType(); | ParameterizedType pt = (ParameterizedType) field.getGenericType(); | ||||
Class<?> clz = (Class<?>) pt.getActualTypeArguments()[0]; | Class<?> clz = (Class<?>) pt.getActualTypeArguments()[0]; | ||||
List<ExcelExportEntity> list = new ArrayList<ExcelExportEntity>(); | List<ExcelExportEntity> list = new ArrayList<ExcelExportEntity>(); | ||||
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 = 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); | excelEntity.setList(list); | ||||
excelParams.add(excelEntity); | excelParams.add(excelEntity); | ||||
} else { | } else { | ||||
@@ -136,9 +139,10 @@ public class ExportBase { | |||||
} | } | ||||
newMethods.add(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName())); | newMethods.add(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName())); | ||||
ExcelEntity excel = field.getAnnotation(ExcelEntity.class); | 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.setMergeVertical(excel.mergeVertical()); | ||||
excelEntity.setMergeRely(excel.mergeRely()); | excelEntity.setMergeRely(excel.mergeRely()); | ||||
excelEntity.setReplace(excel.replace()); | 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.setWrap(excel.isWrap()); | ||||
excelEntity.setExportImageType(excel.imageType()); | excelEntity.setExportImageType(excel.imageType()); | ||||
excelEntity.setSuffix(excel.suffix()); | excelEntity.setSuffix(excel.suffix()); | ||||
excelEntity.setDatabaseFormat(excel.databaseFormat()); | 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.setStatistics(excel.isStatistics()); | ||||
excelEntity.setHyperlink(excel.isHyperlink()); | excelEntity.setHyperlink(excel.isHyperlink()); | ||||
excelEntity.setMethod(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName())); | excelEntity.setMethod(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName())); | ||||
} | } | ||||
/** | /** | ||||
* 多个反射获取值 | * 多个反射获取值 | ||||
* | * | ||||
@@ -252,12 +256,12 @@ public class ExportBase { | |||||
public short getRowHeight(List<ExcelExportEntity> excelParams) { | public short getRowHeight(List<ExcelExportEntity> excelParams) { | ||||
double maxHeight = 0; | double maxHeight = 0; | ||||
for (int i = 0; i < excelParams.size(); i++) { | 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) { | if (excelParams.get(i).getList() != null) { | ||||
for (int j = 0; j < excelParams.get(i).getList().size(); j++) { | 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列 | * 添加Index列 | ||||
* @param entity | * @param entity | ||||
@@ -314,4 +318,34 @@ public class ExportBase { | |||||
return exportEntity; | return exportEntity; | ||||
} | } | ||||
/** | |||||
* 获取导出报表的字段总长度 | |||||
* | |||||
* @param excelParams | |||||
* @return | |||||
*/ | |||||
public int getFieldLength(List<ExcelExportEntity> 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<ExcelExportEntity> 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; | |||||
} | |||||
} | } |
@@ -46,7 +46,7 @@ public class PdfExportUtil { | |||||
*/ | */ | ||||
public static Document exportPdf(ExportParams entity, Class<?> pojoClass, Collection<?> dataSet, | public static Document exportPdf(ExportParams entity, Class<?> pojoClass, Collection<?> dataSet, | ||||
OutputStream outStream) { | 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<? extends Map<?, ?>> dataSet, | Collection<? extends Map<?, ?>> dataSet, | ||||
OutputStream outStream) { | OutputStream outStream) { | ||||
return new PdfExportServer(outStream).createPdfForMap(entity, entityList, dataSet); | |||||
return new PdfExportServer(outStream).createPdfByExportEntity(entity, entityList, dataSet); | |||||
} | } | ||||
} | } |
@@ -22,8 +22,8 @@ import java.util.ArrayList; | |||||
import java.util.Collection; | import java.util.Collection; | ||||
import java.util.Iterator; | import java.util.Iterator; | ||||
import java.util.List; | 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.annotation.ExcelTarget; | ||||
import org.jeecgframework.poi.excel.entity.ExportParams; | import org.jeecgframework.poi.excel.entity.ExportParams; | ||||
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | 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.Document; | ||||
import com.itextpdf.text.DocumentException; | import com.itextpdf.text.DocumentException; | ||||
import com.itextpdf.text.Element; | |||||
import com.itextpdf.text.Font; | import com.itextpdf.text.Font; | ||||
import com.itextpdf.text.Font.FontFamily; | import com.itextpdf.text.Font.FontFamily; | ||||
import com.itextpdf.text.PageSize; | |||||
import com.itextpdf.text.Phrase; | import com.itextpdf.text.Phrase; | ||||
import com.itextpdf.text.pdf.BaseFont; | 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.PdfPTable; | ||||
import com.itextpdf.text.pdf.PdfWriter; | import com.itextpdf.text.pdf.PdfWriter; | ||||
@@ -54,7 +58,7 @@ public class PdfExportServer extends ExportBase { | |||||
public PdfExportServer(OutputStream outStream) { | public PdfExportServer(OutputStream outStream) { | ||||
try { | try { | ||||
document = new Document(); | |||||
document = new Document(PageSize.A4, 36, 36, 24, 36); | |||||
PdfWriter.getInstance(document, outStream); | PdfWriter.getInstance(document, outStream); | ||||
document.open(); | document.open(); | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
@@ -69,7 +73,7 @@ public class PdfExportServer extends ExportBase { | |||||
* @param dataSet | * @param dataSet | ||||
* @return | * @return | ||||
*/ | */ | ||||
public Document createTable(ExportParams entity, Class<?> pojoClass, Collection<?> dataSet) { | |||||
public Document createPdf(ExportParams entity, Class<?> pojoClass, Collection<?> dataSet) { | |||||
try { | try { | ||||
List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>(); | List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>(); | ||||
if (entity.isAddIndex()) { | if (entity.isAddIndex()) { | ||||
@@ -81,6 +85,19 @@ public class PdfExportServer extends ExportBase { | |||||
String targetId = etarget == null ? null : etarget.value(); | String targetId = etarget == null ? null : etarget.value(); | ||||
getAllExcelField(entity.getExclusions(), targetId, fileds, excelParams, pojoClass, | getAllExcelField(entity.getExclusions(), targetId, fileds, excelParams, pojoClass, | ||||
null); | null); | ||||
createPdfByExportEntity(entity, excelParams, dataSet); | |||||
} catch (Exception e) { | |||||
LOGGER.error(e.getMessage(), e); | |||||
} finally { | |||||
document.close(); | |||||
} | |||||
return document; | |||||
} | |||||
public Document createPdfByExportEntity(ExportParams entity, | |||||
List<ExcelExportEntity> excelParams, | |||||
Collection<?> dataSet) { | |||||
try { | |||||
sortAllParams(excelParams); | sortAllParams(excelParams); | ||||
//设置各个列的宽度 | //设置各个列的宽度 | ||||
float[] widths = getCellWidths(excelParams); | float[] widths = getCellWidths(excelParams); | ||||
@@ -96,10 +113,10 @@ public class PdfExportServer extends ExportBase { | |||||
createCells(table, t, excelParams, rowHeight); | createCells(table, t, excelParams, rowHeight); | ||||
} | } | ||||
document.add(table); | document.add(table); | ||||
} catch (DocumentException e) { | |||||
LOGGER.error(e.getMessage(), e); | |||||
} catch (Exception e) { | } catch (Exception e) { | ||||
LOGGER.error(e.getMessage(), e); | LOGGER.error(e.getMessage(), e); | ||||
} finally { | |||||
document.close(); | |||||
} | } | ||||
return document; | return document; | ||||
} | } | ||||
@@ -108,7 +125,6 @@ public class PdfExportServer extends ExportBase { | |||||
short rowHeight) throws Exception { | short rowHeight) throws Exception { | ||||
ExcelExportEntity entity; | ExcelExportEntity entity; | ||||
int maxHeight = 1, cellNum = 0; | int maxHeight = 1, cellNum = 0; | ||||
//int indexKey = createIndexCell(row, index, excelParams.get(0)); | |||||
//cellNum += indexKey; | //cellNum += indexKey; | ||||
for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { | for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { | ||||
entity = excelParams.get(k); | entity = excelParams.get(k); | ||||
@@ -127,8 +143,10 @@ public class PdfExportServer extends ExportBase { | |||||
} else { | } else { | ||||
Object value = getCellValue(entity, t); | Object value = getCellValue(entity, t); | ||||
if (entity.getType() == 1) { | if (entity.getType() == 1) { | ||||
table.addCell(value == null ? "" : value.toString()); | |||||
createStringCell(table, value == null ? "" : value.toString(), | |||||
(int) (entity.getHeight() * 2.5)); | |||||
} else { | } else { | ||||
} | } | ||||
} | } | ||||
} | } | ||||
@@ -155,11 +173,116 @@ public class PdfExportServer extends ExportBase { | |||||
private void createHeaderAndTitle(ExportParams entity, PdfPTable table, | private void createHeaderAndTitle(ExportParams entity, PdfPTable table, | ||||
List<ExcelExportEntity> excelParams) throws DocumentException { | List<ExcelExportEntity> 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<ExcelExportEntity> 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<ExcelExportEntity> 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() { | private Font getFont() { | ||||
try { | try { | ||||
//用以支持中文 | //用以支持中文 | ||||
@@ -176,10 +299,4 @@ public class PdfExportServer extends ExportBase { | |||||
return font; | return font; | ||||
} | } | ||||
public Document createPdfForMap(ExportParams entity, List<ExcelExportEntity> entityList, | |||||
Collection<? extends Map<?, ?>> dataSet) { | |||||
return document; | |||||
} | |||||
} | } |