In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to manage Excel and PDF file types based on SpringBoot framework, which is very detailed and has certain reference value. Friends who are interested must read it!
1. Brief introduction of document types 1. Excel documents
Excel is a spreadsheet software. Intuitive interface, excellent computing functions and chart tools are often used to transfer data to Excel files or import Excel data into the system in system development, which involves data conversion.
2. PDF documents
PDF is a portable document format and an electronic file format, which has many advantages over other electronic document formats. PDF file format can encapsulate text, font, format, color and graphics and images independent of device and resolution in one file. The format file can also contain hypertext links, sound, moving images and other electronic information, support long files, high integration and security and reliability.
2. Excel file management 1. POI dependency
Apache POI is an open source class library of the Apache Software Foundation. POI provides API to Java programs to read and write files in Microsoft Office format.
Org.apache.poi poi 3.9 org.apache.poi poi-ooxml 3.92, file reading public static List readExcel (String path) throws Exception {File file = new File (path); List list = new LinkedList (); XSSFWorkbook xwb = new XSSFWorkbook (new FileInputStream (file)); / / read Sheet1 table contents XSSFSheet sheet = xwb.getSheetAt (0) / / read rows: do not read Excel header for (int I = (sheet.getFirstRowNum () + 1); I 0 & & height > 0) {image.scaleAbsolute (width, height);} return image;} / * Table Generation * / public static PdfPTable getPdfPTable01 (int numColumns,float totalWidth) throws Exception {/ / Table processing PdfPTable table = new PdfPTable (numColumns) / / set the width ratio of the table to 0 table.setWidthPercentage; / / set width: average width table.setTotalWidth (totalWidth); / / lock width table.setLockedWidth (true); / / set blank width table.setSpacingBefore (10f) above the table; / / set blank width table.setSpacingAfter (10f) below the table / / sets the table defaults to borderless table.getDefaultCell (). SetBorder (0); table.setPaddingTop (50); table.setSplitLate (false); return table;} / * Table content * / public static PdfPCell getPdfPCell (Phrase phrase) {return new PdfPCell (phrase) } / * Table content with style * / public static void addTableCell (PdfPTable dataTable,Font font,List cellList) {for (String content:cellList) {dataTable.addCell (getParagraph (content,font,-1));} 3. Generate PDF file
Here, based on the above tool class, draw a PDF page as a reference.
Public class PdfPage01 {/ / basic configuration private static String PDF_SITE = "F:\\ file-type\\ PDF Page 2020-01-15.pdf"; private static String FONT = "C:/Windows/Fonts/simhei.ttf"; private static String PAGE_TITLE = "PDF data Export report"; / / basic style private static Font TITLE_FONT = FontFactory.getFont (FONT, BaseFont.IDENTITY_H,20, Font.BOLD) Private static Font NODE_FONT = FontFactory.getFont (FONT, BaseFont.IDENTITY_H,15, Font.BOLD); private static Font BLOCK_FONT = FontFactory.getFont (FONT, BaseFont.IDENTITY_H,13, Font.BOLD, BaseColor.BLACK); private static Font INFO_FONT = FontFactory.getFont (FONT, BaseFont.IDENTITY_H,12, Font.NORMAL,BaseColor.BLACK); private static Font CONTENT_FONT = FontFactory.getFont (FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED) Private static void createPdfPage () throws Exception {/ / create document Document document = new Document (); PdfWriter writer = PdfWriter.getInstance (document, new FileOutputStream (PDF_SITE)); document.open (); / / report title document.add (PdfFontUtil.getParagraph (PAGE_TITLE,TITLE_FONT,1)) Document.add (PdfFontUtil.getParagraph ("\ nMerchant name: XXX Technology Co., Ltd., INFO_FONT,-1); document.add ("\ nGeneration time: 2020-01-15\ n\ n ", INFO_FONT,-1) / / report content / / paragraph title + report chart document.add (PdfFontUtil.getParagraph (Urban data Distribution Statistics, NODE_FONT,-1); document.add (PdfFontUtil.getParagraph ("\ nVisualization Chart\ n\ n", BLOCK_FONT,-1)) / / set image width and height float documentWidth = document.getPageSize (). GetWidth ()-document.leftMargin ()-document.rightMargin (); float documentHeight = documentWidth / 580 * 320; document.add ("F:\\ file-type\\ myChart.jpg", documentWidth-80,documentHeight-80)) / / data table document.add (PdfFontUtil.getParagraph ("\ ndata details\ n\ n", BLOCK_FONT,-1); PdfPTable dataTable = PdfFontUtil.getPdfPTable01 (4400); / / set table List tableHeadList = tableHead (); List tableDataList = getTableData (); PdfFontUtil.addTableCell (dataTable,CONTENT_FONT,tableHeadList) For (List tableData: tableDataList) {PdfFontUtil.addTableCell (dataTable,CONTENT_FONT,tableData);} document.add (dataTable); document.add (PdfFontUtil.getParagraph ("\ n report description\ n\ n", BLOCK_FONT,-1) Document.add (PdfFontUtil.getParagraph) ("data report can monitor daily promotion," + "can analyze different data performance in order to improve the promotion effect." , CONTENT_FONT,-1); document.newPage (); document.close (); writer.close ();} private static List getTableData () {List tableDataList = new ArrayList (); for (int I = 0; I
< 3 ; i++){ List tableData = new ArrayList() ; tableData.add("浙江"+i) ; tableData.add("杭州"+i) ; tableData.add("276"+i) ; tableData.add("33.3%") ; tableDataList.add(tableData) ; } return tableDataList ; } private static List tableHead (){ List tableHeadList = new ArrayList() ; tableHeadList.add("省份") ; tableHeadList.add("城市") ; tableHeadList.add("数量") ; tableHeadList.add("百分比") ; return tableHeadList ; } public static void main(String[] args) throws Exception { createPdfPage () ; }}4、页面效果 四、网页转PDF1、页面Jar包依赖 org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-freemarker2、编写页面样式 Title body{font-family:SimSun;} 项目信息:名称:${name}作者:${author}3. Core configuration class public class PageConfig {private static final String DEST = "F:\\ file-type\\ HTML page 2020-01-15.pdf"; private static final String HTML = "/ pdf_page_one.html"; private static final String FONT = "C:/Windows/Fonts/simsun.ttc"; private static Configuration freemarkerCfg = null; static {freemarkerCfg = new Configuration (Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS) / freemarker template directory try {String path = "TODO: template path {custom}"; freemarkerCfg.setDirectoryForTemplateLoading (new File (path));} catch (IOException e) {e.printStackTrace () }} / * create document * / private static void createPdf (String content,String dest) throws Exception {Document document = new Document (); PdfWriter writer = PdfWriter.getInstance (document, new FileOutputStream (dest)); document.open (); XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider (XMLWorkerFontProvider.DONTLOOKFORFONTS); fontImp.register (FONT) XMLWorkerHelper.getInstance () .parseXHtml (writer, document, new ByteArrayInputStream (content.getBytes ()), null, Charset.forName ("UTF-8"), fontImp); document.close ();} / * Page rendering * / private static String freeMarkerRender (Map data, String htmlTmp) throws Exception {Writer out = new StringWriter (); Template template = freemarkerCfg.getTemplate (htmlTmp, "UTF-8") Template.process (data, out); out.flush (); out.close (); return out.toString ();} / * method entry * / public static void main (String [] args) throws Exception {Map data = new HashMap (); data.put ("name", "smile"); data.put ("author", "cicada") String content = PageConfig.freeMarkerRender (data,HTML); PageConfig.createPdf (content,DEST);}} these are all the contents of the article "how to manage Excel and PDF file types based on the SpringBoot framework". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.