/*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; }
/** * 生成word,表格 * * @param data * @throws Exception */ public static void productWordForm( Map<String, String> tableinfo, Map<String, LinkedHashMap<String, LinkedHashMap<String, String>>> data, Parameters parameters) throws Exception { XWPFDocument xDocument = new XWPFDocument(); Iterator<String> tableNameIter = data.keySet().iterator(); while (tableNameIter.hasNext()) { String table_name = tableNameIter.next(); XWPFParagraph xp = xDocument.createParagraph(); XWPFRun r1 = xp.createRun(); r1.setText(table_name + " " + tableinfo.get(table_name)); r1.setFontSize(18); r1.setTextPosition(10); XWPFParagraph p = xDocument.createParagraph(); p.setAlignment(ParagraphAlignment.CENTER); p.setWordWrap(true); LinkedHashMap<String, LinkedHashMap<String, String>> columns = data.get(table_name); int rows = columns.size(); XWPFTable xTable = xDocument.createTable(rows + 1, 7); // 表格属性 CTTblPr tablePr = xTable.getCTTbl().addNewTblPr(); // 表格宽度 CTTblWidth width = tablePr.addNewTblW(); width.setW(BigInteger.valueOf(8600)); int i = 0; xTable.getRow(i).setHeight(380); setCellText(xDocument, xTable.getRow(i).getCell(0), "代码", "CCCCCC", getCellWidth(0)); setCellText(xDocument, xTable.getRow(i).getCell(1), "注释", "CCCCCC", getCellWidth(1)); setCellText(xDocument, xTable.getRow(i).getCell(2), "类型", "CCCCCC", getCellWidth(2)); setCellText(xDocument, xTable.getRow(i).getCell(3), "默认值", "CCCCCC", getCellWidth(3)); setCellText(xDocument, xTable.getRow(i).getCell(4), "标识", "CCCCCC", getCellWidth(4)); setCellText(xDocument, xTable.getRow(i).getCell(5), "主键", "CCCCCC", getCellWidth(5)); setCellText(xDocument, xTable.getRow(i).getCell(6), "空值", "CCCCCC", getCellWidth(6)); i = i + 1; // 下一行 int j = 0; // 列column索引 Map<String, LinkedHashMap<String, String>> keyColumnMap = keyColumns(columns); for (Iterator<String> columnNameIter = keyColumnMap.keySet().iterator(); columnNameIter.hasNext(); ) { String column_name = columnNameIter.next(); LinkedHashMap<String, String> columnsAtt = keyColumnMap.get(column_name); int cwidth = getCellWidth(j); setCellText(xDocument, xTable.getRow(i).getCell(j), column_name, "FFFFFF", cwidth); ++j; Iterator<String> columnTypeIter = columnsAtt.keySet().iterator(); while (columnTypeIter.hasNext()) { String colum_type = columnTypeIter.next(); cwidth = getCellWidth(j); setCellText( xDocument, xTable.getRow(i).getCell(j), columnsAtt.get(colum_type), "FFFFFF", cwidth); j++; } ++i; // 下一行 j = 0; // 恢复第一列 } Iterator<String> cloumnsNameIter = columns.keySet().iterator(); while (cloumnsNameIter.hasNext()) { xTable.getRow(i).setHeight(380); String colum_name = cloumnsNameIter.next(); LinkedHashMap<String, String> columnsAtt = columns.get(colum_name); int cwidth = getCellWidth(j); if (xTable.getRow(i) == null) continue; setCellText(xDocument, xTable.getRow(i).getCell(j), colum_name, "FFFFFF", cwidth); j++; Iterator<String> columnTypeIter = columnsAtt.keySet().iterator(); while (columnTypeIter.hasNext()) { String colum_type = columnTypeIter.next(); cwidth = getCellWidth(j); setCellText( xDocument, xTable.getRow(i).getCell(j), columnsAtt.get(colum_type), "FFFFFF", cwidth); j++; } j = 0; // 恢复第一列 ++i; // 下一行 } XWPFTableRow row = xTable.insertNewTableRow(0); row.setHeight(380); row.addNewTableCell(); CTTcPr cellPr = row.getCell(0).getCTTc().addNewTcPr(); cellPr.addNewTcW().setW(BigInteger.valueOf(1600)); row.getCell(0).setColor("CCCCCC"); row.getCell(0).setText("中文名称"); row.addNewTableCell(); cellPr = row.getCell(0).getCTTc().addNewTcPr(); cellPr.addNewTcW().setW(BigInteger.valueOf(3000)); row.getCell(1).setColor("FFFFFF"); row.getCell(1).setText(tableinfo.get(table_name)); row.addNewTableCell(); cellPr = row.getCell(0).getCTTc().addNewTcPr(); cellPr.addNewTcW().setW(BigInteger.valueOf(1200)); row.getCell(2).setColor("CCCCCC"); row.getCell(2).setText("英文名称"); row.addNewTableCell(); CTTc cttc = row.getCell(3).getCTTc(); CTTcPr ctPr = cttc.addNewTcPr(); cellPr = row.getCell(0).getCTTc().addNewTcPr(); cellPr.addNewTcW().setW(BigInteger.valueOf(2800)); ctPr.addNewGridSpan().setVal(BigInteger.valueOf(4)); ctPr.addNewHMerge().setVal(STMerge.CONTINUE); cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER); cttc.getPList().get(0).addNewR().addNewT().setStringValue(table_name); row = xTable.insertNewTableRow(1); row.setHeight(380); row.addNewTableCell(); cellPr = row.getCell(0).getCTTc().addNewTcPr(); cellPr.addNewTcW().setW(BigInteger.valueOf(1600)); row.getCell(0).setColor("CCCCCC"); row.getCell(0).setText("功能描述"); row.addNewTableCell(); cellPr = row.getCell(0).getCTTc().addNewTcPr(); cellPr.addNewTcW().setW(BigInteger.valueOf(7000)); cttc = row.getCell(1).getCTTc(); ctPr = cttc.addNewTcPr(); ctPr.addNewGridSpan().setVal(BigInteger.valueOf(6)); ctPr.addNewHMerge().setVal(STMerge.CONTINUE); cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.LEFT); cttc.getPList().get(0).addNewR().addNewT().setStringValue(""); } FileOutputStream fos = new FileOutputStream(parameters.getPath() + parameters.getDatabase() + "_doc.docx"); xDocument.write(fos); fos.close(); }