主要是懒得再写一次了,所以就在关键点添加了注释,只能凑合着看了
有个地方困惑我了一天,所以发到博客做个记录,避免以后忘了
引用的dll我就不说了,大家应该都知道引用那些!!!
还有一点就是中间有数据库的操作,和循环创建行,这个应该没有什么难度,应该都能看懂!!!
private void AddWord()
{
try
{
TreeNode node = this.tree_main.SelectedNode;//获取选择节点的值
string str = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); //获取桌面路径 没有用上
string root = ""; //获取条件表名称
root = node.Name;
string sql = "SELECT TableName,FieldName,FieldTitle,DataType,DataLen FROM dbo.Table_Stru WHERE TableName='" + root + "'";
this.ds = DM.ReturnTableEx(sql); //执行sql 获取记录 存放在dataSet里面
//需要循环创建word行
string strtitle = "方法生成:";
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
//创建文档
WordAPP._Application oWord = new Microsoft.Office.Interop.Word.Application();
object unit = Microsoft.Office.Interop.Word.WdUnits.wdLine; //光标移到未尾
WordAPP._Document oDoc;
oWord.Visible = false;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
//设置页眉
oWord.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;
oWord.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;
oWord.ActiveWindow.ActivePane.Selection.InsertAfter("文档");
oWord.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
oWord.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument; //跳出页眉设置
//库名
WordAPP.Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text = strtitle;
oPara1.Range.Font.Bold = 1;
oPara1.Range.Font.Name = "宋体";
oPara1.Range.Font.Size = 12;
oPara1.Range.ParagraphFormat.Alignment = WordAPP.WdParagraphAlignment.wdAlignParagraphCenter;
oPara1.Format.SpaceAfter = 5; //24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter();
string funcName = "AddInfoData";
if (ds != null || ds.Tables[0].Rows.Count > 0) //判断读取的是否为空
{
//表名
WordAPP.Paragraph oPara2;
object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oPara2 = oDoc.Content.Paragraphs.Add(ref oRng);
int staIndex = node.Text.IndexOf("-");
oPara2.Range.Text = "添加、修改" + node.Text.Substring(staIndex, node.Text.Length - staIndex) + "(AddInfoData)";//标题
oPara2.Range.Font.Bold = 1;
oPara2.Range.Font.Name = "宋体";
oPara2.Range.Font.Size = 10;
oPara2.Range.ParagraphFormat.Alignment = WordAPP.WdParagraphAlignment.wdAlignParagraphLeft;
oPara2.Format.SpaceBefore = 15;
oPara2.Format.SpaceAfter = 1;
oPara2.Range.InsertParagraphAfter();
//描述信息
WordAPP.Paragraph oPara3;
oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oPara3 = oDoc.Content.Paragraphs.Add(ref oRng);
oPara3.Range.Text = "";
oPara3.Range.Font.Bold = 0;
oPara3.Range.Font.Name = "宋体";
oPara3.Range.Font.Size = 9;
oPara3.Range.ParagraphFormat.Alignment = WordAPP.WdParagraphAlignment.wdAlignParagraphCenter;
oPara3.Format.SpaceBefore = 1;
oPara3.Format.SpaceAfter = 1;
oPara3.Range.InsertParagraphAfter();
//插入表格
WordAPP.Table oTable;
WordAPP.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oTable = oDoc.Tables.Add(wrdRng, 5, 2, ref oMissing, ref oMissing);//指定行数和列数
oTable.Range.Font.Name = "宋体";
oTable.Range.Font.Size = 9;
oTable.Borders.Enable = 1;
oTable.Rows.Height = 10;
oTable.AllowAutoFit = true;
oTable.Columns[1].Width = 80;
//oTable.Columns[2].Width=80;
oTable.Columns[2].Width = 400;
//填充表格内容
oTable.Cell(1, 1).Range.Text = "CmdKind"; //在表格的第一行第一列填入内容。
oTable.Cell(1, 2).Range.Text = funcName; //在表格的第一行第二列填入内容。
oTable.Cell(2, 1).Range.Text = "函数功能";
oTable.Cell(2, 2).Range.Text = "添加或更改Info类型单据数据。";
oTable.Cell(3, 1).Range.Text = "inParam参数说明";
oTable.Cell(3, 2).Range.Text = "1.参数表\"Param\"";
oTable.Cell(3, 2).Range.Font.Bold = 1;
string para = "";
oTable.Cell(3, 2).Range.Select(); //获取焦点
unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;//移到未尾
oDoc.ActiveWindow.Selection.EndKey(ref unit, ref oMissing);
WordAPP.Table paraTable; //声明一个表格
WordAPP.Range rangePara = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
paraTable = oDoc.Tables.Add(oDoc.ActiveWindow.Selection.Range, 7, 6, ref oMissing, ref oMissing); //创建表格在光标的位置
paraTable.Range.Font.Name = "宋体";
paraTable.Range.Font.Size = 9;
paraTable.Borders.Enable = 1;
paraTable.Rows.Height = 10;
paraTable.AllowAutoFit = true;
//设置表格的宽度
paraTable.Columns[2].Width = 100;
paraTable.Columns[3].Width = 100;
paraTable.Columns[5].Width = 40;
paraTable.Columns[6].Width = 40;
paraTable.Cell(1, 1).Range.Text = "字段名称";
paraTable.Cell(1, 2).Range.Text = "字段说明";
paraTable.Cell(1, 3).Range.Text = "附加说明";
paraTable.Cell(1, 4).Range.Text = "字段类型";
paraTable.Cell(1, 5).Range.Text = "主键";
paraTable.Cell(1, 6).Range.Text = "非空";
paraTable.Cell(2, 1).Range.Text = "Module";
paraTable.Cell(2, 2).Range.Text = "数据库标识";
paraTable.Cell(2, 3).Range.Text = "";
paraTable.Cell(2, 4).Range.Text = "varchar(50)";
paraTable.Cell(2, 5).Range.Text = "0";
paraTable.Cell(2, 6).Range.Text = "1";
paraTable.Cell(3, 1).Range.Text = "TableName";
paraTable.Cell(3, 2).Range.Text = "数据表名称";
paraTable.Cell(3, 3).Range.Text = ds.Tables[0].Rows[0]["TableName"].ToString();
paraTable.Cell(3, 4).Range.Text = "varchar(50)";
paraTable.Cell(3, 5).Range.Text = "0";
paraTable.Cell(3, 6).Range.Text = "1";
//设置指定表格的背景颜色
paraTable.Cell(3, 3).Range.Shading.BackgroundPatternColor = Microsoft.Office.Interop.Word.WdColor.wdColorLightYellow;
paraTable.Cell(4, 1).Range.Text = "PID";
paraTable.Cell(4, 2).Range.Text = "树形节点编号值";
paraTable.Cell(4, 3).Range.Text = "Info类表传递树节点的编号,无树形结构的传递空字符串。";
paraTable.Cell(4, 4).Range.Text = "varchar(50)";
paraTable.Cell(4, 5).Range.Text = "0";
paraTable.Cell(4, 6).Range.Text = "0";
paraTable.Cell(5, 1).Range.Text = "UID";
paraTable.Cell(5, 2).Range.Text = "当前用户编号";
paraTable.Cell(5, 3).Range.Text = "";
paraTable.Cell(5, 4).Range.Text = "";
paraTable.Cell(5, 5).Range.Text = "0";
paraTable.Cell(5, 6).Range.Text = "1";
paraTable.Cell(6, 1).Range.Text = "TreeSetID";
paraTable.Cell(6, 2).Range.Text = "树结构配置编号";
paraTable.Cell(6, 3).Range.Text = "";
paraTable.Cell(6, 4).Range.Text = "varchar(50)";
paraTable.Cell(6, 5).Range.Text = "0";
paraTable.Cell(6, 6).Range.Text = "1";
oTable.Cell(3, 2).Range.Select(); //获取指定表格的焦点
unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;//移到未尾
oDoc.ActiveWindow.Selection.EndKey(ref unit, ref oMissing);
oDoc.ActiveWindow.Selection.InsertAfter("2.参数表\"Param2\"");//在指定的光标位置添加标题
oWord.ActiveWindow.Selection.Font.Bold = 1;
oTable.Cell(3, 2).Range.Select(); //获取焦点
unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;//移到未尾
oDoc.ActiveWindow.Selection.EndKey(ref unit, ref oMissing);
//在一个表格里面嵌套了两个表格
WordAPP.Table paraTable2;
WordAPP.Range rangePara1 = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
paraTable2 = oDoc.Tables.Add(oDoc.ActiveWindow.Selection.Range, 3, 6, ref oMissing, ref oMissing); //创建表格在光标的位置
paraTable2.Range.Font.Name = "宋体";
paraTable2.Range.Font.Size = 9;
paraTable2.Borders.Enable = 1;
paraTable2.Rows.Height = 10;
paraTable2.AllowAutoFit = true;
paraTable2.Columns[2].Width = 100;
paraTable2.Columns[3].Width = 100;
paraTable2.Columns[5].Width = 40;
paraTable2.Columns[6].Width = 40;
paraTable2.Cell(1, 1).Range.Text = "字段名称";
paraTable2.Cell(1, 2).Range.Text = "字段说明";
paraTable2.Cell(1, 3).Range.Text = "附加说明";
paraTable2.Cell(1, 4).Range.Text = "字段类型";
paraTable2.Cell(1, 5).Range.Text = "主键";
paraTable2.Cell(1, 6).Range.Text = "非空";
paraTable2.Cell(2, 1).Range.Text = "_op";
paraTable2.Cell(2, 2).Range.Text = "操作类型";
paraTable2.Cell(2, 3).Range.Text = "0:添加 1:更改";
paraTable2.Cell(2, 4).Range.Text = "INT";
paraTable2.Cell(2, 5).Range.Text = "0";
paraTable2.Cell(2, 6).Range.Text = "1";
paraTable2.Cell(3, 1).Range.Text = "ROOT";
paraTable2.Cell(3, 2).Range.Text = "父节点编号";
paraTable2.Cell(3, 3).Range.Text = "";
paraTable2.Cell(3, 4).Range.Text = "char(32)";
paraTable2.Cell(3, 5).Range.Text = "0";
paraTable2.Cell(3, 6).Range.Text = "0";
//循环添加表格
int a = 4;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
//SELECT TableName,FieldName,FieldTitle,DataType,DataLen FROM dbo.Table_Stru
paraTable2.Cell(a, 1).Range.Text = ds.Tables[0].Rows[i]["FieldName"].ToString();
paraTable2.Cell(a, 2).Range.Text = ds.Tables[0].Rows[i]["FieldTitle"].ToString();
paraTable2.Cell(a, 3).Range.Text = "";
paraTable2.Cell(a, 4).Range.Text = ds.Tables[0].Rows[i]["DataType"].ToString() == "Integer" ? "INT" : "VARCHAR(" + ds.Tables[0].Rows[i]["DataLen"].ToString() + ")";
paraTable2.Cell(a, 5).Range.Text = "0";
paraTable2.Cell(a, 6).Range.Text = "0";
//注意嵌套表时只能一行一行的插入
paraTable2.Rows.Add(ref oMissing);//添加一行
a++;
}
oTable.Cell(4, 1).Range.Text = "rtnData参数数据";
oTable.Cell(4, 2).Range.Text = "null";
paraTable.Rows.First.Shading.Texture = WordAPP.WdTextureIndex.wdTexture25Percent;//设置阴影
paraTable.Rows.First.Range.Font.Bold = 1;
oTable.Cell(5, 1).Range.Text = "备注";
oTable.Cell(5, 2).Range.Text = "";
oWord.Visible = true; //打开生成的word
oDoc.Activate();
}
}
catch (Exception )
{
//抛出异常
return;
}
}
这个没有保存在本地,只是把编辑的word打开了