JAVA實(shí)現(xiàn)PDF和EXCEL生成和數(shù)據(jù)動(dòng)態(tài)插入以及導(dǎo)出
點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號(hào)”
優(yōu)質(zhì)文章,第一時(shí)間送達(dá)
? 作者?|? Tom-shushu
來(lái)源 |? urlify.cn/EfIBFf
76套java從入門到精通實(shí)戰(zhàn)課程分享
一、序言
Excel、PDF的導(dǎo)出、導(dǎo)入是我們工作中經(jīng)常遇到的一個(gè)問(wèn)題,剛好今天公司業(yè)務(wù)遇到了這個(gè)問(wèn)題,順便記個(gè)筆記以防下次遇到相同的問(wèn)題而束手無(wú)策。
公司有這么兩個(gè)需求:
需求一、給了一個(gè)表單,讓把查出來(lái)的數(shù)據(jù)組裝到表單中并且提供以PDF格式的下載功能。
需求二、將數(shù)據(jù)查出來(lái)以Excel表格的形式下載下來(lái)。
二、Java實(shí)現(xiàn)PDF的生成和數(shù)據(jù)動(dòng)態(tài)插入、導(dǎo)出功能
1、第一步:PDF制作模板
因?yàn)镻DF常用的軟件不讓支持編輯,我們就先使用WPS以Word的形式進(jìn)行編輯制作出與客戶需求一樣的樣式,然后直接另存為 .pdf 的形式如下圖所示:
a.Word里面制作模板

?b.更改名字為 .pdf形式

c.這時(shí)需要用到一個(gè)叫:Adobe Acrobat DC的軟件(可以白嫖7天^_^),具體操作如下:
用Adobe Acrobat DC打開(kāi)我們剛才改過(guò)名字的PDF文件,點(diǎn)擊右下角的“更多工具”按鈕

到下面這個(gè)頁(yè)面再點(diǎn)擊“準(zhǔn)備表單”按鈕

d.接下來(lái)就需要詳細(xì)的配置你的數(shù)據(jù)源了

數(shù)據(jù)源即:你代碼中實(shí)體類中對(duì)應(yīng)的數(shù)據(jù)(注意字段一定要一一對(duì)應(yīng)),配置完畢就可以保存進(jìn)行下面的代碼編寫工作了。
2、代碼的編寫(假定我們實(shí)體類什么的都已經(jīng)編寫完成、數(shù)據(jù)通過(guò)前端傳入獲取、模板位置在E盤根目錄下名字為:車輛維修審批單.pdf)
導(dǎo)入jar包:
????com.itextpdf
????itextpdf
????5.5.13
實(shí)現(xiàn)生成PDF、數(shù)據(jù)插入、導(dǎo)出
@RegisterToSMP(serviceDisplay?=?"預(yù)覽頁(yè)面PDF下載")??????
????????@RequestMapping(value?=?"/DM/gwclwxsq/qygl/exportPDF$m=query.service",method?=RequestMethod.POST)?
????????public?String?exportPdf(@RequestBody?GwclwxsqBean?gwclwxsqBean?,?HttpServletResponse?response)?throws?UnsupportedEncodingException?{????????????
????????????//?1.指定解析器
????????????System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
????????????????????"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
????????????String?filename="車輛維修審批單.pdf";
????????????String?path="e:/";
????????????response.setContentType("application/pdf");
????????????response.setHeader("Content-Disposition",?"attachment;fileName="
????????????????????+?URLEncoder.encode(filename,?"UTF-8"));
????????????OutputStream?os?=?null;
????????????PdfStamper?ps?=?null;
????????????PdfReader?reader?=?null;
????????????try?{
????????????????os?=?response.getOutputStream();
????????????????//?2?讀入pdf表單
????????????????reader?=?new?PdfReader(path+?"/"+filename);
????????????????//?3?根據(jù)表單生成一個(gè)新的pdf
????????????????ps?=?new?PdfStamper(reader,?os);
????????????????//?4?獲取pdf表單
????????????????AcroFields?form?=?ps.getAcroFields();
????????????????// 5給表單添加中文字體?這里采用系統(tǒng)字體。不設(shè)置的話,中文可能無(wú)法顯示
????????????????BaseFont?bf?=?BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",
??????????????????????????????BaseFont.IDENTITY_H,?BaseFont.EMBEDDED);
????????????????form.addSubstitutionFont(bf);
????????????????//?6查詢數(shù)據(jù)================================================
????????????????Map?data?=?new?HashMap();
??????????????????????data.put("commitTime",?gwclwxsqBean.getCommitTime());
??????????????????????data.put("driver",?gwclwxsqBean.getDriver());
??????????????????????data.put("carId",?gwclwxsqBean.getCarId());
??????????????????????data.put("carType",?gwclwxsqBean.getCarType());
??????????????????????data.put("repairAddress",?gwclwxsqBean.getRepairAddress());
??????????????????????data.put("repairCost",gwclwxsqBean.getRepairCost());
??????????????????????data.put("project",?gwclwxsqBean.getProject());
??????????????????????data.put("fwbzzxfzrYj",?gwclwxsqBean.getFwbzzxfzrYj());
??????????????????????data.put("fgldspYj",?gwclwxsqBean.getFgldspYj());
??????????????????????data.put("remarks",?gwclwxsqBean.getRemarks());???????????
?????????????????//?7遍歷data?給pdf表單表格賦值
????????????????for?(String?key?:?data.keySet())?{
????????????????????form.setField(key,data.get(key).toString());
????????????????}
????????????????ps.setFormFlattening(true);???????
????????????????log.info("*******************PDF導(dǎo)出成功***********************");
????????????}?catch?(Exception?e)?{??????????log.error("*******************PDF導(dǎo)出失敗***********************");
????????????????e.printStackTrace();
????????????}?finally?{
????????????????try?{
????????????????????ps.close();
????????????????????reader.close();
????????????????????os.close();
????????????????}?catch?(Exception?e)?{
????????????????????e.printStackTrace();
????????????????}
????????????}
????????????return?null;
????????}
3.測(cè)試


二、Java實(shí)現(xiàn)Excel生成和數(shù)據(jù)插入、導(dǎo)出
這個(gè)比較簡(jiǎn)單,直接上代碼(假定你的實(shí)體類、查詢什么的都已經(jīng)寫好)注意:實(shí)體類一個(gè)是你自己的數(shù)據(jù)實(shí)體類還有一個(gè)是你導(dǎo)出時(shí)表格中對(duì)應(yīng)的實(shí)體類
我們以一個(gè)真實(shí)的公司業(yè)務(wù)來(lái)舉個(gè)例子(一個(gè)統(tǒng)計(jì)疫情登記人員信息的Excel導(dǎo)出功能)
1.表頭對(duì)應(yīng)實(shí)體類ExportYqfkdj.java:
import?lombok.Data;
/**
?*?description:?
?*?@author:?zhouhong
?*?@version:?V1.0.0
?*?@date:?2021年1月14日?下午3:05:54
?*/
@Data
public?class?ExportYqfkdj?{
????/**
?????*?序號(hào)
?????*/
????private?Integer?xuhao;
????/**
?????*?姓名
?????*/
????private?String?xingming;??
????/**
?????*?證件號(hào)碼
?????*/
????private?String?zjhm;
????/**
?????*?聯(lián)系電話
?????*/
????private?String?lxdh;????
????/**
?????*?申請(qǐng)人工作單位
?????*/
????private?String?sqrGzdw;????
????/**
?????*?是否接觸過(guò)疑似病例
?????*/
????private?String?sfjcgysbl;
????/**
?????*?當(dāng)前是否與居家隔離人員同住
?????*/
????private?String?sfyjjglrytz;????
????/**
?????*?當(dāng)前狀態(tài)
?????*/
????private?String?dqzt;
????/**
?????*?當(dāng)前健康狀態(tài)
?????*/
????private?String?dqjkzt;
????/**
?????*?當(dāng)前體溫
?????*/
????private?String?dqtw;
????/**
?????*?當(dāng)前所在地址
?????*/
????private?String?dqszdz;
????/**
?????*?當(dāng)前居住地址
?????*/
????private?String?dqjzdz;
????/**
?????*?提交時(shí)間
?????*?*/
????private?String?tjsj;
}
b.Service層
/**
?????*?導(dǎo)出
?????*?@param?yqfkdjBean
?????*?@author?zhouhong
?????*?@return?
?????*?@throws?Exception
?????*/
????@Transactional(rollbackFor?=?{?Exception.class?})
????public?DataResult?exporYqfkdj(YqfkdjBean?yqfkdjBean)?throws?Exception?{
????????DataResult?result?=?new?DataResult();
????????List?list?=?new?ArrayList();
????????try?{
????????????/*?查詢導(dǎo)出信息?*/
????????????result?=?getYqfkMhCXQuery(yqfkdjBean);
????????????SimpleDateFormat?df?=?new?SimpleDateFormat("yyyyMMddhhmmssSSS");
????????????for?(int?i?=?0;?i?????????????????ExportYqfkdj?dmKhfwdcDtjlZxDto?=?new?ExportYqfkdj();
????????????????dmKhfwdcDtjlZxDto?=?ObjectUtil.parsePojo(result.getResults().get(i),?ExportYqfkdj.class);
????????????????dmKhfwdcDtjlZxDto.setXuhao(i?+?1);
????????????????list.add(dmKhfwdcDtjlZxDto);
????????????}
????????????String?filepath?=?"D:/疫情防控信息"?+?df.format(new?Date())?+?".xlsx";
????????????if?(System.getProperty(YqfkdjUtils.Wjdz.NAME).toLowerCase().startsWith(YqfkdjUtils.Wjdz.LI)
????????????????????||?System.getProperty(YqfkdjUtils.Wjdz.NAME).toLowerCase().startsWith(YqfkdjUtils.Wjdz.LIN))?{
????????????????filepath?=?"/home/Tomcat/temp/"?+?df.format(new?Date())?+?".xlsx";
????????????}
????????????EasyExcel.write(filepath,?ExportYqfkdj.class).head(head()).sheet().doWrite(list);
????????????result.setResults(list);
????????????result.setSuccess(true);
????????????result.setMsg(filepath);
????????}?catch?(Exception?e)?{
????????????result.setSuccess(false);
????????????result.setMsg(YqfkdjUtils.Cytx.DCSB);
????????????e.printStackTrace();
????????????throw?e;
????????}
????????return?result;
????}
????/**
?????*?疫情防控信息導(dǎo)出表頭
?????*?@author?zhouhong
?????*?@return?List>
?????*/
????private?List>?head()?{
????????List>?list?=?new?ArrayList>();
????????List?head0?=?new?ArrayList();
????????head0.add("序號(hào)");
????????List?head1?=?new?ArrayList();
????????head1.add("姓名");
????????List?head2?=?new?ArrayList();
????????head2.add("證件號(hào)碼");
????????List?head3?=?new?ArrayList();
????????head3.add("聯(lián)系電話");
????????List?head4?=?new?ArrayList();
????????head4.add("工作所在單位");
????????List?head5?=?new?ArrayList();
????????head5.add("是否接觸疑似病例");
????????List?head6?=?new?ArrayList();
????????head6.add("是否與隔離人員同住");
????????List?head7?=?new?ArrayList();
????????head7.add("當(dāng)前狀態(tài)");
????????List?head8?=?new?ArrayList();
????????head8.add("當(dāng)前健康狀態(tài)");
????????List?head9?=?new?ArrayList();
????????head9.add("體溫(°C)");
????????List?head10?=?new?ArrayList();
????????head10.add("當(dāng)前所在地址");
????????List?head11?=?new?ArrayList();
????????head11.add("當(dāng)前居住地址");
????????List?head12?=?new?ArrayList();
????????head12.add("提交時(shí)間");
????????list.add(head0);
????????list.add(head1);
????????list.add(head2);
????????list.add(head3);
????????list.add(head4);
????????list.add(head5);
????????list.add(head6);
????????list.add(head7);
????????list.add(head8);
????????list.add(head9);
????????list.add(head10);
????????list.add(head11);
????????list.add(head12);
????????return?list;
????}
c.Controller層
@RegisterToSMP(serviceDisplay?=?"疫情防控查詢導(dǎo)出")
????@RequestMapping(value?=?"/DM/yqfkdj/gr/yqfkdjdc$m=export.service",?method?=?RequestMethod.POST)
????public?void?exportKhfxxx(@RequestBody?YqfkdjBean?yqfkdjBean,?HttpServletResponse?resp)?throws?Exception?{
????????DataResult?result?=?new?DataResult();
????????try?{
????????????SimpleDateFormat?df?=?new?SimpleDateFormat("yyyyMMddhhmmssSSS");
????????????result?=?yqfkdjService.exporYqfkdj(yqfkdjBean);
????????????String?filepath?=?result.getMsg().replace("\"",?"");
????????????File?file?=?new?File(filepath);
????????????String?filename?=?"疫情防控信息"?+?df.format(new?Date())?+?".xlsx";
????????????InputStream?fis?=?new?BufferedInputStream(new?FileInputStream(filepath));
????????????byte[]?buffer?=?new?byte[fis.available()];
????????????fis.read(buffer);
????????????fis.close();
????????????resp.reset();
????????????resp.setHeader("Content-Disposition",
????????????????????"attachment;filename="?+?new?String(filename.replaceAll("?",?"").getBytes("gbk")));
????????????resp.setHeader("Content-Length",?""?+?file.length());
????????????OutputStream?os?=?new?BufferedOutputStream(resp.getOutputStream());
????????????resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
????????????//?輸出文件
????????????os.write(buffer);
????????????os.flush();
????????????os.close();
????????}?catch?(Exception?e)?{
????????????e.printStackTrace();
????????????log.info(YqfkdjUtils.Cytx.DCSB);
????????????throw?e;
????????}
????}
d.測(cè)試

已經(jīng)全部完成PDF和Excel的生成、插入、導(dǎo)出功能。
粉絲福利:Java從入門到入土學(xué)習(xí)路線圖
??????

??長(zhǎng)按上方微信二維碼?2 秒
感謝點(diǎn)贊支持下哈?
