C# 创建word文档(DOCX) 并在word文档中添加“内容控件”

guoyun911 2011-08-22 11:56:15
如题:
“内容控件”是OFFICE2007以上版本中“开发人员工具”里面的东西,现在希望通过程序生成一个DOCX文档,
里面会有日期型内容控件,下拉列表型内容控件等等。
目前生成word文档我查到的资料只有Microsoft.Office.Interop.Word来创建,但是创建“内容控件”这种东西
貌似只能用Microsoft.Office.Tools.Word里面的RichTextContentControl等东西来创建,希望搞过的大哥大姐能提供
解决方案,小弟在此谢过。。。分貌似不多了,所有的分一起上了。。。
...全文
748 32 打赏 收藏 转发到动态 举报
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
Luoable 2011-08-26
  • 打赏
  • 举报
回复
呵呵,70%的功能可以录制宏,查看宏的代码可以转换的。
sulele_2009 2011-08-25
  • 打赏
  • 举报
回复
帮顶,期待牛人出面摆平。给力
guoyun911 2011-08-25
  • 打赏
  • 举报
回复
感谢Luoable提供的最终解决方案。。。没想到这么简单,我被Microsoft.Office.Tools.Word这个命名控件给迷惑了。。灰常灰常的感谢,至于后面我追问的问题,自己解决了!不用大牛费心了。

同时也感谢zyloveyrf的多次帮助,虽然没有解决核心问题,但精神同样感谢。

最后感谢帮顶的几位大哥们,没有你们帮顶,Luoable估计也不会看到我的提问贴了!

呵呵,最后结贴并没有把所有分全部送给Luoable,拿出一点点感谢下zyloveyrf吧!HOHO
guoyun911 2011-08-25
  • 打赏
  • 举报
回复
[Quote=引用 27 楼 luoable 的回复:]
ContentControl control = Application.ActiveDocument.ContentControls.Add(WdContentControlType.wdContentControlRichText);
control.Range.Text = "test";

ContentControl control1 = Application.Activ……
[/Quote]
追问:
如果在标签处插入内容控件,该如何实现呢?
ContentControl control = Application.ActiveDocument.ContentControls.Add(WdContentControlType.wdContentControlRichText);
这个方法其实还缺一个参数,是ref Range。
希望得到一个比较详细的代码!谢谢,谢谢!
再次拜谢!
guoyun911 2011-08-25
  • 打赏
  • 举报
回复
[Quote=引用 27 楼 luoable 的回复:]
ContentControl control = Application.ActiveDocument.ContentControls.Add(WdContentControlType.wdContentControlRichText);
control.Range.Text = "test";

ContentControl control1 = Application.Activ……
[/Quote]

待测试,谢谢提供方案!如果有效,分全部奉上!
guoyun911 2011-08-24
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 xyl_ztj 的回复:]
case 5:
CCU_StrCpy(stCommParam[5].cPortName, "/dev/ttyV0");
stCommParam[5].iBaudRate = B9600;
break;
default:
break;
[4].cPortName, "/dev/ttyS0");
[3].cPortName, "/dev/ttyS1");
[……
[/Quote]

这是什么东东?看不懂
Luoable 2011-08-24
  • 打赏
  • 举报
回复
ContentControl control = Application.ActiveDocument.ContentControls.Add(WdContentControlType.wdContentControlRichText);
control.Range.Text = "test";

ContentControl control1 = Application.ActiveDocument
.ContentControls.Add(WdContentControlType.wdContentControlComboBox);
control1.DropdownListEntries.Add("测试");

是这个要求吧~?
十五字 2011-08-24
  • 打赏
  • 举报
回复
帮顶下吧
guoyun911 2011-08-24
  • 打赏
  • 举报
回复
看来问题沉了,顶起来
guoyun911 2011-08-23
  • 打赏
  • 举报
回复
期待头像下面N多勋章红花的大牛们也参与讨论
萧炎 2011-08-23
  • 打赏
  • 举报
回复
哦 我以前做的就是我最开始的那个 呵呵 帮顶期待高手
guoyun911 2011-08-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 guoyun911 的回复:]
目前实现思路:

C# code


object Nothing = System.Reflection.Missing.Value;
//创建Microsoft.Office.Interop.Word.Application myWordApp
Microsoft.Office.Interop.Word.Application MyWrodApp = new Microsoft.……
[/Quote]

但是我这里想把创建的新的Microsoft.Office.Interop.Word.Document 对象转换为
Microsoft.Office.Tools.Word.Document 对象为null。导致下面的操作无法继续,其实我也不知道我这样做对不对,还望高手指教
guoyun911 2011-08-23
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 zyloveyrf 的回复:]
这个你去看看
[/Quote]

感谢鼎力相助,但是用
Microsoft.Office.Interop.Word.Document 是满足不了使用“内容控件”的需求的!在MSDN上的office开发中得到信息,说如果想使用“内容控件”就得使用Microsoft.Office.Tools.Word.Document 类来调用AddRichTextContentContrl()方法实现“内容控件”的创建!
萧炎 2011-08-23
  • 打赏
  • 举报
回复
萧炎 2011-08-23
  • 打赏
  • 举报
回复
    public bool CreateWordFile(string _filename, "数据List或者你C#要写的数据")          {                         #region 开始生成Word              try              {                    string strtitle = "任务导出";                  object oEndOfDoc = "\\endofdoc";                  Object Nothing = System.Reflection.Missing.Value;                  Object filename = _filename;                  //创建Word文档                    Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();                  Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);                      //设置页眉                  WordApp.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;                  WordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;                  WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("WPFOA任务导出");                  WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐                  WordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;//跳出页眉设置                  //任务导出------名字                  Word.Paragraph oPara1;                  oPara1 = WordDoc.Content.Paragraphs.Add(ref Nothing);                  oPara1.Range.Text = strtitle;                  oPara1.Range.Font.Bold = 1;                  oPara1.Range.Font.Name = "宋体";                  oPara1.Range.Font.Size = 20;                  oPara1.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;                  oPara1.Format.SpaceAfter = 5;    //24 pt spacing after paragraph.                  oPara1.Range.InsertParagraphAfter();                  #region 循环每个表                    foreach (var v in lst_task)                  {                      #region 循环每一个列,产生一行数据                      //描述信息                      Word.Paragraph oPara3;                      object oRng = WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;                      oPara3 = WordDoc.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 = Word.WdParagraphAlignment.wdAlignParagraphCenter;                      oPara3.Format.SpaceBefore = 1;                      oPara3.Format.SpaceAfter = 1;                      oPara3.Range.InsertParagraphAfter();                        //插入表格                                Word.Table newTable;                      Word.Range wrdRng = WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;                      newTable = WordDoc.Tables.Add(wrdRng, 5, 4, ref Nothing, ref Nothing);                      newTable.Columns[1].Width = 60;                      newTable.Columns[2].Width = 145;                      newTable.Columns[3].Width = 80;                      newTable.Columns[4].Width = 145;                      newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;                      newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;                        //填充表格内容                      newTable.Cell(1, 1).Range.Text = "项目名称";                     newTable.Cell(1, 2).Range.Text = "1";                      newTable.Cell(1, 3).Range.Text = "模块名称";                      newTable.Cell(1, 4).Range.Text = "2";                      newTable.Cell(2, 1).Range.Text = "负责人名";                      newTable.Cell(2, 2).Range.Text = "3";                      newTable.Cell(2, 3).Range.Text = "状态";                      newTable.Cell(2, 4).Range.Text = "4";                      newTable.Cell(3, 1).Range.Text = "发布时间";                      newTable.Cell(3, 2).Range.Text = "5";                      newTable.Cell(3, 3).Range.Text = "要求完成时间";                      newTable.Cell(3, 4).Range.Text = "6";                      newTable.Cell(4, 1).Range.Text = "完成时间";                      newTable.Cell(4, 2).Range.Text = "7";                      newTable.Cell(4, 3).Range.Text = "分配人";                      newTable.Cell(4, 4).Range.Text = "8";                      newTable.Cell(5, 1).Range.Text = "任务描述";                      newTable.Cell(5, 2).Merge(newTable.Cell(5, 4));                      newTable.Cell(5, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;                      newTable.Cell(5, 2).Range.Text = "12313213123213";                      #endregion                  }                      WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing,                   ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,                   ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,                   ref Nothing, ref Nothing);                    WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);                  WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);                  #endregion                  //关闭WinWord进程                  System.Diagnostics.Process[] MyProcess = System.Diagnostics.Process.GetProcessesByName("WINWORD");                  MyProcess[0].Kill();                  return true;                }              catch              {                    System.Diagnostics.Process[] MyProcess = System.Diagnostics.Process.GetProcessesByName("WINWORD");                  MyProcess[0].Kill();                  return false;              }              #endregion          }  
guoyun911 2011-08-23
  • 打赏
  • 举报
回复
等待大神的到来!
guoyun911 2011-08-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zyloveyrf 的回复:]
using Microsoft.Office.Interop;


private void button1_Click(object sender, EventArgs e)
{

//将内容输出到word文档中
object oMissing = System.Reflection.Missing.Value;
//创建一个Word应用程序实例
……
[/Quote]

谢谢你的代码,但是用DOCX模板和书签插入的方式已经尝试过,失败了。。。内容控件中插入书签貌似不行。。。还有一点,如果是日期型的内容控件,在OFFICE里根本就不能插入书签。。相当纠结!继续跪求解决方案!
萧炎 2011-08-23
  • 打赏
  • 举报
回复
using Microsoft.Office.Interop;


private void button1_Click(object sender, EventArgs e)
{

//将内容输出到word文档中
object oMissing = System.Reflection.Missing.Value;
//创建一个Word应用程序实例
Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application();
//设置为不可见
oWord.Visible = false;
//模板文件地址,这里假设在X盘根目录
object oTemplate = "L://person2.dotx";//2007版本
//以模板为基础生成文档
Microsoft.Office.Interop.Word._Document oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);
//声明书签数组
object[] oBookMark = new object[5];
//赋值书签名
oBookMark[0] = "beizhu";
oBookMark[1] = "name";
oBookMark[2] = "sex";
oBookMark[3] = "birthday";
oBookMark[4] = "hometown";

//赋值任意数据到书签的位置
oDoc.Bookmarks.get_Item(ref oBookMark[0]).Range.Text = "使用模板实现Word生成";
oDoc.Bookmarks.get_Item(ref oBookMark[1]).Range.Text = "李四";
oDoc.Bookmarks.get_Item(ref oBookMark[2]).Range.Text = "女";
oDoc.Bookmarks.get_Item(ref oBookMark[3]).Range.Text = "1987.06.07";
oDoc.Bookmarks.get_Item(ref oBookMark[4]).Range.Text = "贺州";

//弹出保存文件对话框,保存生成的Word
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Word Document(*.doc)|*.doc";
sfd.DefaultExt = "Word Document(*.doc)|*.doc";
if (sfd.ShowDialog() == DialogResult.OK)
{
object filename = sfd.FileName;

oDoc.SaveAs(ref filename, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
//关闭word
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
}

}
guoyun911 2011-08-23
  • 打赏
  • 举报
回复
目前实现思路:

object Nothing = System.Reflection.Missing.Value;
//创建Microsoft.Office.Interop.Word.Application myWordApp
Microsoft.Office.Interop.Word.Application MyWrodApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document tempdoc = myWordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);
//下面这句执行后doc对象为null导致后续代码无法执行!
Microsoft.Office.Tools.Word.Document doc = tempdoc.GetVstoObject();
guoyun911 2011-08-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zyloveyrf 的回复:]
帮顶 我做的是xlsx
[/Quote]

求xlsx的实现方案,也许可以用在docx上!谢谢!
加载更多回复(12)

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧