当前位置: 首页 > wzjs >正文

电子商城平台网站建设优化推广

电子商城平台网站建设,优化推广,招投标网站销售怎么做,建设招标网官网需求 最近需要开发一个功能,涉及到Excel导出与pdf导出,其实pdf导出不适合表头太多的表格,不美观,但是需求如此,那就开发吧。 技术选型 参考了网上的资料,选用itextpdf做为技术支持,开始itext…

需求

最近需要开发一个功能,涉及到Excel导出与pdf导出,其实pdf导出不适合表头太多的表格,不美观,但是需求如此,那就开发吧。

技术选型

参考了网上的资料,选用itextpdf做为技术支持,开始itextpdf-core对应的依赖,但是导出中文会有格式问题,并且乱码,需要引入本地的字体。

        <dependency><groupId>com.itextpdf</groupId><artifactId>itext7-core</artifactId><version>7.1.16</version><type>pom</type></dependency>
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.UnitValue;
import com.travelsky.commons.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;import javax.servlet.http.HttpServletResponse;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;/*** @Author maruko* @Date 2023/12/15 10:05* @Description: 导出pdf添加页数* @Version 1.0*/
@Slf4j
public class PdfPageUtil {/*** 下载导出为PDF* @param fileName 文件名* @param headers 表头 注意中英文* @param data 数据集合  实体类可采用entityToList转换* @param response* @throws Exception*/public static void downloadPdf(String fileName, List<String> headers, List<List<String>> data, HttpServletResponse response) throws Exception {// 设置编码格式response.setContentType("application/pdf;charset=UTF-8");response.setCharacterEncoding("utf-8");fileName = URLEncoder.encode(fileName, "UTF-8");response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".pdf");PdfWriter writer = new PdfWriter(response.getOutputStream());PdfDocument pdfDoc = new PdfDocument(writer);Document document = new Document(pdfDoc);URL resource = PdfPageUtil.class.getClassLoader().getResource("simhei.ttf");String path = resource.getPath();PdfFont font = PdfFontFactory.createFont(path, PdfEncodings.IDENTITY_H, true);//根据表头数量设置列宽比例和宽度为页面宽度float[] columnWidths = new float[headers.size()];for (int i = 0; i < headers.size(); i++) {columnWidths[i] = 1;}// 创建表格并添加数据和表头Table table = new Table(UnitValue.createPercentArray(columnWidths)).useAllAvailableWidth();// 添加表头单元格for (String header : headers) {if (StringUtil.isEmpty(header)) {table.addHeaderCell("");} else {table.addHeaderCell(header).setFont(font);}}// 添加数据单元格到表格中for (List<String> rowData : data) {for (String cell : rowData) {if (StringUtil.isEmpty(cell)) {table.addCell("");} else {table.addCell(cell).setFont(font);}}}document.add(table);document.close();}public static List<List<String>> entityToList(List<?> list, String[] fields, Class<?> classType) {List<List<String>> dataList = new ArrayList<>();String temp = JSONObject.toJSONString(list);List<?> objects = JSONArray.parseArray(temp, classType);for (Object object : objects) {Map<String, String> map = JSONObject.parseObject(JSONObject.toJSONString(object),new TypeReference<Map<String, String>>() {});List<String> tempList = new ArrayList<>();for (String field : fields) {tempList.add(map.get(field));}dataList.add(tempList);}return dataList;}
}

问题

上述实现,对于中文有一定问题,还无法合并单元格,所以换了其它依赖

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

实现方式


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.travelsky.domain.pdf.PdfHeader;
import lombok.extern.slf4j.Slf4j;import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;/*** @Author maruko* @Date 2023/12/15 10:05* @Description: 导出pdf添加页数* @Version 1.0*/
@Slf4j
public class PdfPageUtil {public static void main(String[] args) throws Exception {}/*** 下载导出为PDF** @param fileName   文件名* @param headerSize 表头数量* @param headers    表头 注意中英文* @param data       数据集合  实体类可采用entityToList转换* @param response   HttpServletResponse* @throws Exception ex*/public static void downloadPdf(String fileName, int headerSize, List<List<PdfHeader>> headers, List<List<String>> data, HttpServletResponse response) throws Exception {// 设置编码格式response.setContentType("application/pdf;charset=UTF-8");response.setCharacterEncoding("utf-8");fileName = URLEncoder.encode(fileName, "UTF-8");response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".pdf");Document document = new Document();PdfWriter pdfWriter = PdfWriter.getInstance(document, response.getOutputStream());pdfWriter.setViewerPreferences(PdfWriter.PageModeUseThumbs);//设置A4document.setPageSize(PageSize.A4);document.open();// 创建表格并添加数据和表头 采用百分比模式 不用固定长度PdfPTable table = new PdfPTable(headerSize);table.setWidthPercentage(100.0F);// 添加表头单元格for (List<PdfHeader> header : headers) {for (PdfHeader pdfHeader : header) {fillHeaderCell(pdfHeader, getPdfChineseFont(), table);}}// 添加数据单元格到表格中for (List<String> rowData : data) {for (String cell : rowData) {fillCell(cell, getPdfChineseFont(), table);}}document.add(table);document.close();}/*** 填充单元格** @param header PdfRowHeader* @param font   Font* @param table  PdfPTable*/private static void fillHeaderCell(PdfHeader header, Font font, PdfPTable table) {PdfPCell headerCell = new PdfPCell();//不为0才有合并列if (header.getRowSpan() > 0) {headerCell.setRowspan(header.getRowSpan());}if (header.getColSpan() > 0) {headerCell.setColspan(header.getColSpan());}headerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);headerCell.setFixedHeight(30);Paragraph paragraph = new Paragraph(header.getContent(), font);headerCell.setPhrase(paragraph);table.addCell(headerCell);}/*** 填充单元格** @param content String* @param font    Font* @param table   PdfPTable*/private static void fillCell(String content, Font font, PdfPTable table) {PdfPCell headerCell = new PdfPCell(new Paragraph(content, font));headerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);headerCell.setFixedHeight(30);table.addCell(headerCell);}/*** 实体类转换成List** @param list       数据集合* @param fields     字段* @param classType  实体类* @return List<List < String>>*/public static List<List<String>> entityToList(List<?> list, String[] fields, Class<?> classType) {List<List<String>> dataList = new ArrayList<>();String temp = JSONObject.toJSONString(list);List<?> objects = JSONArray.parseArray(temp, classType);for (Object object : objects) {Map<String, String> map = JSONObject.parseObject(JSONObject.toJSONString(object),new TypeReference<Map<String, String>>() {});List<String> tempList = new ArrayList<>();for (String field : fields) {tempList.add(map.get(field));}dataList.add(tempList);}return dataList;}/*** 获取中文字体** @return Font* @throws Exception ex*/private static Font getPdfChineseFont() throws Exception {BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);return new Font(bfChinese, 12, Font.NORMAL);}
}
http://www.dtcms.com/wzjs/109908.html

相关文章:

  • 新手怎么开网店百度整站优化
  • 海林建设局网站网站推广的几种方法
  • wordpress 访问人数防控措施持续优化
  • 哪里网站可以做微信头像网站自动提交收录
  • 免费crm网站下载的软件网站推广什么意思
  • web网站开发部署百度网站介绍
  • 外贸网站代码成都网站搜索排名优化公司
  • 做维修广告效最好是哪个网站吗最新的新闻 今天
  • 南昌教育网站建设千锋教育培训机构地址
  • 奢侈品网站建设方案今日新闻头条
  • 网站的文件结构网站运营是做什么的
  • 网上花钱做ppt的网站seo快速优化排名
  • 做门的网站建设seo黑帽教程视频
  • 佛山做pc端网站韩国网站
  • 设计作品展示网站百度开户流程
  • 三明seo郑州seo外包费用
  • 你愿意做我女朋友吗表白网站正规引流推广公司
  • 重庆网站建设公司怎么做百度竞价推广代运营公司
  • 网站运行维护方案做企业推广
  • 网站怎么做的支付宝接口农产品网络营销推广方案
  • 页面设计毕业论文8000字网站优化怎么操作
  • django做视频网站抖音seo优化公司
  • php网站开发视频教程关键词排名优化公司
  • 如何看网站做没做推广百度公司注册地址在哪里
  • 开发网站设计南京seo公司排名
  • 海岸城网站建设广州seo公司排行
  • 广州荔湾建网站的公司江苏免费关键词排名外包
  • wordpress 4.8 中文sem和seo有什么区别
  • 首钢建设二公司网站如何免费找精准客户
  • pc网站 公众号数据互通制定营销推广方案