poi匯出複雜模板excel和word
阿新 • • 發佈:2020-09-10
匯出複雜木板使用這個老哥的東西很好用
https://blog.csdn.net/jiangyongbing008/article/details/100795250
很方便的完成了,插入圖片也有,只需要把圖片轉換成位元組流。程式碼如下
//初始化工具 ExcelTemplate excel = new ExcelTemplate(filePath + fileName); /*javaBean轉map*/ Map<String, Object> map = getSevenInfo(affairSevenKnowledge);// 第一個引數,需要操作的sheet的索引 // 第二個引數,需要複製的區域的第一行索引(佔位符所在的第一行) // 第三個引數,需要複製的區域的最後一行索引(佔位符所在的最後一行) // 第四個引數,需要插入的位置的索引(佔位符的下一行) // 第五個引數,填充行區域中${}的值 // 第六個引數,是否需要刪除原來的區域 // 需要注意的是,行的索引一般要減一 try { excel.fillVariable(0, (Map<String, String>) map.get("param"));int i = excel.addRowByExist(0,14,14,15, (LinkedHashMap<Integer, LinkedList<String>>) map.get("rows"),true); //家庭成員和主要社會關係下方多處來的列合併到一個單元格 //沒有關係就不需要合併了 if (i>0){ // excel.mergedRegion(0,16,16+i+1,1,1); } // excel.addRowByExist(0,14,14,15,rows,true);} catch (IOException e) { e.printStackTrace(); } // 第一個引數,需要操作的sheet的索引 // 第二個引數,替換sheet當中${<變數的值>}的值 //下載 try { // 清除buffer快取 response.reset(); response.setContentType("application/octet-stream; charset=utf-8"); //指定下載名字 response.setHeader("Content-Disposition", "attachment; filename="+ Encodes.urlEncode(affairSevenKnowledge.getName()+fileName)); response.addHeader("Pargam", "no-cache"); response.addHeader("Cache-Control", "no-cache"); OutputStream fout = response.getOutputStream(); excel.getWorkbook().write(fout); fout.close(); } catch (IOException e) { e.printStackTrace(); }
匯出word模板使用的這個老哥的東西
https://blog.csdn.net/qq_27828675/article/details/88708983
匯出word模板還是挺坑的,word是由xml組成的。再word裡編輯好佔位符後,轉成xml文件會發現,佔位符已經分開了。我使用數字做佔位符,轉換成xml後把數字轉換成需要的佔位符。但是,
轉換完後,有些佔位符還是無法替換,再次轉成xml發現,佔位符還是分開了。。。。最後
我看了下老哥寫的程式碼,我給改動了下,模板未匹配到的區域我給替換成了空字串。修改後的程式碼如下
import java.io.*; import java.util.Map; import com.thinkgem.jeesite.common.utils.StringUtils; import org.apache.poi.ooxml.POIXMLDocument; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.xwpf.usermodel.*; import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlToken; import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps; import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D; import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge; import java.util.*; /** * https://blog.csdn.net/qq_27828675/article/details/88708983 */ public class WordTemplate { /** * 根據模板生成新word文件 * 判斷表格是需要替換還是需要插入,判斷邏輯有$為替換,表格無$為插入 * * @param inputUrl 模板存放地址 * @param outputUrl 新文件存放地址 * @param textMap 需要替換的資訊集合 * @param tableList 需要插入的表格資訊集合 * @return 成功返回true, 失敗返回false */ public static CustomXWPFDocument changWord(String inputUrl, String outputUrl, Map<String, Object> textMap, List<String[]> tableList) { CustomXWPFDocument document = null; //模板轉換預設成功 boolean changeFlag = true; try { //獲取docx解析物件 document = new CustomXWPFDocument(POIXMLDocument.openPackage(inputUrl)); //解析替換文字段落物件 WordTemplate.changeText(document, textMap); //解析替換表格物件 WordTemplate.changeTable(document, textMap, tableList); //生成新的word /*File file = new File(outputUrl); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } FileInputStream stream = new FileInputStream(file);*/ // document.write(stream); // stream.close(); } catch (IOException e) { e.printStackTrace(); changeFlag = false; } return document; } /** * 替換段落文字 * * @param document docx解析物件 * @param textMap 需要替換的資訊集合 */ public static void changeText(XWPFDocument document, Map<String, Object> textMap) { //獲取段落集合 List<XWPFParagraph> paragraphs = document.getParagraphs(); for (XWPFParagraph paragraph : paragraphs) { //判斷此段落時候需要進行替換 String text = paragraph.getText(); if (checkText(text)) { List<XWPFRun> runs = paragraph.getRuns(); for (XWPFRun run : runs) { //替換模板原來位置 run.setText((String) changeValue(run.toString(), textMap), 0); } } } } /** * 替換表格物件方法 * * @param document docx解析物件 * @param textMap 需要替換的資訊集合 * @param tableList 需要插入的表格資訊集合 */ public static void changeTable(CustomXWPFDocument document, Map<String, Object> textMap, List<String[]> tableList) { //獲取表格物件集合 List<XWPFTable> tables = document.getTables(); for (int i = 0; i < tables.size(); i++) { //只處理行數大於等於2的表格,且不迴圈表頭 XWPFTable table = tables.get(i); if (table.getRows().size() > 1) { //判斷表格是需要替換還是需要插入,判斷邏輯有$為替換,表格無$為插入 if (checkText(table.getText())) { List<XWPFTableRow> rows = table.getRows(); //遍歷表格,並替換模板 eachTable(document, rows, textMap); } else { // System.out.println("插入"+table.getText()); insertTable(table, tableList); } } } } /** * 遍歷表格,包含對圖片內容的替換 * * @param rows 表格行物件 * @param textMap 需要替換的資訊集合 */ public static void eachTable(CustomXWPFDocument document, List<XWPFTableRow> rows, Map<String, Object> textMap) { for (XWPFTableRow row : rows) { List<XWPFTableCell> cells = row.getTableCells(); for (XWPFTableCell cell : cells) { //判斷單元格是否需要替換 if (checkText(cell.getText())) { List<XWPFParagraph> paragraphs = cell.getParagraphs(); for (XWPFParagraph paragraph : paragraphs) { List<XWPFRun> runs = paragraph.getRuns(); for (XWPFRun run : runs) { // run.setText(changeValue(run.toString(), textMap),0); Object ob = changeValue(run.toString(), textMap); if (ob instanceof String) { run.setText((String) ob, 0); } else if (ob instanceof Map) { run.setText("", 0); Map pic = (Map) ob; int width = Integer.parseInt(pic.get("width").toString()); int height = Integer.parseInt(pic.get("height").toString()); int picType = getPictureType(pic.get("type").toString()); byte[] byteArray = (byte[]) pic.get("content"); ByteArrayInputStream byteInputStream = new ByteArrayInputStream(byteArray); try { String ind = document.addPictureData(byteInputStream, picType); document.createPicture(run, ind, document.getNextPicNameNumber(picType), width, height); } catch (Exception e) { e.printStackTrace(); } } } } } } } } /** * 為表格插入資料,行數不夠新增新行 * * @param table 需要插入資料的表格 * @param tableList 插入資料集合 */ public static void insertTable(XWPFTable table, List<String[]> tableList) { //建立行,根據需要插入的資料新增新行,不處理表頭 for (int i = 1; i < tableList.size(); i++) { XWPFTableRow row = table.createRow(); } //遍歷表格插入資料 List<XWPFTableRow> rows = table.getRows(); for (int i = 1; i < rows.size(); i++) { XWPFTableRow newRow = table.getRow(i); List<XWPFTableCell> cells = newRow.getTableCells(); for (int j = 0; j < cells.size(); j++) { XWPFTableCell cell = cells.get(j); cell.setText(tableList.get(i - 1)[j]); } } } /** * word跨列合併單元格 */ public static void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) { for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) { XWPFTableCell cell = table.getRow(row).getCell(cellIndex); if (cellIndex == fromCell) { // The first merged cell is set with RESTART merge value cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART); } else { // Cells which join (merge) the first one, are set with CONTINUE cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE); } } } /** * word跨行並單元格 */ public static void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) { for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) { XWPFTableCell cell = table.getRow(rowIndex).getCell(col); if (rowIndex == fromRow) { // The first merged cell is set with RESTART merge value cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART); } else { // Cells which join (merge) the first one, are set with CONTINUE cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE); } } } /** * 單元格設定字型 * @param cell * @param cellText */ private static void getParagraph(XWPFTableCell cell,String cellText){ CTP ctp = CTP.Factory.newInstance(); XWPFParagraph p = new XWPFParagraph(ctp, cell); p.setAlignment(ParagraphAlignment.CENTER); XWPFRun run = p.createRun(); run.setText(cellText); CTRPr rpr = run.getCTR().isSetRPr() ? run.getCTR().getRPr() : run.getCTR().addNewRPr(); CTFonts fonts = rpr.isSetRFonts() ? rpr.getRFonts() : rpr.addNewRFonts(); fonts.setAscii("仿宋"); fonts.setEastAsia("仿宋"); fonts.setHAnsi("仿宋"); cell.setParagraph(p); } /** * 判斷文字中時候包含$ * * @param text 文字 * @return 包含返回true, 不包含返回false */ public static boolean checkText(String text) { boolean check = false; if (text.indexOf("$") != -1) { check = true; } return check; } /** * 匹配傳入資訊集合與模板 * * @param value 模板需要替換的區域 * @param textMap 傳入資訊集合 * @return 模板需要替換區域資訊集合對應值 */ public static Object changeValue(String value, Map<String, Object> textMap) { Object obj = null; Set<Map.Entry<String, Object>> textSets = textMap.entrySet(); for (Map.Entry<String, Object> textSet : textSets) { if (textSet.getKey().contains("jobFullname") && value.contains("jobFullname")){ System.out.println(textSet.getKey()); } //匹配模板與替換值 格式${key} String key = "${" + textSet.getKey() + "}"; if (value.indexOf(key) != -1) { obj = textSet.getValue(); } } if (!(obj instanceof Map)) { //模板未匹配到區域替換為空 if (obj != null && checkText(obj.toString())) { obj = ""; } } if (obj==null){ obj=""; } return obj; } /** * 根據圖片型別,取得對應的圖片型別程式碼 * * @param picType * @return int */ private static int getPictureType(String picType) { int res = CustomXWPFDocument.PICTURE_TYPE_PICT; if (picType != null) { if (picType.equalsIgnoreCase("png")) { res = CustomXWPFDocument.PICTURE_TYPE_PNG; } else if (picType.equalsIgnoreCase("dib")) { res = CustomXWPFDocument.PICTURE_TYPE_DIB; } else if (picType.equalsIgnoreCase("emf")) { res = CustomXWPFDocument.PICTURE_TYPE_EMF; } else if (picType.equalsIgnoreCase("jpg") || picType.equalsIgnoreCase("jpeg")) { res = CustomXWPFDocument.PICTURE_TYPE_JPEG; } else if (picType.equalsIgnoreCase("wmf")) { res = CustomXWPFDocument.PICTURE_TYPE_WMF; } } return res; } /** * 將輸入流中的資料寫入位元組陣列 * * @param in * @return */ public static byte[] inputStream2ByteArray(InputStream in, boolean isClose) { byte[] byteArray = null; try { int total = in.available(); byteArray = new byte[total]; in.read(byteArray); } catch (IOException e) { e.printStackTrace(); } finally { if (isClose) { try { in.close(); } catch (Exception e2) { System.out.println("關閉流失敗"); } } } return byteArray; } } /** * @BelongsProject: exchange * @BelongsPackage: com.elens.util * @Author: xuweichao * @CreateTime: 2019-03-20 12:34 * @Description: 重寫XWPFDocument的方法,插入圖片 */
import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlToken; import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps; import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D; import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline; import java.io.IOException; import java.io.InputStream; public class CustomXWPFDocument extends XWPFDocument { public CustomXWPFDocument() { super(); } public CustomXWPFDocument(OPCPackage opcPackage) throws IOException { super(opcPackage); } public CustomXWPFDocument(InputStream in) throws IOException { super(in); } public void createPicture(XWPFRun run, String blipId, int id, int width, int height) { final int EMU = 9525; width *= EMU; height *= EMU; //舊版本方法 .getPackageRelationship() 在該依賴包下已被刪除 //String blipId = getAllPictures().get(id).getPackageRelationship().getId(); //在docment下建立XWPFRun 圖片會被新增到文件末尾 // CTInline inline = createParagraph().createRun().getCTR().addNewDrawing().addNewInline(); CTInline inline =run.getCTR().addNewDrawing().addNewInline(); String picXml = "" + "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" + " <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" + " <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" + " <pic:nvPicPr>" + " <pic:cNvPr id=\"" + id + "\" name=\"Generated\"/>" + " <pic:cNvPicPr/>" + " </pic:nvPicPr>" + " <pic:blipFill>" + " <a:blip r:embed=\"" + blipId + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" + " <a:stretch>" + " <a:fillRect/>" + " </a:stretch>" + " </pic:blipFill>" + " <pic:spPr>" + " <a:xfrm>" + " <a:off x=\"0\" y=\"0\"/>" + " <a:ext cx=\"" + width + "\" cy=\"" + height + "\"/>" + " </a:xfrm>" + " <a:prstGeom prst=\"rect\">" + " <a:avLst/>" + " </a:prstGeom>" + " </pic:spPr>" + " </pic:pic>" + " </a:graphicData>" + "</a:graphic>"; //CTGraphicalObjectData graphicData = inline.addNewGraphic().addNewGraphicData(); XmlToken xmlToken = null; try { xmlToken = XmlToken.Factory.parse(picXml); } catch (XmlException xe) { xe.printStackTrace(); } inline.set(xmlToken); //graphicData.set(xmlToken); inline.setDistT(0); inline.setDistB(0); inline.setDistL(0); inline.setDistR(0); CTPositiveSize2D extent = inline.addNewExtent(); extent.setCx(width); extent.setCy(height); CTNonVisualDrawingProps docPr = inline.addNewDocPr(); docPr.setId(id); docPr.setName("Picture " + id); docPr.setDescr("Generated"); } }
使用起來也是很方便的,程式碼如下
response.reset(); response.setContentType("application/octet-stream; charset=utf-8"); //指定下載名字 response.setHeader("Content-Disposition", "attachment; filename="+ Encodes.urlEncode("檔名.zip")); response.addHeader("Pargam", "no-cache"); response.addHeader("Cache-Control", "no-cache"); String fileSeperator = File.separator; String filePath = Global.getUserfilesBaseDir() + fileSeperator + "userfiles" + fileSeperator + "template" + fileSeperator; ZipOutputStream out = null; try { out = new ZipOutputStream(response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } ZipOutputStream finalOut = out; try { /*遍歷所有人,每個物件準換為map*/ for (PersonnelBase item : page.getList()) { Map<String, Object> map = getPersonnelBaseInfo(item, request); /*寫入xlsx*/ Map<String,Object> photo = (Map<String, Object>) map.get("excelPhoto"); byte[] imageBytes = (byte[]) photo.get("imageBytes"); ExcelTemplate excel = new ExcelTemplate(filePath + "檔名.xlsx"); excel.fillVariable(0, (Map<String, String>) map.get("sheet1")); excel.fillVariable(1, (Map<String, String>) map.get("sheet2")); int line = excel.addRowByExist(1, 5, 5, 6, (LinkedHashMap<Integer, LinkedList<String>>) map.get("rows"), true); if (photo.get("imageType") != null && imageBytes != null) { int imageType = (int) photo.get("imageType"); // excel.insertPicture(0,imageBytes, imageType,1,5,8,8); } ZipEntry xlsx = new ZipEntry(item.getName() + "檔名.xlsx"); finalOut.putNextEntry(xlsx); excel.getWorkbook().write(finalOut); /*寫入doc*/ ZipEntry docx = new ZipEntry(item.getName() + "檔名.docx"); finalOut.putNextEntry(docx); String fileNameInResource = filePath + "檔名.docx"; CustomXWPFDocument document = WordTemplate.changWord(fileNameInResource, personnelBase.getName() + "幹部任免審批表.docx", (Map<String, Object>) map.get("doc"), null); document.write(finalOut); } } catch (IOException e) { e.printStackTrace(); } finally { try { /*注意順序 否則導致檔案錯誤*/ finalOut.flush(); finalOut.close(); response.getOutputStream().close(); } catch (IOException e) { e.printStackTrace(); } }
就這麼記記吧,突然想起來的,記的不是很詳細了。