Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does the SpringBoot framework manage Excel and PDF file types

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how the SpringBoot framework manages Excel and PDF file types". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

A brief introduction to 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.

II. 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.9

2. 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 to 0 table.setWidthPercentage; / / set width: average width table.setTotalWidth (totalWidth); / / Lock width table.setLockedWidth (true); / / set blank width table.setSpacingBefore (10f) / / set the blank width below the table table.setSpacingAfter (10f); / / set the table default to no border table.getDefaultCell (). SetBorder (0); table.setPaddingTop (50); table.setSplitLate (false); return table;} / * * Table contents * / 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 (PdfFontUtil.getParagraph ("\ 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 (PdfFontUtil.getImage ("F:\\ file-type\\ myChart.jpg", documentWidth-80,documentHeight-80)); / / data table document.add (PdfFontUtil.getParagraph ("\ 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\ 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 () ; }} 四、网页转PDF 1、页面Jar包依赖 org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-freemarker 2、编写页面样式 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);}}

That's all for "how the SpringBoot Framework manages Excel and PDF file types". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report