1 2 3 import java.awt.Color; 4 import java.io.FileOutputStream; 5 import java.sql.Connection; 6 import java.sql.DriverManager; 7 import java.sql.ResultSet; 8 import java.sql.SQLException; 9 import java.sql.Statement; 10 import java.util.ArrayList; 11 import java.util.HashMap; 12 import java.util.Iterator; 13 import java.util.List; 14 importa java.util.Map; 15 16 Import com.lowagie.text *. ; . 17 Import com.lowagie.text.rtf.RtfWriter2; 18 is / ** . 19 * Oracle Database Version document generator 20 is * iText-2.1.7.jar 21 is * iText-2.1.7.jar RTF- 22 is * @author cuiyj 23 is * 24 * / 25 public class GenerateTableDoc { 26 is // key type dictionary 27 Private static the Map <String, String> = keyType new new the HashMap <String, String> (); 28 //需要导出的目标表 29 private static List<String> targetTable = new ArrayList<String>(); 30 static{ 31 targetTable.add("COMMON_ADDRESS");//表名 32 targetTable.add("L_USER"); 33 } 34 //初始化jdbc 35 static{ 36 try { 37 keyType.put("ID", "主键"); 38 // keyType.put("C", "Check"); 39 Class.forName("oracle.jdbc.OracleDriver"); 40 } the catch (a ClassNotFoundException E) { 41 is e.printStackTrace (); 42 is } 43 is } 44 is // Private static String URL = ""; // link URL 45 Private static String URL = "JDBC: Oracle: Thin: @ 127.0. 0.1: 1521: orcl "; // link url 46 Private static String username =" the user "; // . username need to set the default table space Ha 47 Private static String password =" the user "; // password 48 Private static String Schema = "USER";// target database name 49 // query all the tables sql statement 50 Private static String sql_get_all_tables = "the SELECT a.TABLE_NAME, b.COMMENTS from USER_TABLES A, USER_TAB_COMMENTS b = b.TABLE_NAME a.TABLE_NAME the Order by the WHERE TABLE_NAME"; // query all the fields sql statement 51 Private static String sql_get_all_columns = "select T1.column_name,T1.data_type,T1.data_length,t2.comments,T1.NULLABLE,(select max(constraint_type) from user_constraints x left join user_cons_columns y on x.constraint_name=y.constraint_name where x.table_name=t1.TABLE_NAME and y.COLUMN_NAME=T1.column_name) from user_tab_cols t1, user_col_comments t2, user_tab_comments t3 where t1.TABLE_NAME=t2.table_name(+) and t1.COLUMN_NAME=t2.column_name(+) and t1.TABLE_NAME=t3.table_name(+) and t1.TABLE_NAME='{table_name}' order by T1.COLUMN_ID "; 52 public static void main(String[] args) throws Exception { 53 //初始化word文档 54 Document document = new Document(PageSize.A4); 55 RtfWriter2.getInstance(document,new FileOutputStream("E:/word.doc")); 56 document.open(); 57 //查询开始 58 Connection conn = getConnection(); 59 //获取所有表 60 List tables = getDataBySQL(sql_get_all_tables,conn); 61 int i=1; 62 for (Iterator iterator = tables.iterator(); iterator.hasNext();) { 63 String [] arr = (String []) iterator.next(); 64 //Obtaining field information loop 65 String tableName ARR = [0 ]; 66 IF (targetTable.contains (tableName)) { 67 of System.out.print (I +. "Processing Data Sheet -----------" ARR + [0 ]); 68 addTableMetaData (Document, ARR, I); 69 List Columns = getDataBySQL (sql_get_all_columns.replace ( "} {table_name", ARR [0 ]), Conn); 70 addTableDetail (Document, Columns); 71 is addBlank (Document); 72 System.out.println ( "DONE ..." ); 73 is I ++ ; 74 } 75 } 76 document.close(); 77 conn.close(); 78 } 79 /** 80 * 添加一个空行 81 * @param document 82 * @throws Exception 83 */ 84 public static void addBlank(Document document)throws Exception{ 85 Paragraph ph = new Paragraph(""); 86 ph.setAlignment(Paragraph.ALIGN_LEFT); 87 document.add(ph); 88 } 89 / * 90 * add details table contains a field 91 is * @param Document 92 * @param of arr1 93 * @param Columns 94 * @throws Exception 95 * / 96 public static void addTableDetail (the Document Document, List Columns) throws {Exception 97 the Table Table = new new the Table (. 6 ); 98 table.setWidth (100F); 99 table.setBorderWidth (. 1 ); 100 table.setBorderColor (as Color.BLACK.); 101 table.setPadding (0 ); 102 table.setSpacing (0 ); 103 the Cell Cell1 = new new the Cell ( "No."); // cell 104 cell1.setHeader ( to true ); 105 106 the cell Cell2 = new new the cell ( "column name"); // cell 107 cell2.setHeader ( to true ); 108 109 the cell the cell 3 = new new the cell ( "type"); // cell 110 cell3.setHeader (to true ); 111 112 the Cell CELL4 = new new the Cell ( "length"); // cell 113 cell4.setHeader ( to true ); 114 115 the Cell Cell 5 = new new the Cell ( "key"); // cell 1 16 cell5.setHeader ( to true ); 117 1 18 the cell CELL6 = new new the cell ( "instructions"); // cell 119 cell6.setHeader ( to true ); 120 // set header format 121 table.setWidths ( new new a float[]{8f,30f,15f,8f,10f,29f}); 122 cell1.setHorizontalAlignment(Cell.ALIGN_CENTER); 123 cell1.setBackgroundColor(Color.gray); 124 cell2.setHorizontalAlignment(Cell.ALIGN_CENTER); 125 cell2.setBackgroundColor(Color.gray); 126 cell3.setHorizontalAlignment(Cell.ALIGN_CENTER); 127 cell3.setBackgroundColor(Color.gray); 128 cell4.setHorizontalAlignment(Cell.ALIGN_CENTER); 129 cell4.setBackgroundColor(Color.gray); 130 cell5.setHorizontalAlignment(Cell.ALIGN_CENTER); 131 cell5.setBackgroundColor(Color.gray); 132 cell6.setHorizontalAlignment(Cell.ALIGN_CENTER); 133 cell6.setBackgroundColor(Color.gray); 134 table.addCell(cell1); 135 table.addCell(cell2); 136 table.addCell(cell3); 137 table.addCell(cell4); 138 table.addCell(cell5); 139 table.addCell(cell6); 140 table.endHeaders();// 表头结束 141 int x = 1; 142 for (Iterator iterator = columns.iterator(); iterator.hasNext();) { 143 String [] arr2 = (String []) iterator.next(); 144 Cell c1 = new Cell(x+""); 145 Cell c2 = new Cell(arr2[0]); 146 Cell c3 = new Cell(arr2[1]); 147 Cell c4 = new Cell(arr2[2]); 148 149 String key = keyType.get(arr2[5]); 150 if(key==null)key = ""; 151 Cell c5 = new Cell(key); 152 Cell c6 = new Cell(arr2[3]); 153 c1.setHorizontalAlignment(Cell.ALIGN_CENTER); 154 c2.setHorizontalAlignment(Cell.ALIGN_CENTER); 155 c3.setHorizontalAlignment(Cell.ALIGN_CENTER); 156 c4.setHorizontalAlignment(Cell.ALIGN_CENTER); 157 c5.setHorizontalAlignment(Cell.ALIGN_CENTER); 158 c6.setHorizontalAlignment(Cell.ALIGN_CENTER); 159 table.addCell(c1); 160 table.addCell(c2); 161 table.addCell(c3); 162 table.addCell(c4); 163 table.addCell (C5); 164 is table.addCell (C6); 165 X ++ ; 166 } 167 is document.add (Table); 168 } 169 / ** 170. * increase Profiles table 171 is * @param dcument 172 * @param ARR 173 * @param I 174 * @throws Exception 175 * / 176 public static void addTableMetaData (dcument the Document, String [] ARR, intI) throws Exception { 177 the Paragraph pH = new new the Paragraph (I + "Table Name:." + arr [0] + " Description:" + (ARR [. 1] == null "":? ARR [. 1 ])); 178 ph.setAlignment (Paragraph.ALIGN_LEFT); 179 dcument.add (pH); 180 [ } 181 / ** 182 * a list of the query's SQL statement 183 is * @param SQL 184 * @param Conn 185 * @return 186 * / 187 public static List getDataBySQL (String SQL, Conn Connection) { 188 Statement stmt = null; 189 ResultSet rs = null; 190 List list = new ArrayList(); 191 try { 192 stmt = conn.createStatement(); 193 rs = stmt.executeQuery(sql); 194 while(rs.next()){ 195 String [] arr = new String[rs.getMetaData().getColumnCount()]; 196 for(int i=0;i<arr.length;i++){ 197 arr[i] = rs.getString(i+1); 198 } 199 list.add(arr); 200 } 201 } catch (SQLException e) { 202 e.printStackTrace(); 203 }finally{ 204 try { 205 if(rs!=null)rs.close(); 206 if(stmt!=null)stmt.close(); 207 } catch (SQLException e) { 208 e.printStackTrace(); 209 } 210 } 211 return list; 212 } 213 /** 214 * 获取数据库连接 215 * @return 216 */ 217 public static Connection getConnection(){ 218 try { 219 return DriverManager.getConnection(url, username, password); 220 } catch (SQLException e) { 221 e.printStackTrace(); 222 } 223 return null; 224 } 225 }
1 ------ 这是相关依赖 2 <dependency> 3 <groupId>com.lowagie</groupId> 4 <artifactId>itext</artifactId> 5 <version>2.1.7</version> 6 </dependency> 7 <dependency> 8 <groupId>com.lowagie</groupId> 9 <artifactId>itext-rtf</artifactId> 10 <version>2.1.7</version> 11 </dependency>