/** * 基本的写操作 * * @throws Exception */ public void testSimpleWrite() throws Exception { // 新建一个文档 XWPFDocument doc = new XWPFDocument(); // 创建一个段落 XWPFParagraph para = doc.createParagraph(); XWPFParagraph para1 = doc.createParagraph(); // 一个XWPFRun代表具有相同属性的一个区域。 XWPFRun run = para.createRun(); run.setBold(true); // 加粗 run.setText("加粗的内容"); run = para.createRun(); run.setColor("FF0000"); run.setText("红色的字。"); // 一个XWPFRun代表具有相同属性的一个区域。 XWPFRun runa = para1.createRun(); runa.setBold(true); // 加粗 runa.setText("加粗的内容"); runa = para1.createRun(); runa.setText("红色的字。"); OutputStream os = new FileOutputStream(SystemUtil.getSystemPath("testw111notable.docx")); // 把doc输出到输出流 doc.write(os); this.close(os); }
private void generateWordDoc(String docName) throws FileNotFoundException, IOException { XWPFDocument doc = new XWPFDocument(); for (Theme t : themes) { for (Keyword k : t.getKeywords()) { for (Occurrence c : k.getOccurrs()) { XWPFParagraph p = doc.createParagraph(); p.setAlignment(ParagraphAlignment.LEFT); XWPFRun r = p.createRun(); setRunAttributes(r); r.setText(c.getOccurInfo()); r.addCarriageReturn(); String[] strings = c.getSentece().split(k.getName()); for (int i = 0; i < strings.length; i++) { XWPFRun r2 = p.createRun(); setRunAttributes(r2); r2.setText(strings[i]); if (i < strings.length - 1) { XWPFRun r3 = p.createRun(); setRunAttributes(r3); r3.setBold(true); r3.setItalic(true); r3.setColor(t.getHexColor()); r3.setText(k.getName()); } } } } } FileOutputStream outStream = new FileOutputStream(docName); doc.write(outStream); outStream.close(); }
/*public static void main(String aaa[]){*/ public boolean writeDataToMSWord(UserResume resumeData) { System.out.println("This is Word To Document Class"); File file = null; XWPFDocument document = null; XWPFParagraph para = null; XWPFRun run = null; try { // Create the first paragraph and set it's text. document = new XWPFDocument(); para = document.createParagraph(); para.setAlignment(ParagraphAlignment.CENTER); para.setSpacingAfter(100); para.setSpacingAfterLines(10); run = para.createRun(); run.addBreak(); // similar to new line run.addBreak(); XWPFTable table = document.createTable(4, 3); table.setRowBandSize(1); table.setWidth(1); table.setColBandSize(1); table.setCellMargins(1, 1, 100, 30); table.setStyleID("finest"); table.getRow(1).getCell(1).setText(resumeData.getName()); table.getRow(2).getCell(1).setText("fine"); XWPFParagraph p1 = table.getRow(0).getCell(0).getParagraphs().get(0); p1.setAlignment(ParagraphAlignment.CENTER); XWPFRun r1 = p1.createRun(); r1.setBold(true); r1.setText(resumeData.getInitial()); r1.setItalic(true); r1.setFontFamily("Courier"); r1.setTextPosition(100); table.getRow(0).getCell(0).setText(resumeData.getPhoneNumber()); table.getRow(0).getCell(2).setText(resumeData.getEmail()); System.out.println("Email:" + resumeData.getEmail()); table.getRow(2).getCell(2).setText(resumeData.getAddress()); table.setWidth(120); // done change from 120 to 200 file = new File("e:/resume.doc"); if (file.exists()) file.delete(); FileOutputStream out = new FileOutputStream(file); document.write(out); out.close(); return true; } catch (Exception e) { e.printStackTrace(); } return false; }