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)文章
面向?qū)ο蠛兔嫦蜻^程的區(qū)別(動力節(jié)點java學(xué)院整理)
很多朋友不清楚面向?qū)ο蠛兔嫦蜻^程有什么區(qū)別,接下來小編給大家整理了關(guān)于面向?qū)ο蠛兔嫦蜻^程的區(qū)別講解,感興趣的朋友可以參考下2017-04-04
Java的StringBuilder在高性能場景下的正確用法
StringBuilder?對字符串的操作是直接改變字符串對象本身,而不是生成新的對象,所以新能開銷小.與StringBuffer相比StringBuilder的性能略高,StringBuilder則沒有保證線程的安全,從而性能略高于StringBuffer,需要的朋友可以參考下2023-05-05
SpringBoot JPA 表關(guān)聯(lián)查詢實例
本篇文章主要介紹了SpringBoot JPA 表關(guān)聯(lián)查詢實例,使用JPA原生的findBy語句實現(xiàn),具有一定的參考價值,有興趣的可以了解一下。2017-04-04
Mapstruct?@Mapper?@Mapping?使用小結(jié)
這篇文章主要介紹了Mapstruct?@Mapper?@Mapping使用小結(jié),他們用于各個對象實體間的相互轉(zhuǎn)換,例如數(shù)據(jù)庫底層實體轉(zhuǎn)為頁面對象,Model?轉(zhuǎn)為?DTO,?DTO?轉(zhuǎn)為其他中間對象,?VO?等等,相關(guān)轉(zhuǎn)換代碼為編譯時自動產(chǎn)生的新文件和代碼,需要的朋友可以參考下2023-09-09

