Java純代碼實現(xiàn)導(dǎo)出pdf合并單元格
java 純代碼導(dǎo)出pdf合并單元格
接上篇博客java導(dǎo)出pdf(純代碼實現(xiàn))后有一部分猿友叫我提供一下源碼,實際上我的源碼已經(jīng)貼在帖子上了,都是同樣的步驟,只是加多一點設(shè)置就可以了。今天我再次上傳一下相對情況比較完整導(dǎo)出PDF的場景,包含列表,合并單元格,設(shè)置邊框等,具體請先看效果圖:
注:次效果圖僅供參考,內(nèi)容均為測試數(shù)據(jù)不具有任何意義。
廢話不多說,直接上源碼:
@SneakyThrows @PostMapping("/download") @ApiOperation(value = "模板下載") public void download(@RequestBody TemplateDownloadDTO downloadDTO, HttpServletRequest request, HttpServletResponse response){ //該導(dǎo)出僅針對一條數(shù)據(jù)故要傳id確定數(shù)據(jù) Assert.notNull(downloadDTO.getId(),"id必傳"); request.getSession(); String fileName = "文件名稱"; PdfUtil.setResponseContentType(response,fileName); stockOutService.download(downloadDTO,response); }
以下為導(dǎo)出PDF頭部設(shè)置,具體在另外一個帖子中有
public static void setResponseContentType(HttpServletResponse response, String fileName) throws UnsupportedEncodingException { response.setContentType("application/pdf"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8") + ".pdf"); response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); }
以下是邏輯部分
/** * 調(diào)運明細模板下載 * * @param downloadDTO 入?yún)? * @param response 返回 */ @Override public void download(TemplateDownloadDTO downloadDTO, HttpServletResponse response) { //你自己的查詢數(shù)據(jù)的邏輯部分,我這里做了刪減不展示 List<StockOutDtlVO> vos = BeanUtils.copyListPropertiesByClass(list, StockOutDtlVO.class); vo.setDtls(vos); //定義全局字體靜態(tài)變量 Font titlefont; Font headfont = null; Font headfont1 = null; Font keyfont = null; Font textfont = null; Font textfont1 = null; Font content = null; Font space = null; Font space1 = null; Font space2 = null; Font space3 = null; //最大寬度 try { BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); titlefont = new Font(font, 16, Font.BOLD); //四號 headfont = new Font(font, 14, Font.BOLD); headfont1 = new Font(font, 14, Font.NORMAL); //三號 content = new Font(font, 16, Font.NORMAL); //小四 textfont = new Font(font, 11, Font.BOLD); textfont1 = new Font(font, 11, Font.NORMAL); space = new Font(font, 2, Font.NORMAL); space1 = new Font(font, 10, Font.NORMAL); space2 = new Font(font, 30, Font.NORMAL); space3 = new Font(font, 20, Font.NORMAL); } catch (Exception e) { e.printStackTrace(); } BaseFont bf; Font font = null; try { //創(chuàng)建字體 bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); font = new Font(bf, 22, Font.BOLD, BaseColor.BLACK); } catch (Exception e) { e.printStackTrace(); } Document document = new Document(new Rectangle(PageSize.A4)); //設(shè)置PDF工作區(qū)上下左右和紙張的邊距 document.setMargins(60, 60, 72, 72); try { PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream()); //頁碼,具體詳見另外一篇帖子 writer.setPageEvent(new PdfPageUtil()); document.open(); Paragraph paragraph = new Paragraph("深圳市市級救災(zāi)物資調(diào)運明細表", font); paragraph.setAlignment(Element.ALIGN_CENTER); document.add(paragraph); document.add(new Paragraph("\n", space1)); Paragraph paragraph1 = new Paragraph(CharSequenceUtil.format("編號:{}", vo.getDjbh()), headfont1); paragraph1.setAlignment(Element.ALIGN_RIGHT); document.add(paragraph1); document.add(new Paragraph("\n", space)); float[] widths = {25f, 25f, 25f, 25f, 25f, 25f}; PdfPTable table = new PdfPTable(widths); table.setSpacingBefore(20f); table.setWidthPercentage(100.0f); table.setHeaderRows(Element.ALIGN_CENTER); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); PdfPCell cell = null; //第一行 cell = new PdfPCell(new Paragraph("調(diào)出單位", textfont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setFixedHeight(55); table.addCell(cell); cell = new PdfPCell(new Paragraph(vo.getDcdwmc(), textfont1)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph("接收單位", textfont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph(vo.getJsdwmc(), textfont1)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph("儲備服務(wù)單位", textfont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph("XXXX", textfont1)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph("調(diào)出地點", textfont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setFixedHeight(55); table.addCell(cell); cell = new PdfPCell(new Paragraph(vo.getDckdmc(), textfont1)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph("接收地點", textfont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph(vo.getPsdz(), textfont1)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph("車牌號", textfont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph(null, textfont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph("聯(lián)系人", textfont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setFixedHeight(55); table.addCell(cell); cell = new PdfPCell(new Paragraph("聯(lián)系人", textfont1)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph("聯(lián)系人", textfont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph(vo.getJsdwlxr(), textfont1)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph("聯(lián)系人\n(司機)", textfont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph(null, textfont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph("聯(lián)系電話", textfont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setFixedHeight(40); table.addCell(cell); cell = new PdfPCell(new Paragraph("1300000000", textfont1)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph("聯(lián)系電話", textfont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph(vo.getJsdwlxrdh(), textfont1)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph("聯(lián)系人電話", textfont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph(null, textfont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph("車型及運輸費用結(jié)算", textfont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setFixedHeight(55); table.addCell(cell); String concent = CharSequenceUtil.format("車型:{} \n\n 結(jié)算方式:{}", getCx(vo), getJsfs(vo)); cell = new PdfPCell(new Paragraph(concent, textfont1)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); //合并單元格 cell.setColspan(5); cell.setRowspan(1); table.addCell(cell); cell = new PdfPCell(new Paragraph("調(diào)出時間", textfont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setFixedHeight(40); table.addCell(cell); cell = new PdfPCell(new Paragraph(" 月 日 時 分", textfont1)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setColspan(2); cell.setRowspan(1); table.addCell(cell); cell = new PdfPCell(new Paragraph("應(yīng)送達時間", textfont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph(" 月 日 時 分", textfont1)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setColspan(2); cell.setRowspan(1); table.addCell(cell); cell = new PdfPCell(new Paragraph("物資名稱", textfont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setFixedHeight(40); table.addCell(cell); cell = new PdfPCell(new Paragraph("規(guī)格型號", textfont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); //后面2個單元格合并 cell.setColspan(2); //合并為1個 cell.setRowspan(1); table.addCell(cell); cell = new PdfPCell(new Paragraph("數(shù)量", textfont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph("單位", textfont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph("備注", textfont)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); //以下為列表數(shù)據(jù)輸出 List<StockOutDtlVO> dtl = vo.getDtls(); if (dtl.size() > 0) { for (StockOutDtlVO stock : dtl) { PdfPCell cell1 = new PdfPCell(new Paragraph(stock.getWzmc(), textfont1)); PdfPCell cell2 = new PdfPCell(new Paragraph(stock.getGgxh(), textfont1)); PdfPCell cell3 = new PdfPCell(new Paragraph(stock.getSl().toString(), textfont1)); PdfPCell cell4 = new PdfPCell(new Paragraph(stock.getDw(), textfont1)); PdfPCell cell5 = new PdfPCell(new Paragraph(stock.getBz(), textfont1)); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setVerticalAlignment(Element.ALIGN_MIDDLE); cell1.setFixedHeight(40); cell2.setHorizontalAlignment(Element.ALIGN_CENTER); cell2.setVerticalAlignment(Element.ALIGN_MIDDLE); cell2.setColspan(2); cell2.setRowspan(1); // cell2.setFixedHeight(20); cell3.setHorizontalAlignment(Element.ALIGN_CENTER); cell3.setVerticalAlignment(Element.ALIGN_MIDDLE); // cell3.setFixedHeight(20); cell4.setHorizontalAlignment(Element.ALIGN_CENTER); cell4.setVerticalAlignment(Element.ALIGN_MIDDLE); // cell4.setFixedHeight(20); cell5.setHorizontalAlignment(Element.ALIGN_CENTER); cell5.setVerticalAlignment(Element.ALIGN_MIDDLE); // cell5.setFixedHeight(20); table.addCell(cell1); table.addCell(cell2); table.addCell(cell3); table.addCell(cell4); table.addCell(cell5); } } document.add(table); document.add(new Paragraph("\n", space2)); float[] widthes = {25f, 25f}; table = new PdfPTable(widthes); table.setSpacingBefore(20f); table.setWidthPercentage(100.0f); PdfPCell cell1 = new PdfPCell(new Paragraph("儲備管理服務(wù)單位:", headfont1)); cell1.setVerticalAlignment(Element.ALIGN_LEFT); cell1.setHorizontalAlignment(Element.ALIGN_MIDDLE); cell1.setFixedHeight(35); //加上該配置不顯示單元格邊框 cell1.setBorder(0); PdfPCell cell2 = new PdfPCell(new Paragraph("承運司機:", headfont1)); cell2.setVerticalAlignment(Element.ALIGN_LEFT); cell2.setHorizontalAlignment(Element.ALIGN_MIDDLE); cell2.setBorder(0); table.addCell(cell1); table.addCell(cell2); document.add(table); float[] widthe2 = {25f, 25f, 25f}; table = new PdfPTable(widthe2); table.setSpacingBefore(20f); table.setWidthPercentage(100.0f); PdfPCell cell3 = new PdfPCell(new Paragraph("調(diào)出單位初審:", headfont1)); cell3.setVerticalAlignment(Element.ALIGN_LEFT); cell3.setHorizontalAlignment(Element.ALIGN_MIDDLE); cell3.setFixedHeight(35); cell3.setBorder(0); PdfPCell cell4 = new PdfPCell(new Paragraph("復(fù)核:", headfont1)); cell4.setVerticalAlignment(Element.ALIGN_LEFT); cell4.setHorizontalAlignment(Element.ALIGN_MIDDLE); cell4.setBorder(0); PdfPCell cell5 = new PdfPCell(new Paragraph("領(lǐng)導(dǎo)審批:", headfont1)); cell5.setVerticalAlignment(Element.ALIGN_LEFT); cell5.setHorizontalAlignment(Element.ALIGN_MIDDLE); cell5.setBorder(0); table.addCell(cell3); table.addCell(cell4); table.addCell(cell5); document.add(table); float[] widthe3 = {25f, 25f}; table = new PdfPTable(widthe3); table.setSpacingBefore(20f); table.setWidthPercentage(100.0f); PdfPCell cell6 = new PdfPCell(new Paragraph("接收單位簽收人:", headfont1)); cell6.setVerticalAlignment(Element.ALIGN_LEFT); cell6.setHorizontalAlignment(Element.ALIGN_MIDDLE); cell6.setFixedHeight(35); cell6.setBorder(0); PdfPCell cell7 = new PdfPCell(new Paragraph("簽收時間: 月 日 時 分", headfont1)); cell7.setVerticalAlignment(Element.ALIGN_LEFT); cell7.setHorizontalAlignment(Element.ALIGN_MIDDLE); cell7.setBorder(0); table.addCell(cell6); table.addCell(cell7); document.add(table); document.close(); } catch (Exception e) { e.printStackTrace(); } }
到此這篇關(guān)于Java純代碼實現(xiàn)導(dǎo)出pdf合并單元格的文章就介紹到這了,更多相關(guān)Java導(dǎo)出pdf內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring學(xué)習(xí)筆記2之表單數(shù)據(jù)驗證、文件上傳實例代碼
這篇文章主要介紹了Spring學(xué)習(xí)筆記2之表單數(shù)據(jù)驗證、文件上傳 的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07SpringBoot淺析安全管理之Spring Security配置
安全管理是軟件系統(tǒng)必不可少的的功能。根據(jù)經(jīng)典的“墨菲定律”——凡是可能,總會發(fā)生。如果系統(tǒng)存在安全隱患,最終必然會出現(xiàn)問題,這篇文章主要介紹了SpringBoot安全管理Spring Security基本配置2022-08-08SpringCloud?客戶端Ribbon負載均衡的實現(xiàn)方法
Ribbon 是 Netflix 提供的一個基于 Http 和 TCP 的客戶端負載均衡工具,且已集成在 Eureka 依賴中,這篇文章主要介紹了SpringCloud?客戶端Ribbon負載均衡的實現(xiàn)方法,需要的朋友可以參考下2022-06-06詳解Java8函數(shù)式編程之收集器的應(yīng)用
這篇文章主要介紹了詳解Java8函數(shù)式編程之收集器的應(yīng)用,收集器是一種通用的、從流生成復(fù)雜值的結(jié)構(gòu)??梢允褂盟鼜牧髦猩蒐ist、Set、Map等集合,需要的朋友可以參考下2023-04-04spring cloud config分布式配置中心的高可用問題
本文給大家介紹spring cloud config分布式配置中心的高可用問題,通過整合Eureka來實現(xiàn)配置中心的高可用,需要的朋友參考下本文2018-01-01深入學(xué)習(xí)java8?中的CompletableFuture
本文主要介紹了java8中的CompletableFuture,CompletableFuture實現(xiàn)了CompletionStage接口和Future接口,前者是對后者的一個擴展,增加了異步回調(diào)、流式處理、多個Future組合處理的能力,使Java在處理多任務(wù)的協(xié)同工作時更加順暢便利,下文需要的朋友可以參考一下2022-05-05