请教下关于winform操作word的问题 请教下关于winform操作word的问题,我有个word模板,想在winform中填写数据,填完之后自动填入模板的相应位置,有固定的 几个模板,模板里有的地方填文字,有的地方是图片,要填入模板的数据都是从数据库中取出来的,注意不是用web browser打开一个word文档,有人知道怎么写吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 通过WORD书签设置,插入相应位置参考http://www.cnblogs.com/jinglelin/archive/2008/12/26/367586.html 可以写一个word addin程序来实现这个功能~呵呵。 我原来写的一个程序,跟这一样,给你粘贴一段,主要思路还是在模板文件里加书签,然后再在书签处插入字符串,三个函数 Doit()、 FillMarks()和ExeMark(),不是很全,你自己研究研究,记得给F哦void DoIt(){ Microsoft.Office.Interop.Word.ApplicationClass app=null; try { if (MyPublic.OutPutPath.Trim() == "") { MessageBox.Show("无效的输出目录!请重新设定之!", MyPublic.gl_cap, MessageBoxButtons.OK, MessageBoxIcon.Error); Settings fm = new Settings(); fm.ShowDialog(); fm.Dispose(); return; } string dot = System.IO.Directory.GetCurrentDirectory() + "\\Templates\\"; dot = dot + sdoc + ".dot"; string outpath = _outputpath + "\\" + dr["案号"].ToString(); if (!System.IO.Directory.Exists(outpath)) Directory.CreateDirectory(outpath); string doc = outpath + "\\" + sdoc + ".doc"; object dotobject = (object)dot; object docobject = (object)doc; bool ExistDoc = false; bool OverWrite = false ; if (System.IO.File.Exists(doc)) { ExistDoc = true; if (MessageBox.Show("文件\n\n " + doc + "\n\n已经存在!确定覆盖此文件吗?", MyPublic.gl_cap, MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK) OverWrite = true; else return; } //******************// object wd = null; try { wd = Microsoft.VisualBasic.Interaction.GetObject(null, "Word.Application"); } catch (Exception e1) { wd = Microsoft.VisualBasic.Interaction.CreateObject("Word.Application", ""); } finally { app = (Microsoft.Office.Interop.Word.ApplicationClass)wd; } int cou = app.Documents.Count; bool Is_Word_Opened = (cou > 0); //***关闭并覆盖已有文件 if (ExistDoc && OverWrite) { if (cou > 0) foreach (Document d in app.Documents) if (d.FullName == doc) ((DocumentClass)d).Close(ref m_false, ref m_false, ref m_false); System.IO.File.Delete(doc); } //*********************************// DocumentClass dc = null; string mess = ""; app.Visible = false; dc = (DocumentClass)app.Documents.Add(ref dotobject, ref m_null, ref m_null, ref m_null); FillMarks(dc, dr, dt, BookID); dc.SaveAs(ref docobject, ref m_null, ref m_null, ref m_null, ref m_null, ref m_null, ref m_null, ref m_null, ref m_null, ref m_null, ref m_null, ref m_null, ref m_null, ref m_null, ref m_null, ref m_null); mess += "\n " + doc; mess = "文件\n" + mess + "\n\n已生成,需要现在打开吗?"; DialogResult result = MessageBox.Show(mess, MyPublic.gl_cap, MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result==DialogResult.Yes) { app.Visible = true; app.Activate(); app.WindowState = WdWindowState.wdWindowStateMaximize; } else { dc.Close(ref m_false, ref m_null, ref m_null); if (app.Documents.Count == 0) { app.Quit(ref m_false, ref m_null, ref m_null); app = null; GC.Collect(); GC.WaitForPendingFinalizers(); } else app.Visible = true; } } catch (Exception ec) { MessageBox.Show("生成 Word 文件失败~\n" + ec.Message, MyPublic.gl_cap, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } finally { if (app != null) { if (app.Documents.Count == 0) { app.Quit(ref m_false, ref m_null, ref m_null); app = null; } else app.Visible = true; } GC.Collect(); GC.WaitForPendingFinalizers(); }}private static void FillMarks(DocumentClass dc, DataRow dr, lyftDataSet.tb_dangshirenDataTable dt, int BookID){ object mac; object n; string txt; n = "案号"; txt = anhao; mac = "MACROBUTTON anhao [单击此处键入案号]"; ExeMark(n, txt, mac, dc); n = "原告1"; txt = dsr[0]; mac = "MACROBUTTON anhao [单击此处键入原告]"; ExeMark(n, txt, mac, dc);}private static void ExeMark(object n, string txt, object m_marcobutton, DocumentClass dc){ object fieldtype = WdFieldType.wdFieldEmpty; Book bm; bm = dc.Books.get_Item(ref n); int a1 = bm.Range.Start; object r1 = a1; Range r = bm.Range; object u = WdUnits.wdCharacter; object x = 1; r.Move(ref u, ref x); bm.Range.InsertAfter(txt); if (r.Start > a1) a1 = r.Start - 1; else a1 = r.Start; object r2 = a1; dc.Range(ref r1, ref r2).Font.Color = WdColor.wdColorRed;} 这该怎么写多线程 vs2005组件名称显示问题 关于串口同步问题 如何获取用户控件中按钮单击事件时 的 指定值? 怎么在WebViewer中显示所有的页面吗?已经分页的页面怎么在一屏显示??? 两个数组之间的比较问题 急!!请求达人帮助!!! 在制作安装程序的时候我想把当前日期写到注册表中,请问该怎么做? 关于流的存入和读取 关于重绘的问题 我是新手。想了解一下。c# vb.net asp.net 遍历控件的问题!! ListBox有个属性MultiColumn可以显示多列,但怎样指定显示列的数量,比如显示为两列或三列
参考
http://www.cnblogs.com/jinglelin/archive/2008/12/26/367586.html
{ Microsoft.Office.Interop.Word.ApplicationClass app=null;
try
{
if (MyPublic.OutPutPath.Trim() == "")
{
MessageBox.Show("无效的输出目录!请重新设定之!", MyPublic.gl_cap, MessageBoxButtons.OK, MessageBoxIcon.Error);
Settings fm = new Settings();
fm.ShowDialog(); fm.Dispose();
return;
} string dot = System.IO.Directory.GetCurrentDirectory() + "\\Templates\\";
dot = dot + sdoc + ".dot"; string outpath = _outputpath + "\\" + dr["案号"].ToString();
if (!System.IO.Directory.Exists(outpath)) Directory.CreateDirectory(outpath); string doc = outpath + "\\" + sdoc + ".doc";
object dotobject = (object)dot;
object docobject = (object)doc; bool ExistDoc = false; bool OverWrite = false ;
if (System.IO.File.Exists(doc))
{
ExistDoc = true;
if (MessageBox.Show("文件\n\n " + doc + "\n\n已经存在!确定覆盖此文件吗?", MyPublic.gl_cap, MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)
OverWrite = true;
else return;
} //******************//
object wd = null;
try { wd = Microsoft.VisualBasic.Interaction.GetObject(null, "Word.Application"); }
catch (Exception e1) { wd = Microsoft.VisualBasic.Interaction.CreateObject("Word.Application", ""); }
finally { app = (Microsoft.Office.Interop.Word.ApplicationClass)wd; } int cou = app.Documents.Count;
bool Is_Word_Opened = (cou > 0); //***关闭并覆盖已有文件
if (ExistDoc && OverWrite)
{
if (cou > 0)
foreach (Document d in app.Documents)
if (d.FullName == doc) ((DocumentClass)d).Close(ref m_false, ref m_false, ref m_false);
System.IO.File.Delete(doc);
} //*********************************// DocumentClass dc = null;
string mess = "";
app.Visible = false;
dc = (DocumentClass)app.Documents.Add(ref dotobject, ref m_null, ref m_null, ref m_null);
FillMarks(dc, dr, dt, BookID); dc.SaveAs(ref docobject, ref m_null, ref m_null, ref m_null, ref m_null, ref m_null, ref m_null, ref m_null, ref m_null, ref m_null, ref m_null, ref m_null, ref m_null, ref m_null, ref m_null, ref m_null);
mess += "\n " + doc; mess = "文件\n" + mess + "\n\n已生成,需要现在打开吗?";
DialogResult result = MessageBox.Show(mess, MyPublic.gl_cap, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result==DialogResult.Yes)
{
app.Visible = true;
app.Activate();
app.WindowState = WdWindowState.wdWindowStateMaximize;
}
else
{
dc.Close(ref m_false, ref m_null, ref m_null);
if (app.Documents.Count == 0)
{
app.Quit(ref m_false, ref m_null, ref m_null);
app = null;
GC.Collect(); GC.WaitForPendingFinalizers();
}
else app.Visible = true;
}
}
catch (Exception ec)
{
MessageBox.Show("生成 Word 文件失败~\n" + ec.Message, MyPublic.gl_cap, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
finally
{
if (app != null)
{
if (app.Documents.Count == 0)
{
app.Quit(ref m_false, ref m_null, ref m_null);
app = null;
}
else app.Visible = true;
}
GC.Collect(); GC.WaitForPendingFinalizers();
}}private static void FillMarks(DocumentClass dc, DataRow dr, lyftDataSet.tb_dangshirenDataTable dt, int BookID)
{
object mac; object n; string txt;
n = "案号"; txt = anhao; mac = "MACROBUTTON anhao [单击此处键入案号]"; ExeMark(n, txt, mac, dc);
n = "原告1"; txt = dsr[0]; mac = "MACROBUTTON anhao [单击此处键入原告]"; ExeMark(n, txt, mac, dc);
}private static void ExeMark(object n, string txt, object m_marcobutton, DocumentClass dc)
{
object fieldtype = WdFieldType.wdFieldEmpty;
Book bm;
bm = dc.Books.get_Item(ref n);
int a1 = bm.Range.Start;
object r1 = a1;
Range r = bm.Range;
object u = WdUnits.wdCharacter;
object x = 1;
r.Move(ref u, ref x);
bm.Range.InsertAfter(txt); if (r.Start > a1) a1 = r.Start - 1;
else a1 = r.Start;
object r2 = a1;
dc.Range(ref r1, ref r2).Font.Color = WdColor.wdColorRed;
}