//生成word程序对象
MyWord.Application app = new MyWord.Application();
//生成documnet对象
MyWord.Document doc = new MyWord.Document();
object missing = System.Reflection.Missing.Value;
try
{
//模板文件
string TemplateFile = HttpContext.Current.Server.MapPath("~/FileUpload/WordTemplate/上行文_归档.dot");
//生成的具有模板样式的新文件
string FileName = HttpContext.Current.Server.MapPath("~/FileUpload/Archiving/" fileName "/" _dirName.Trim() "/" fileTitle Convert.ToDateTime(drafttime).ToString("yyyy年MM月dd") ".doc");
//模板文件拷贝到新文件
File.Copy(TemplateFile, FileName, true);
object Obj_FileName = FileName;
object Visible = false;
object ReadOnly = false;
//打开文件
doc = app.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref Visible,
ref missing, ref missing, ref missing,
ref missing);
doc.Activate();
System.Collections.HashtablehtBookmark = GetArchiveHashTable();
#region 处理文档 将数据生成到word
foreach (DictionaryEntry de in htBookmark)
{
object WordMarkName = de.Key;//word模板中的书签名称
string InsertText = string.Empty;
if (de.Key.ToString() == "发文年")
{
InsertText = Convert.ToDateTime(dtDispatch.Rows[0][de.Value.ToString()]).Year.ToString();
}
else if (de.Key.ToString() == "缓急")
{
switch (dtDispatch.Rows[0][de.Value.ToString()].ToString())
{
case "0": InsertText = "平急"; break;
case "1": InsertText = "特急"; break;
case "2": InsertText = "加急"; break;
case "3": InsertText = "特提"; break;
default: InsertText = "平急"; break;
}
}
else if (de.Key.ToString()=="主送")
{
DataTable dt = logicUser.GetTable("SELECT Typeid,'各' TypeName AS TypeName FROM dbo.SectionType WHERE IsOffice=0");
foreach (DataRow item in dt.Rows)
{
if (item["TypeName"].ToString() != dtDispatch.Rows[0][de.Value.ToString()].ToString())
{
InsertText = "有关车间及有关科室";
}
else
{
InsertText = item["TypeName"].ToString();
}
}
}
else if (de.Key.ToString() == "发文日期")
{
InsertText = Convert.ToDateTime(dtDispatch.Rows[0][de.Value.ToString()]).ToString("yyyy.MM.dd");
}
else if (de.Key.ToString() == "部门代号")
{
InsertText = this.GetHeaderByName(dtDispatch.Rows[0][de.Value.ToString()].ToString());
}
else if (de.Key.ToString() == "签发")
{
string dtDispat = dtDispatch.Rows[0]["signatory"].ToString();
if (!string.IsNullOrEmpty(dtDispat))
{
InsertText = HttpContext.Current.Server.MapPath(logicUser.GetInk(dtDispat));
if (File.Exists(InsertText))
{
//获取光标位置
doc.Bookmarks.get_Item(ref WordMarkName).Select();
//在标签中插入图片
doc.Bookmarks.get_Item(ref WordMarkName).Range.InlineShapes.AddPicture(InsertText);
}
else {
doc.Bookmarks.get_Item(ref WordMarkName).Range.Text = "";
} continue;
}
}
else if (de.Key.ToString() == "会签")
{
string temp = dtDispatch.Rows[0]["unionsignatory"].ToString();
if (!string.IsNullOrEmpty(temp))
{
string[] pers = temp.Split(new char[] { ',' });
foreach (string item in pers)
{
if (item != dtDispatch.Rows[0]["signatory"].ToString())
{
string picFile = HttpContext.Current.Server.MapPath(logicUser.GetInk(item));
if (File.Exists(picFile))
{
//获取光标位置
doc.Bookmarks.get_Item(ref WordMarkName).Select();
//在标签中插入图片
InlineShape shape = doc.Bookmarks.get_Item(ref WordMarkName).Range.InlineShapes.AddPicture(picFile);
}
else
{
doc.Bookmarks.get_Item(ref WordMarkName).Range.Text = "";
}
}
}
continue;
}
}
doc.Save();
object IsSave = true;
doc.Close(ref IsSave, ref missing, ref missing);
app.Application.Quit(ref missing, ref missing, ref missing);
doc = null;
app = null;}
catch (Exceptione)
{
throw;
}
finally
{
if (doc != null)
{
doc.Close(false, ref missing, ref missing);
doc = null;
}
if (app != null)
{
app.Application.Quit(ref missing, ref missing, ref missing);
app = null;
}
}
///<summary>
/// 书签名和字段对应表
///</summary>
///<returns></returns>
private Hashtable GetArchiveHashTable()
{
Hashtable markHashTable = new Hashtable();
markHashTable.Add("部门代号", "draftDept");
markHashTable.Add("文件号", "dispatchNum");
markHashTable.Add("缓急", "timeLevel");
markHashTable.Add("主送", "mailto");
markHashTable.Add("抄送", "mailcc");
markHashTable.Add("拟稿部门", "draftDept");
markHashTable.Add("段办核稿", "auditor");
markHashTable.Add("拟稿人", "draftman");
markHashTable.Add("负责人", "principalName");
markHashTable.Add("电话", "tel1");
markHashTable.Add("发文日期", "draftTime");
markHashTable.Add("标题", "topic");
markHashTable.Add("主题词", "subject");
markHashTable.Add("发文年", "draftTime");
markHashTable.Add("联系人", "draftman");
markHashTable.Add("联系电话", "tel2");
markHashTable.Add("签发", "signatory");
markHashTable.Add("会签", "unionsignatory");
return markHashTable;
}
/// <summary>
/// 读取word内容
/// </summary>
/// <param name="docFileName">路径</param>
/// <returns></returns>
public string GetText(string docFileName)
{
//实例化COM
Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
object fileobj = docFileName;
object nullobj = System.Reflection.Missing.Value;
//打开指定文件(不同版本的COM参数个数有差异,一般而言除第一个外都用nullobj就行了)
Microsoft.Office.Interop.Word.Document doc1 = wordApp.Documents.Open(ref fileobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj
);
//取得doc文件中的文本
string outText = doc1.Content.Text;
//关闭文件
doc1.Close(ref nullobj, ref nullobj, ref nullobj);
//关闭COM
wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);
//返回
return outText;
}
/// <summary>
/// 向word中追加内容
/// </summary>
/// <param name="filePath"></param>
/// <param name="contents"></param>
public void AppendContent(string filePath, string contents)
{
object fileName = filePath;
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
Word.ApplicationClass oWordApp = new Word.ApplicationClass();
Word.Document oWordDoc = oWordApp.Documents.Open(ref fileName, ref missing, ref readOnly,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing);
oWordDoc.Activate();
oWordApp.Selection.TypeText(contents);
oWordApp.Selection.TypeParagraph();
oWordDoc.Save();
}
/// <summary>
/// 合并word
/// </summary>
/// <param name="wordPath1">第一个word</param>
/// <param name="wordPath2">第二个word</param>
/// <param name="mergeWordPath">合并后的word</param>
public void MergeWord(string oFirstDoc, string wordPath2, string mergeWordPath)
{
object missing = System.Reflection.Missing.Value;
object oSecondDoc = wordPath2; ;//word檔2
object oOutputDoc = mergeWordPath;//合併檔
//object oPageBreak = Word.WdBreakType.wdLineBreak;//接下行合併(LineBreak)
object oPageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;//接下頁合併(PageBreak)
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
try
{
Microsoft.Office.Interop.Word.Document origDoc = wordApp.Documents.Open(ref oSecondDoc, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
origDoc.Activate();
wordApp.Selection.InsertFile(oFirstDoc, ref missing, ref missing, ref missing, ref missing);
wordApp.Selection.InsertBreak(ref oPageBreak);
wordApp.ActiveDocument.SaveAs(ref oOutputDoc, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}
catch (Exception)
{
throw;
}
finally
{
if (wordApp != null)
{
wordApp.ActiveDocument.Close(ref missing, ref missing, ref missing);
wordApp.Quit(ref missing, ref missing, ref missing);//加這行可以 Kill WINWORD.EXE process
wordApp = null;
}
}
}
}