Browse Source

可以多行模板输出和样式自定义了

4.1.3.A
jueyue 9 years ago
parent
commit
677cff5c8e
5 changed files with 368 additions and 111 deletions
  1. +7
    -6
      easypoi-base/src/main/java/org/jeecgframework/poi/pdf/PdfExportUtil.java
  2. +135
    -0
      easypoi-base/src/main/java/org/jeecgframework/poi/pdf/entity/PdfExportParams.java
  3. +105
    -105
      easypoi-base/src/main/java/org/jeecgframework/poi/pdf/export/PdfExportServer.java
  4. +51
    -0
      easypoi-base/src/main/java/org/jeecgframework/poi/pdf/styler/IPdfExportStyler.java
  5. +70
    -0
      easypoi-base/src/main/java/org/jeecgframework/poi/pdf/styler/PdfExportStylerDefaultImpl.java

+ 7
- 6
easypoi-base/src/main/java/org/jeecgframework/poi/pdf/PdfExportUtil.java View File

@@ -20,8 +20,8 @@ import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.jeecgframework.poi.pdf.entity.PdfExportParams;
import org.jeecgframework.poi.pdf.export.PdfExportServer;
import com.itextpdf.text.Document;
@@ -44,9 +44,9 @@ public class PdfExportUtil {
* @param dataSet
* PDF对象数据List
*/
public static Document exportPdf(ExportParams entity, Class<?> pojoClass, Collection<?> dataSet,
OutputStream outStream) {
return new PdfExportServer(outStream).createPdf(entity, pojoClass, dataSet);
public static Document exportPdf(PdfExportParams entity, Class<?> pojoClass,
Collection<?> dataSet, OutputStream outStream) {
return new PdfExportServer(outStream, entity).createPdf(entity, pojoClass, dataSet);
}
/**
@@ -58,11 +58,12 @@ public class PdfExportUtil {
* @param dataSet
* PDF对象数据List
*/
public static Document exportPdf(ExportParams entity, List<ExcelExportEntity> entityList,
public static Document exportPdf(PdfExportParams entity, List<ExcelExportEntity> entityList,
Collection<? extends Map<?, ?>> dataSet,
OutputStream outStream) {
return new PdfExportServer(outStream).createPdfByExportEntity(entity, entityList, dataSet);
return new PdfExportServer(outStream, entity).createPdfByExportEntity(entity, entityList,
dataSet);
}
}

+ 135
- 0
easypoi-base/src/main/java/org/jeecgframework/poi/pdf/entity/PdfExportParams.java View File

@@ -0,0 +1,135 @@
/**
* Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jeecgframework.poi.pdf.entity;
import org.jeecgframework.poi.excel.entity.ExcelBaseParams;
import org.jeecgframework.poi.pdf.styler.IPdfExportStyler;
/**
* PDF 导出参数设置
* @author JueYue
* @date 2016年1月8日 下午1:52:06
*/
public class PdfExportParams extends ExcelBaseParams {
/**
* 表格名称
*/
private String title;
/**
* 表格名称
*/
private short titleHeight = 30;
/**
* 第二行名称
*/
private String secondTitle;
/**
* 表格名称
*/
private short secondTitleHeight = 25;
/**
* 过滤的属性
*/
private String[] exclusions;
/**
* 是否添加需要需要
*/
private boolean addIndex;
/**
* 是否添加需要需要
*/
private String indexName = "序号";
private IPdfExportStyler styler;
public PdfExportParams() {
}
public PdfExportParams(String title, String secondTitle) {
this.title = title;
this.secondTitle = secondTitle;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public short getTitleHeight() {
return titleHeight;
}
public void setTitleHeight(short titleHeight) {
this.titleHeight = titleHeight;
}
public String getSecondTitle() {
return secondTitle;
}
public void setSecondTitle(String secondTitle) {
this.secondTitle = secondTitle;
}
public short getSecondTitleHeight() {
return secondTitleHeight;
}
public void setSecondTitleHeight(short secondTitleHeight) {
this.secondTitleHeight = secondTitleHeight;
}
public String[] getExclusions() {
return exclusions;
}
public void setExclusions(String[] exclusions) {
this.exclusions = exclusions;
}
public boolean isAddIndex() {
return addIndex;
}
public void setAddIndex(boolean addIndex) {
this.addIndex = addIndex;
}
public String getIndexName() {
return indexName;
}
public void setIndexName(String indexName) {
this.indexName = indexName;
}
public IPdfExportStyler getStyler() {
return styler;
}
public void setStyler(IPdfExportStyler styler) {
this.styler = styler;
}
}

+ 105
- 105
easypoi-base/src/main/java/org/jeecgframework/poi/pdf/export/PdfExportServer.java View File

@@ -15,7 +15,6 @@
*/
package org.jeecgframework.poi.pdf.export;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
@@ -25,9 +24,11 @@ import java.util.List;
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;
import org.jeecgframework.poi.excel.export.base.ExportBase;
import org.jeecgframework.poi.pdf.entity.PdfExportParams;
import org.jeecgframework.poi.pdf.styler.IPdfExportStyler;
import org.jeecgframework.poi.pdf.styler.PdfExportStylerDefaultImpl;
import org.jeecgframework.poi.util.PoiPublicUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -35,13 +36,8 @@ 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;
@@ -52,13 +48,18 @@ import com.itextpdf.text.pdf.PdfWriter;
*/
public class PdfExportServer extends ExportBase {
private static final Logger LOGGER = LoggerFactory.getLogger(PdfExportServer.class);
private static final Logger LOGGER = LoggerFactory.getLogger(PdfExportServer.class);
private Document document;
private IPdfExportStyler styler;
public PdfExportServer(OutputStream outStream) {
private boolean isListData = false;
public PdfExportServer(OutputStream outStream, PdfExportParams entity) {
try {
document = new Document(PageSize.A4, 36, 36, 24, 36);
styler = entity.getStyler() == null ? new PdfExportStylerDefaultImpl()
: entity.getStyler();
document = styler.getDocument();
PdfWriter.getInstance(document, outStream);
document.open();
} catch (Exception e) {
@@ -73,11 +74,11 @@ public class PdfExportServer extends ExportBase {
* @param dataSet
* @return
*/
public Document createPdf(ExportParams entity, Class<?> pojoClass, Collection<?> dataSet) {
public Document createPdf(PdfExportParams entity, Class<?> pojoClass, Collection<?> dataSet) {
try {
List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>();
if (entity.isAddIndex()) {
excelParams.add(indexExcelEntity(entity));
//excelParams.add(indexExcelEntity(entity));
}
// 得到所有字段
Field fileds[] = PoiPublicUtil.getClassFields(pojoClass);
@@ -94,11 +95,16 @@ public class PdfExportServer extends ExportBase {
return document;
}
public Document createPdfByExportEntity(ExportParams entity,
public Document createPdfByExportEntity(PdfExportParams entity,
List<ExcelExportEntity> excelParams,
Collection<?> dataSet) {
try {
sortAllParams(excelParams);
for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) {
if (excelParams.get(k).getList() != null) {
isListData = true;
}
}
//设置各个列的宽度
float[] widths = getCellWidths(excelParams);
PdfPTable table = new PdfPTable(widths.length);
@@ -106,7 +112,7 @@ public class PdfExportServer extends ExportBase {
//table.setLockedWidth(true);
//设置表头
createHeaderAndTitle(entity, table, excelParams);
short rowHeight = getRowHeight(excelParams);
int rowHeight = getRowHeight(excelParams) / 50;
Iterator<?> its = dataSet.iterator();
while (its.hasNext()) {
Object t = its.next();
@@ -122,29 +128,21 @@ public class PdfExportServer extends ExportBase {
}
private void createCells(PdfPTable table, Object t, List<ExcelExportEntity> excelParams,
short rowHeight) throws Exception {
int rowHeight) throws Exception {
ExcelExportEntity entity;
int maxHeight = 1, cellNum = 0;
//cellNum += indexKey;
int maxHeight = getThisMaxHeight(t, excelParams);
for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) {
entity = excelParams.get(k);
if (entity.getList() != null) {
Collection<?> list = getListCellValue(entity, t);
int listC = 0;
for (Object obj : list) {
//createListCells(patriarch, index + listC, cellNum, obj, entity.getList(), sheet,
// workbook);
listC++;
}
cellNum += entity.getList().size();
if (list != null && list.size() > maxHeight) {
maxHeight = list.size();
createListCells(table, obj, entity.getList(), rowHeight);
}
} else {
Object value = getCellValue(entity, t);
if (entity.getType() == 1) {
createStringCell(table, value == null ? "" : value.toString(),
(int) (entity.getHeight() * 2.5));
createStringCell(table, value == null ? "" : value.toString(), entity,
rowHeight, 1, maxHeight);
} else {
}
@@ -152,6 +150,56 @@ public class PdfExportServer extends ExportBase {
}
}
/**
* 创建集合对象
* @param table
* @param obj
* @param rowHeight
* @param list
* @throws Exception
*/
private void createListCells(PdfPTable table, Object obj, List<ExcelExportEntity> excelParams,
int rowHeight) throws Exception {
ExcelExportEntity entity;
for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) {
entity = excelParams.get(k);
Object value = getCellValue(entity, obj);
if (entity.getType() == 1) {
createStringCell(table, value == null ? "" : value.toString(), entity, rowHeight);
} else {
}
}
}
/**
* 获取这一列的高度
* @param t 对象
* @param excelParams 属性列表
* @return
* @throws Exception 通过反射过去值得异常
*/
private int getThisMaxHeight(Object t, List<ExcelExportEntity> excelParams) throws Exception {
if (isListData) {
ExcelExportEntity entity;
int maxHeight = 1;
for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) {
entity = excelParams.get(k);
if (entity.getList() != null) {
Collection<?> list = getListCellValue(entity, t);
maxHeight = (list == null || maxHeight > list.size()) ? maxHeight : list.size();
}
}
return maxHeight;
}
return 1;
}
/**
* 获取Cells的宽度数组
* @param excelParams
* @return
*/
private float[] getCellWidths(List<ExcelExportEntity> excelParams) {
List<Float> widths = new ArrayList<Float>();
for (int i = 0; i < excelParams.size(); i++) {
@@ -171,7 +219,7 @@ public class PdfExportServer extends ExportBase {
return widthArr;
}
private void createHeaderAndTitle(ExportParams entity, PdfPTable table,
private void createHeaderAndTitle(PdfExportParams entity, PdfPTable table,
List<ExcelExportEntity> excelParams) throws DocumentException {
int feildWidth = getFieldLength(excelParams);
if (entity.getTitle() != null) {
@@ -186,78 +234,52 @@ public class PdfExportServer extends ExportBase {
* @param title
* @param index
*/
private int createTitleRow(ExportParams title, PdfPTable table,
private int createTitleRow(PdfExportParams 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++)
{
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());
createStringCell(table, entity.getName(), entity, 10, entity.getList().size(),
1);
}
List<ExcelExportEntity> sTitel = entity.getList();
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++;
createStringCell(table, sTitel.get(j).getName(), sTitel.get(j), 10);
}
cellIndex--;
} else if (rows == 2) {
iCell.setRowspan(2);
//把合并后的边框去掉
iCell.setBorderWidthBottom(0);
listRow.getCells()[cellIndex].setBorderWidthTop(0);
} else {
createStringCell(table, entity.getName(), entity, 10, 1, rows == 2 ? 2 : 1);
}
cellIndex++;
}
return rows;
}
private void createHeaderRow(ExportParams entity, PdfPTable table, int feildLength) {
createStringCell(table, entity.getTitle(), entity.getTitleHeight() / 20, feildLength + 1,
1);
private void createHeaderRow(PdfExportParams entity, PdfPTable table, int feildLength) {
PdfPCell iCell = new PdfPCell(
new Phrase(entity.getTitle(), styler.getFont(null, entity.getTitle())));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_CENTER);
iCell.setFixedHeight(entity.getTitleHeight());
iCell.setColspan(feildLength + 1);
table.addCell(iCell);
if (entity.getSecondTitle() != null) {
PdfPCell iCell = new PdfPCell(new Phrase(entity.getSecondTitle(), getFont()));
iCell = new PdfPCell(
new Phrase(entity.getSecondTitle(), styler.getFont(null, entity.getSecondTitle())));
iCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
iCell.setVerticalAlignment(Element.ALIGN_CENTER);
iCell.setFixedHeight(entity.getSecondTitleHeight() / 20);
iCell.setFixedHeight(entity.getSecondTitleHeight());
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);
private PdfPCell createStringCell(PdfPTable table, String text, ExcelExportEntity entity,
int rowHeight, int colspan, int rowspan) {
PdfPCell iCell = new PdfPCell(new Phrase(text, styler.getFont(entity, text)));
styler.setCellStyler(iCell, entity, text);
iCell.setFixedHeight((int) (rowHeight * 2.5));
if (colspan > 1) {
iCell.setColspan(colspan);
}
@@ -268,35 +290,13 @@ public class PdfExportServer extends ExportBase {
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);
private PdfPCell createStringCell(PdfPTable table, String text, ExcelExportEntity entity,
int rowHeight) {
PdfPCell iCell = new PdfPCell(new Phrase(text, styler.getFont(entity, text)));
styler.setCellStyler(iCell, entity, text);
iCell.setFixedHeight((int) (rowHeight * 2.5));
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 {
//用以支持中文
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
Font font = new Font(bfChinese);
return font;
} catch (DocumentException e) {
LOGGER.error(e.getMessage(), e);
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
Font font = new Font(FontFamily.UNDEFINED);
return font;
}
}

+ 51
- 0
easypoi-base/src/main/java/org/jeecgframework/poi/pdf/styler/IPdfExportStyler.java View File

@@ -0,0 +1,51 @@
/**
* Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jeecgframework.poi.pdf.styler;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.PdfPCell;
/**
* PDF导出样式设置
* @author JueYue
* @date 2016年1月7日 下午11:16:51
*/
public interface IPdfExportStyler {
/**
* 获取文档格式
* @return
*/
public Document getDocument();
/**
* 设置Cell的样式
* @param entity
* @param text
*/
public void setCellStyler(PdfPCell iCell, ExcelExportEntity entity, String text);
/**
* 获取字体
* @param entity
* @param text
*/
public Font getFont(ExcelExportEntity entity, String text);
}

+ 70
- 0
easypoi-base/src/main/java/org/jeecgframework/poi/pdf/styler/PdfExportStylerDefaultImpl.java View File

@@ -0,0 +1,70 @@
/**
* Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jeecgframework.poi.pdf.styler;
import java.io.IOException;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.slf4j.Logger;
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.PageSize;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
/**
* 默认的PDFstyler 实现
* @author JueYue
* @date 2016年1月8日 下午2:06:26
*/
public class PdfExportStylerDefaultImpl implements IPdfExportStyler {
private static final Logger LOGGER = LoggerFactory.getLogger(PdfExportStylerDefaultImpl.class);
@Override
public Document getDocument() {
return new Document(PageSize.A4, 36, 36, 24, 36);
}
@Override
public void setCellStyler(PdfPCell iCell, ExcelExportEntity entity, String text) {
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
@Override
public Font getFont(ExcelExportEntity entity, String text) {
try {
//用以支持中文
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
Font font = new Font(bfChinese);
return font;
} catch (DocumentException e) {
LOGGER.error(e.getMessage(), e);
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
Font font = new Font(FontFamily.UNDEFINED);
return font;
}
}

Loading…
Cancel
Save