1、輸出表結(jié)果,表結(jié)構(gòu)可自己通過代碼調(diào)整,主要思路:
a 在java代碼中,通過數(shù)據(jù)庫查詢語句獲取所有表名和表名備注信息。
b 通過表名獲取某張表的所有字段說明。
c 整理查詢出來的結(jié)果,寫入到word文檔中。
2、主要數(shù)據(jù)庫查詢語句說明
a、查詢數(shù)據(jù)庫所有表名和表名說明,查詢語句如下
" select table_name,table_comment from information_schema.tables where table_schema = 'xmsa_trace'? "
b、查詢數(shù)據(jù)庫某張表的所有字段說明,查詢語句如下
" SHOW FULL FIELDS FROM xmsa_trace.area_classify "
3、java代碼中,通過sql語句查詢,查詢上述兩個結(jié)果,不同的框架查詢方法不同,以下的是springmvc+mybatis框架的代碼,詳細代碼見文章結(jié)尾。
4、將查詢出來的結(jié)果整理,寫入word,并生成表格。
5、寫入word表格的詳細代碼,需導(dǎo)入itext-2.1.7.jar? itext-asian-5.2.0.jar? itext-rtf-2.1.7.jar 三個架包
package com.xmbestone.tlb.manage.util;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;import java.io.IOException;import java.util.List;
import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lowagie.text.Cell;import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.rtf.RtfWriter2;
import com.xmbestone.tlb.manage.service.business.IBusinessSupplierService;
/** * 創(chuàng)建word文檔 步驟: 1,建立文檔 2,創(chuàng)建一個書寫器 3,打開文檔 4,向文檔中寫入數(shù)據(jù) 5,關(guān)閉文檔 */
@Service("dateToWordUtil")public class DateToWordUtil {@Autowiredprivate IBusinessSupplierService businessSupplierService;
/** * @param args * @throws Exception */public void toWord(List> listAll) throws Exception {// 創(chuàng)建word文檔,并設(shè)置紙張的大小Document document = new Document(PageSize.A4);
try {// 創(chuàng)建word文檔RtfWriter2.getInstance(document, new FileOutputStream("E:/word5.doc"));
document.open();// 設(shè)置文檔標題
Paragraph ph = new Paragraph();
Font f = new Font();
Paragraph p = new Paragraph("數(shù)據(jù)庫表設(shè)計文檔", new Font(Font.NORMAL, 24,Font.BOLDITALIC, new Color(0, 0, 0)));
p.setAlignment(1);
document.add(p);
ph.setFont(f);/* * 創(chuàng)建表格 通過查詢出來的表遍歷 */
for (int i = 0; i < listAll.size(); i++) {
// 表名
String table_name = (String) listAll.get(i).get("table_name");
// 表說明
String table_comment = (String) listAll.get(i).get("table_comment");
String sql = "SHOW FULL FIELDS FROM xmsa_trace." + table_name+ " ";
//獲取某張表的所有字段說明
List> list = businessSupplierService.listMap(sql);
//構(gòu)建表說明
String all = "" + (i + 1) + " 表名:" + table_name + " "+ table_comment + "";
//創(chuàng)建有6列的表格
Table table = new Table(6);
document.add(new Paragraph(""));
table.setBorderWidth(1);
// table.setBorderColor(Color.BLACK);
table.setPadding(0);
table.setSpacing(0);
/*
* 添加表頭的元素,并設(shè)置表頭背景的顏色
*/
Color chade = new Color(176, 196, 222);
Cell cell = new Cell("序號");// 單元格
cell.setBackgroundColor(chade);
cell.setHeader(true);
// cell.setColspan(3);//設(shè)置表格為三列
// cell.setRowspan(3);//設(shè)置表格為三行
table.addCell(cell);
cell = new Cell("字段名");// 單元格
cell.setBackgroundColor(chade);
table.addCell(cell);
cell = new Cell("類型");// 單元格
cell.setBackgroundColor(chade);
table.addCell(cell);
cell = new Cell("是否為空");// 單元格
cell.setBackgroundColor(chade);
table.addCell(cell);
cell = new Cell("主鍵");// 單元格
cell.setBackgroundColor(chade);
table.addCell(cell);
cell = new Cell("字段說明");// 單元格
cell.setBackgroundColor(chade);
table.addCell(cell);
table.endHeaders();// 表頭結(jié)束
// 表格的主體,
for (int k = 0; k < list.size(); k++) {
//獲取某表每個字段的詳細說明
String Field = (String) list.get(k).get("Field");
String Type = (String) list.get(k).get("Type");
String Null = (String) list.get(k).get("Null");
String Key = (String) list.get(k).get("Key");
String Comment = (String) list.get(k).get("Comment");
table.addCell((k + 1) + "");
table.addCell(Field);
table.addCell(Type);
table.addCell(Null);
table.addCell(Key);
table.addCell(Comment);
}
Paragraph pheae = new Paragraph(all);
//寫入表說明
document.add(pheae);
//生成表格
document.add(table);
}
document.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
6、java代碼,將數(shù)據(jù)寫入到word文檔中并生成表格的樣例代碼。
package com.xmbestone.tlb.manage.util;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
/**
* 創(chuàng)建word文檔 步驟:
* 1,建立文檔
* 2,創(chuàng)建一個書寫器
* 3,打開文檔
* 4,向文檔中寫入數(shù)據(jù)
* 5,關(guān)閉文檔
*/
public class WordDemo {
public WordDemo() {
}
/**
* @param args
*/
public static void main(String[] args) {
// 創(chuàng)建word文檔,并設(shè)置紙張的大小
Document document = new Document(PageSize.A4);
try {
RtfWriter2.getInstance(document,
new FileOutputStream("E:/word5.doc"));
document.open();
//設(shè)置合同頭
Paragraph ph = new Paragraph();
Font f? = new Font();
Paragraph p = new Paragraph("數(shù)據(jù)庫表設(shè)計文檔", new Font(Font.NORMAL, 24, Font.BOLDITALIC, new Color(0, 0, 0)) );
p.setAlignment(1);
document.add(p);
ph.setFont(f);
// 設(shè)置中文字體
// BaseFont bfFont =? ? BaseFont.createFont("STSongStd-Light",? "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
// Font chinaFont = new Font();
/*
* 創(chuàng)建有三列的表格
*/
for(int i=0;i<5;i++){
Table table = new Table(6);
document.add(new Paragraph(""));
table.setBorderWidth(1);
// table.setBorderColor(Color.BLACK);
table.setPadding(0);
table.setSpacing(0);
/*
* 添加表頭的元素
*/
Color chade = new Color(176, 196, 222);
Cell cell = new Cell("序號");//單元格
cell.setBackgroundColor(chade);
cell.setHeader(true);
//? ? ? ? cell.setColspan(1);//設(shè)置表格為三列
//? ? ? ? cell.setRowspan(1);//設(shè)置表格為三行
table.addCell(cell);
cell = new Cell("字段名");//單元格
cell.setBackgroundColor(chade);
table.addCell(cell);
cell = new Cell("類型");//單元格
cell.setBackgroundColor(chade);
table.addCell(cell);
cell = new Cell("是否為空");//單元格
cell.setBackgroundColor(chade);
table.addCell(cell);
cell = new Cell("主鍵");//單元格
cell.setBackgroundColor(chade);
table.addCell(cell);
cell = new Cell("字段說明");//單元格
cell.setBackgroundColor(chade);
table.addCell(cell);
table.endHeaders();// 表頭結(jié)束
// 表格的主體
table.addCell("1,1");
table.addCell("1,2");
table.addCell("1,3");
table.addCell("1,4");
table.addCell("1,5");
table.addCell("1,6");
table.addCell("你好啊");
table.addCell("你好啊");
table.addCell("你好啊");
table.addCell("你好啊");
table.addCell("你好啊");
table.addCell("你好啊");
document.add(new Paragraph("表一"));
document.add(table);
}
document.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}