pdf导出工具类

阅读: 评论:0

pdf导出工具类

pdf导出工具类

(目前只更新导出pdf表格)

导出pdf表格

1、依赖:

<dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13</version>
</dependency>
<dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version>
</dependency>

2、工具类:

ption.BusinessException;
import *;
import pdf.BaseFont;
import pdf.PdfPCell;
import pdf.PdfPTable;
import pdf.PdfWriter;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import flect.Field;
import java.URLEncoder;
SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;/*** @Filename: PdfUtils* @Author: sheng.wanping* <li>Date: 2022/8/1 15:40</li>* <li>Version: 1.0</li>* <li>Content: create</li>*/
public class PdfUtils {// 定义全局的字体静态变量private static Font titlefont;private static Font textfont;static {try {BaseFont bfChinese = ateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);titlefont = new Font(bfChinese, 8, Font.BOLD);textfont = new Font(bfChinese, 8, Font.NORMAL);} catch (Exception e) {e.printStackTrace();}}/*** @Filename: PdfUtils pdf工具类* @Author: sheng.wanping* <li>Date: 2022/8/1 15:40</li>* <li>Version: 1.0</li>* <li>Content: create</li>*/
public class PdfUtils {/*** 用于返回 简单列表PDF表格* @param response* @param tableName 表名* @param list 数据列表* @param map 中英文map(key为标题,value为字段英文名;注意:map是有顺序的,可用LinkedHashMap)* @param <T>*/public static  <T> void pdfSingleTable(HttpServletResponse response, String tableName, List<T> list, Map<String, String> map) {// 1、从map中获取字段中文名和英文名List<String> valueList = new ArrayList<>(); // 存表格所有数据List<String> fieldEnList = new ArrayList<>(); // 字段英文名for (Map.Entry<String, String> entry : Set()) {valueList.Key());fieldEnList.Value());}// 2、用反射根据字段名获取值for (int i = 0; i < list.size(); i++) {for (int j = 0; j < fieldEnList.size(); j++) {Field field = null;String strValue = "";try {field = (i).getClass().(j));field.setAccessible(true);Object value = ((i));if (value != null) {if (value instanceof Date) {strValue = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value);} else {strValue = String.valueOf(value);}} else {strValue = "";}} catch (NoSuchFieldException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} finally {valueList.add(strValue);// 字段值}}}// 3、返回pdftry {// 3-1.新建document对象Document document = new Document(PageSize.A4);// 3-2.建立一个书写器response.setHeader("Content-Type", "application/pdf");response.setHeader("Content-Disposition", "attachment;filename=" + de(tableName+".pdf", "UTF-8"));ServletOutputStream out = OutputStream();Instance(document, out);// 3-3.打开文档document.open();// 3-4、查询数据库数据,并存入list中// 3-5、设置单元格宽度List<Integer> mergeCellList = new ArrayList<>();int idx = 0;while (idx < (list.size() + 1) * fieldEnList.size()) {idx++;mergeCellList.add(1);}// 3-6.创建表格PdfPTable table = new PdfPTable(fieldEnList.size());for (int i = 0; i < mergeCellList.size(); i++) {PdfPCell cell = new PdfPCell(); // 创建行cell.(i)); // 合并单元格BaseFont bfChinese = ateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);cell.setPhrase(new (i), new Font(bfChinese, 8, Font.NORMAL)));cell.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中table.addCell(cell); // 添加单元格内容}// 3-7、向文档中添加内容document.add(table);// 3-8、关闭文档document.close();out.close();}catch (Exception e){e.printStackTrace();throw new BusinessException("导出pdf失败");}}/*** 返回一个字节数组输出流* @param list 数据列表* @param map 中英文map(key为标题,value为字段英文名;注意:map是有顺序的,可用LinkedHashMap)* @param <T>*/public static  <T> ByteArrayOutputStream getByteArray(List<T> list, Map<String, String> map) {// 1、从map中获取字段中文名和英文名List<String> valueList = new ArrayList<>(); // 存表格所有数据List<String> fieldEnList = new ArrayList<>(); // 字段英文名for (Map.Entry<String, String> entry : Set()) {valueList.Key());fieldEnList.Value());}// 2、用反射根据字段名获取值for (int i = 0; i < list.size(); i++) {for (int j = 0; j < fieldEnList.size(); j++) {Field field = null;String strValue = "";try {field = (i).getClass().(j));field.setAccessible(true);Object value = ((i));if (value != null) {if (value instanceof Date) {strValue = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value);} else {strValue = String.valueOf(value);}} else {strValue = "";}} catch (NoSuchFieldException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} finally {valueList.add(strValue);// 字段值}}}// 3、返回pdftry {// 3-1.新建document对象Document document = new Document(PageSize.A4);// 2.建立一个书写器ByteArrayOutputStream os = new ByteArrayOutputStream();Instance(document, os);// 3-3.打开文档document.open();// 3-4、查询数据库数据,并存入list中// 3-5、设置单元格宽度List<Integer> mergeCellList = new ArrayList<>();int idx = 0;while (idx < (list.size() + 1) * fieldEnList.size()) {idx++;mergeCellList.add(1);}// 3-6.创建表格PdfPTable table = new PdfPTable(fieldEnList.size());for (int i = 0; i < mergeCellList.size(); i++) {PdfPCell cell = new PdfPCell(); // 创建行cell.(i)); // 合并单元格BaseFont bfChinese = ateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);cell.setPhrase(new (i), new Font(bfChinese, 8, Font.NORMAL)));cell.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中table.addCell(cell); // 添加单元格内容}// 3-7、向文档中添加内容document.add(table);document.close();return os;}catch (Exception e){e.printStackTrace();throw new BusinessException("导出pdf失败");}}}

3、测试代码

	private static final Map<String, String> BUDGE_MAP = new LinkedHashMap<>();static {BUDGE_MAP.put("主设备", "mainEquipmentExpense");BUDGE_MAP.put("天线", "aerialExpense");BUDGE_MAP.put("电源", "powerExpense");}public void testPdf(HttpServletResponse response) {List<Object> list = Data;// 查询数据列表PdfUtils.pdfSingleTable(response, "表名", list, BUDGE_MAP);// 导出pdf表格}

本文发布于:2024-02-02 09:56:30,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170683898943033.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:工具   pdf
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23