mysql數(shù)據(jù)庫自動生成數(shù)據(jù)庫開發(fā)設(shè)計文檔


1、輸出表結(jié)果,表結(jié)構(gòu)可自己通過代碼調(diào)整,主要思路:

a 在java代碼中,通過數(shù)據(jù)庫查詢語句獲取所有表名和表名備注信息。

b 通過表名獲取某張表的所有字段說明。

c 整理查詢出來的結(jié)果,寫入到word文檔中。

輸出表結(jié)果

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é)尾。

查詢數(shù)據(jù)表名和備注

4、將查詢出來的結(jié)果整理,寫入word,并生成表格。

寫入word
寫入word
寫入word
寫入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();

}

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,030評論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,310評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,951評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,796評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,566評論 6 407
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,055評論 1 322
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,142評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,303評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,799評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,683評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,899評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,409評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,135評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,520評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,757評論 1 282
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,528評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,844評論 2 372

推薦閱讀更多精彩內(nèi)容