package com.chipmunk.word; import java.io.FileOutputStream; import java.math.BigInteger; import java.util.List; import java.util.Map; import org.apache.poi.hslf.model.textproperties.FontAlignmentProp; import org.apache.poi.xwpf.usermodel.ParagraphAlignment; import org.apache.poi.xwpf.usermodel.TextAlignment; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableCell; import org.apache.poi.xwpf.usermodel.XWPFTableRow; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHeight; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTrPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVerticalJc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc; import com.chipmunk.db.MysqlDAO; import com.chipmunk.db.StringUtil; public class WordTable { public static void main(String[] args) throws Exception { String path = "E:\\temp\\word\\1.docx"; XWPFDocument doc = new XWPFDocument(); MysqlDAO dao = new MysqlDAO(); String sql = "select * from TABLES where TABLE_SCHEMA='mydb'"; String sql2 = "select * from COLUMNS where TABLE_SCHEMA='mydb' and TABLE_NAME=?"; List<Map<String, Object>> list = dao.query(sql, null); for (Map<String, Object> map : list) { // System.out.println(map); String TABLE_NAME = StringUtil.toString(map.get("TABLE_NAME")); String ENGINE = StringUtil.toString(map.get("ENGINE")); String TABLE_COMMENT = StringUtil.toString(map.get("TABLE_COMMENT")); System.out.println(TABLE_NAME); System.out.println(ENGINE); System.out.println(TABLE_COMMENT); Object[]params = {TABLE_NAME}; List<Map<String, Object>> list2 = dao.query(sql2, params); XWPFParagraph p1 = doc.createParagraph(); // p1.setAlignment(ParagraphAlignment.CENTER); // p1.setVerticalAlignment(TextAlignment.TOP); XWPFRun r1 = p1.createRun(); r1.setBold(true); r1.setText(" "); r1.setFontFamily("Courier"); r1.setTextPosition(60); XWPFParagraph p2 = doc.createParagraph(); p1.setAlignment(ParagraphAlignment.LEFT); p1.setFontAlignment(1); p1.setVerticalAlignment(TextAlignment.TOP); XWPFRun r2 = p2.createRun(); r2.setBold(true); r2.setText(TABLE_NAME+":"+TABLE_COMMENT); r2.setFontFamily("Courier"); r2.setTextPosition(20); XWPFTable table = doc.createTable(list2.size()+1, 5); CTTbl tbl = table.getCTTbl(); CTTblPr tblPr = tbl.getTblPr() == null ? tbl.addNewTblPr() : tbl.getTblPr(); CTString styleStr = tblPr.addNewTblStyle(); styleStr.setVal("StyledTable"); CTTblWidth tblWidth = tblPr.isSetTblW ()? tblPr.getTblW (): tblPr.addNewTblW (); tblWidth.setW(BigInteger.valueOf(8000)); tblWidth.setType(STTblWidth.DXA); List<XWPFTableRow> rows = table.getRows(); for (int i = 0; i < rows.size(); i++) { XWPFTableRow row = rows.get(i); CTTrPr trPr = row.getCtRow().addNewTrPr(); CTHeight ht = trPr.addNewTrHeight(); ht.setVal(BigInteger.valueOf(360L)); List<XWPFTableCell> cells = row.getTableCells(); if (i==0) { for (int j = 0; j < cells.size(); j++) { XWPFTableCell cell = cells.get(j); CTTcPr tcpr = cell.getCTTc().addNewTcPr(); CTVerticalJc va = tcpr.addNewVAlign(); va.setVal(STVerticalJc.CENTER); CTShd ctshd = tcpr.addNewShd(); ctshd.setColor("auto"); ctshd.setVal(STShd.CLEAR); XWPFParagraph para = cell.getParagraphs().get(0); XWPFRun rh = para.createRun (); ctshd.setFill("E0E0E0"); String text_cell = ""; if (j == 0) { CTTblWidth tcwidth = tcpr.addNewTcW(); tcwidth.setW(BigInteger.valueOf(1700)); tcwidth.setType(STTblWidth.DXA); text_cell = "Field Name"; }else if (j == 1) { CTTblWidth tcwidth = tcpr.addNewTcW(); tcwidth.setW(BigInteger.valueOf(1700)); tcwidth.setType(STTblWidth.DXA); text_cell = "Field Type"; }else if (j == 2) { CTTblWidth tcwidth = tcpr.addNewTcW(); tcwidth.setW(BigInteger.valueOf(1000)); tcwidth.setType(STTblWidth.DXA); text_cell = "Field default value"; }else if (j == 3) { CTTblWidth tcwidth = tcpr.addNewTcW(); tcwidth.setW(BigInteger.valueOf(1000)); tcwidth.setType(STTblWidth.DXA); text_cell = "Field is not empty"; }else if (j == 4) { CTTblWidth tcwidth = tcpr.addNewTcW(); tcwidth.setW(BigInteger.valueOf(2600)); tcwidth.setType(STTblWidth.DXA); text_cell = "Field Meaning"; } rh.setText(text_cell); rh.setBold(true); para.setAlignment (ParagraphAlignment.CENTER); } }else { Map<String, Object> fields = list2.get(i-1); for (int j = 0; j < cells.size(); j++) { XWPFTableCell cell = cells.get(j); CTTcPr tcpr = cell.getCTTc().addNewTcPr(); CTVerticalJc va = tcpr.addNewVAlign(); va.setVal(STVerticalJc.CENTER); CTShd ctshd = tcpr.addNewShd(); ctshd.setColor("auto"); ctshd.setVal(STShd.CLEAR); XWPFParagraph para = cell.getParagraphs().get(0); XWPFRun rh = para.createRun (); if (i % 2 == 0) ctshd.setFill("F0F0F0"); else ctshd.setFill("FFFFFF"); String text_cell = ""; if (j==0) { CTTblWidth tcwidth = tcpr.addNewTcW(); tcwidth.setW(BigInteger.valueOf(1700)); tcwidth.setType(STTblWidth.DXA); String COLUMN_NAME = StringUtil.toString(fields.get("COLUMN_NAME")); text_cell = COLUMN_NAME; }else if (j==1) { CTTblWidth tcwidth = tcpr.addNewTcW(); tcwidth.setW(BigInteger.valueOf(1700)); tcwidth.setType(STTblWidth.DXA); String COLUMN_TYPE = StringUtil.toString(fields.get("COLUMN_TYPE")); text_cell = COLUMN_TYPE; }else if (j==2) { CTTblWidth tcwidth = tcpr.addNewTcW(); tcwidth.setW(BigInteger.valueOf(1000)); tcwidth.setType(STTblWidth.DXA); String COLUMN_DEFAULT = StringUtil.toString(fields.get("COLUMN_DEFAULT")); text_cell = COLUMN_DEFAULT; }else if (j==3) { CTTblWidth tcwidth = tcpr.addNewTcW(); tcwidth.setW(BigInteger.valueOf(1000)); tcwidth.setType(STTblWidth.DXA); String IS_NULLABLE = StringUtil.toString(fields.get("IS_NULLABLE")); text_cell = IS_NULLABLE; }else if (j==4) { CTTblWidth tcwidth = tcpr.addNewTcW(); tcwidth.setW(BigInteger.valueOf(3400)); tcwidth.setType(STTblWidth.DXA); String COLUMN_COMMENT = StringUtil.toString(fields.get("COLUMN_COMMENT")); text_cell = COLUMN_COMMENT; } rh.setText(text_cell); para.setAlignment (ParagraphAlignment.LEFT); } } } // for (Map<String, Object> map2 : list2) { // System.out.println(map2); // } } FileOutputStream out = new FileOutputStream(path); doc.write(out); out.close(); } }
Depends on jar package