pom.xml
代碼:
private static String fileDir = "/home/xdja/conf/edums_js/"; private static String errorWordFilePath = "importWordErrorDetails.doc"; private static String errorExcelFilePath = "importExcelErrorDetails.doc"; /** * 寫導入題庫錯誤文件類容 * @param fileDir 文件路徑 * @param filePath 文件路徑 * @param content 內容 * @param type 文件類型 1---excel 2 word; * @author niugang */ private void writerErrorFileContent(String fileDir, String filePath, List<String> content, String type) { XWPFDocument document = new XWPFDocument(); OutputStream stream = null; BufferedOutputStream bufferStream = null; try { File dir = new File(fileDir); if (!dir.exists()) { dir.mkdirs(); } stream = new FileOutputStream(new File(fileDir+filePath)); bufferStream = new BufferedOutputStream(stream, 1024); // 創建一個段落 XWPFParagraph p1 = document.createParagraph(); // 設置居中 p1.setAlignment(ParagraphAlignment.CENTER); XWPFRun r1 = p1.createRun(); // 是否加粗 r1.setBold(true); // 與下一行的距離 r1.setTextPosition(30); if (EdumsConstants.FILE_TYPE_XLS.equals(type)) { // 段落名稱 r1.setText("EXCEL導入題目錯誤格式詳解"); } if (EdumsConstants.FILE_TYPE_DOC.equals(type)) { // 段落名稱 r1.setText("WORD導入題目錯誤格式詳解"); } // 字體大小 r1.setFontSize(18);// 字體大小 // 增加換行 r1.addCarriageReturn(); // 創建第二個段落 XWPFParagraph p2 = document.createParagraph(); XWPFRun r2 = p2.createRun(); for (int i = 0; i < content.size(); i++) { r2.setText(i + 1 + "、" + content.get(i)); r2.addCarriageReturn(); } // 設置字體 r2.setFontFamily("仿宋"); r2.setFontSize(14);// 字體大小 document.write(stream); stream.close(); bufferStream.close(); } catch (Exception ex) { logger.error("寫word或Excel錯誤文件失敗:{}", ex.getMessage()); } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { e.printStackTrace(); logger.error("寫word或Excel錯誤文件失敗:{}", e.getMessage()); } } if (bufferStream != null) { try { bufferStream.close(); } catch (IOException e) { e.printStackTrace(); logger.error("寫word或Excel錯誤文件失敗:{}", e.getMessage()); } } } }
效果:
相關博客:
https://www.cnblogs.com/dayuruozhi/p/6490793.html
https://www.cnblogs.com/unruly/archive/2017/09/06/7483858.html
微信公眾號