1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| @Service public class TableToWordUtil {
@Autowired TableMapper tableMapper;
public void toWord(List<Tables> tables, String fileName, String title) { Document document = new Document(PageSize.A4); try { File dir = new File(Constants.FILE_PATH); dir.mkdirs();
File file = new File(fileName); if (file.exists() && file.isFile()) { file.delete(); } file.createNewFile();
RtfWriter2.getInstance(document, new FileOutputStream(fileName)); document.open(); Paragraph ph = new Paragraph(); Font f = new Font(); Paragraph p = new Paragraph(title, new Font(Font.NORMAL, 24, Font.BOLDITALIC, new Color(0, 0, 0))); p.setAlignment(1); document.add(p); ph.setFont(f); for (int i = 0; i < tables.size(); i++) { String table_name = tables.get(i).getName(); String table_comment = tables.get(i).getComment(); List<TableFileds> fileds = tableMapper.getTable(tables.get(i).getName()); String all = "" + (i + 1) + " 表名称:" + table_name + "(" + table_comment + ")"; Table table = new Table(6);
document.add(new Paragraph(""));
table.setBorderWidth(1); table.setPadding(0); table.setSpacing(0);
Color chade = new Color(176, 196, 222);
Cell cell = new Cell("编号"); addCell(table, cell, chade); cell = new Cell("字段名"); addCell(table, cell, chade); cell = new Cell("类型"); addCell(table, cell, chade); cell = new Cell("是否非空"); addCell(table, cell, chade); cell = new Cell("是否主键"); addCell(table, cell, chade); cell = new Cell("注释"); addCell(table, cell, chade);
table.endHeaders();
for (int k = 0; k < fileds.size(); k++) { addContent(table, cell, (k + 1) + ""); addContent(table, cell, fileds.get(k).getField()); addContent(table, cell, fileds.get(k).getType()); addContent(table, cell, fileds.get(k).getNull().equals("YES") ? "否" : "是"); addContent(table, cell, fileds.get(k).getKey() != "" ? "是" : "否"); addContent(table, cell, fileds.get(k).getComment()); } Paragraph pheae = new Paragraph(all); document.add(pheae); document.add(table); } document.close(); } catch (Exception e) { e.printStackTrace(); } }
private void addCell(Table table, Cell cell, Color chade) { cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBackgroundColor(chade); table.addCell(cell); }
private void addContent(Table table, Cell cell, String content) { cell = new Cell(content); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); }
}
|