⼀键⽣成Oracle、SqlServer、MySql数据库表结构说明⽂档
⼀键⽣成Oracle、SqlServer、MySql数据库表结构说明⽂档
作为⼀名后台开发⼈员,平时⼯作中经常需要编写数据库表结构说明⽂档,⼿动编写显然不符合程序员的风格,在⽹上了⼀些⼯具⼤多只⽀持单⼀类型的数据库,于是决定⾃⼰动⼿写⼀个。有兴趣的朋友可下载源码在我的基础上继续拓展。
⼯具下载链接
⼯具使⽤说明
运⾏⼯具需提前安装“ framework 4.0”,⽬前⽀持Oracle、Mysql、Sqlserver这三种常见数据库,⽂档输出类型有word、html、PDF这三种。
⼯具使⽤界⾯效果图
⽣成⽂档效果图
核⼼代码
使⽤到的第三⽅DLL
Oracle.ManagedDataAccess.dll 该dll的好处在于不⽤安装oracle客户端可直连oracle数据库
MySql.Data.dll
Microsoft.Office.Interop.Word (word Api)
/// <summary>
/// ⽣成word⽂档
/// </summary>
/// <param name="frm">进度条</param>
/// <param name="data">字典表数据</param>
/// <param name="path">输出路径</param>
/
// <param name="dbName">数据库名称</param>
/// <param name="saveFile">保存⽂件名</param>
/// <param name="toHTML">是否⽣成html</param>
public  void WriteWord(FrmShowProgress frm, List<DesignDocData> data, string path, string dbName, string saveFile, bool toHTML)        {
_Application application = null;
_Document document = null;
object oEndOfDoc = "\\endofdoc";
try
{
int num6;
int num12;
application = new ApplicationClass
{
Visible = false
Visible = false
};
document = application.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
Paragraph paragraph = document.Content.Paragraphs.Add(ref oMissing);
paragraph.Range.Font.Name = "微软雅⿊";
paragraph.Range.Text = dbName + "数据库设计⽂档";
paragraph.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
// paragraph.Range.Font.Shadow = 1;
paragraph.Range.Font.Size = 14f;
paragraph.Format.SpaceAfter = 10f;
paragraph.Range.InsertParagraphAfter();
mysql帮助文档
int numRows = data.Count + 1;
int numColumns = 3;
Range range = _Item(ref oEndOfDoc).Range;
Table table = document.Tables.Add(range, numRows, numColumns, ref oMissing, ref oMissing);
table.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
table.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
table.Range.ParagraphFormat.SpaceAfter = 1f;
table.Range.Font.Size = 10f;
table.Rows[1].Range.Font.Shadow = 1;
table.Rows[1].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
table.Cell(1, 1).Range.Text = "序号";
table.Cell(1, 1).Range.Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;
table.Cell(1, 2).Range.Text = "表名";
table.Cell(1, 2).Range.Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;
table.Cell(1, 3).Range.Text = "说明";
table.Cell(1, 3).Range.Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;
table.Rows.First.Range.Font.Color = WdColor.wdColorBlack;
int progress = 0;
string message = string.Empty;
int num4 = 10;
int num5 = 90;
for (num6 = 2; num6 <= numRows; num6++)
{
progress = (int)((((num6 - 1) * 1.0) / ((double)(numRows * num4))) * 100.0);
try
{
frm.ShowProgress(string.Format("正在写⼊表名 {0} ", data[num6 - 2].TbName), progress, false);
}
catch
{
}
table.Rows[num6].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
table.Rows[num6].Cells[1].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;                    table.Rows[num6].Range.Font.Color = WdColor.wdColorBlack;
num12 = num6 - 1;
table.Cell(num6, 1).Range.Text = num12.ToString();
table.Cell(num6, 2).Range.Text = data[num6 - 2].TbName;
table.Cell(num6, 3).Range.Text = "";
}
for (num6 = 0; num6 < data.Count; num6++)
{
message = string.Format("正在处理表 {0} ", data[num6].TbName);
progress = ((int)((((num6 + 1) * 1.0) * num5) / ((double)data.Count))) + num4;
if (progress > 100)
{
progress = 100;
}
try
{
frm.ShowProgress(message, progress, false);
}
catch
{
}
Paragraph paragraph2 = document.Content.Paragraphs.Add(ref oMissing);
Paragraph paragraph2 = document.Content.Paragraphs.Add(ref oMissing);
paragraph2.Range.Font.Color = WdColor.wdColorBlack;
num12 = num6 + 1;
paragraph2.Range.Text = string.Format("{0}.{1}", num12.ToString(), data[num6].TbName);
paragraph2.Range.Font.Size = 10f;
paragraph2.Range.Font.Name = "宋体";
paragraph2.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
paragraph2.Range.InsertParagraphAfter();
int num7 = data[num6].TbCols.Count + 1;
int num8 = 6;
Range range2 = _Item(ref oEndOfDoc).Range;
Table table2 = document.Tables.Add(range2, num7, num8, ref oMissing, ref oMissing);
table2.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
table2.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
table2.Range.ParagraphFormat.SpaceAfter = 1f;
table2.Cell(1, 1).Range.Text = "序号";
table2.Cell(1, 1).Range.Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;
table2.Cell(1, 2).Range.Text = "名称";
table2.Cell(1, 2).Range.Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;
table2.Cell(1, 3).Range.Text = "数据类型";
table2.Cell(1, 3).Range.Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;
table2.Cell(1, 4).Range.Text = "允许空值";
table2.Cell(1, 4).Range.Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;
table2.Cell(1, 5).Range.Text = "是否主键";
table2.Cell(1, 5).Range.Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;
table2.Cell(1, 6).Range.Text = "说明";
table2.Cell(1, 6).Range.Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;
table2.Rows[1].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
int row = 2;
while (row <= num7)
{
table2.Rows[row].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
table2.Rows[row].Range.Font.Size = 10f;
table2.Rows[row].Cells[1].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;                        table2.Cell(row, 1).Range.Text = (row - 1).ToString();
table2.Cell(row, 2).Range.Text = data[num6].TbCols[row - 2].Name;
table2.Cell(row, 3).Range.Text = data[num6].TbCols[row - 2].DataTypeEx;
table2.Cell(row, 4).Range.Text = data[num6].TbCols[row - 2].Nullable ? "" : "N";
table2.Cell(row, 5).Range.Text = data[num6].TbCols[row - 2].IsPK ? "PK" : "";
table2.Cell(row, 6).Range.Text = data[num6].TbCols[row - 2].Describle;
row++;
}
if (data[num6].TbIndexs.Count > 0)
{
Paragraph paragraph3 = document.Content.Paragraphs.Add(ref oMissing);
paragraph3.Range.Font.Color = WdColor.wdColorBlack;
paragraph3.Range.Text = "";
paragraph3.Range.Font.Size = 10f;
paragraph3.Range.Font.Name = "宋体";
paragraph3.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
paragraph3.Range.InsertParagraphAfter();
int num10 = data[num6].TbIndexs.Count + 1;
int num11 = 2;
Range range3 = _Item(ref oEndOfDoc).Range;
Table table3 = document.Tables.Add(range3, num10, num11, ref oMissing, ref oMissing);
table3.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
table3.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
table3.Range.ParagraphFormat.SpaceAfter = 1f;
table3.Cell(1, 1).Range.Text = "索引名称";
table3.Cell(1, 1).Range.Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;
table3.Cell(1, 2).Range.Text = "列名";
table3.Cell(1, 2).Range.Shading.BackgroundPatternColor = WdColor.wdColorLightBlue;
table3.Rows[1].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
for (row = 2; row <= num10; row++)
{
table3.Rows[row].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
table3.Rows[row].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
table3.Rows[row].Range.Font.Size = 10f;
table3.Rows[row].Cells[1].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
table3.Cell(row, 1).Range.Text = data[num6].TbIndexs[row - 2].IndexName;
table3.Cell(row, 2).Range.Text = data[num6].TbIndexs[row - 2].ColName;
}
}
}
TimeSpan span = DateTime.Now.Subtract(this.timeStart);
if (span.Hours > 0)
{
message = string.Format("word写⼊完毕! ⽤时{0}时{1}分{2}秒", span.Hours, span.Minutes, span.Seconds);
}
else if (span.Minutes > 0)
{
message = string.Format("word写⼊完毕! ⽤时{0}分{1}秒", span.Minutes, span.Seconds);
}
else
{
message = string.Format("word写⼊完毕! ⽤时{0}秒", span.Seconds);
}
progress = 100;
try
{
frm.ShowProgress(message, progress, true);
}
catch
{
}
object fileName = path + @"\" + saveFile + ".doc";
if (File.Exists(fileName.ToString()))
{
File.Delete(fileName.ToString());
}
document.SaveAs(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref o Missing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
document.Close(ref oMissing, ref oMissing, ref oMissing);
application.Quit(ref oMissing, ref oMissing, ref oMissing);
document = null;
application = null;
}
catch (Exception exception)
{
throw exception;
}
finally
{
if (toHTML)
{
this.DocFomartHTML(frm, path, saveFile);
}
this.KillAllWinWord();
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。