• c# 操作word


    //生成WORD程序对象和WORD文档对象 Microsoft.Office.Interop.Word.Application appWord = new Application(); Microsoft.Office.Interop.Word.Document doc = new Document(); object oMissing = System.Reflection.Missing.Value;//这个是什么东西,我始终没搞明白-_- //打开模板文档,并指定doc的文档类型 object objTemplate = Server.MapPath(p_TemplatePath); object objDocType = WdDocumentType.wdTypeDocument; doc = (Document)appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue); //获取模板中所有的书签 Bookmarks odf = doc.Bookmarks; string[] testTableremarks = { "ApplyNo", "AuditingDate", "Auditor", "CheckDate", "Checker"}; string[] testTablevalues = { "ApplyNo", "AuditingDate", "Auditor", "CheckDate", "Checker",}; //循环所有的书签,并给书签赋值 for (int oIndex = 0; oIndex < testTableremarks.Length; oIndex++) { obDD_Name = WD + testTableremarks[oIndex]; doc.Bookmarks.get_Item(ref obDD_Name).Range.Text = p_TestReportTable.Rows[0][testTablevalues [oIndex]].ToString();//此处Range也是WORD中很重要的一个对象,就是当前操作参数所在的区域 } //第四步 生成word,将当前的文档对象另存为指定的路径,然后关闭doc对象。关闭应用程序 object filename = Server.MapPath(p_SavePath) + "\\Testing_" + DateTime.Now.ToShortDateString() + ".doc"; object miss = System.Reflection.Missing.Value; doc.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss); object missingValue = Type.Missing; object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges; doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue); appWord.Application.Quit(ref miss, ref miss, ref miss); doc = null; appWord = null; this.Hid_ShowMessage.Value = "生成成功!";
    上述代码就是一个通过模板文件生成WORD的过程。其实也就是一个替换书签内容的过程。 在开发的过程中,有些数据是动态增加的,假如我要向一个表格中动态的添加几行数据,就无法用替换书签的方式来进行操作,需要通过程序在文档页面的表格中添加行。 向表格中添加行,有两种操作形式:一种是在WORD模板中已经存在了一个表格。一种是我们在程序中直接添加一个表格对象。 第一种情况下,需要注意:在WORD模板中要操作的表格中,不能有纵向合并的单元格,不然程序无法获取到当前要操作对象导致程序报错.单元格的合并,我们可以在程序中控制。 第二种情况下就需要我们通过程序去直接添加表格了。 生成表格的代码具体如下: 1.获取文档中已存在的表格: Microsoft.Office.Interop.Word.Table characterTable = doc.Tables[2];//在document对象的集合操作中,起始点是从1开始,并不是从0开始的,此处需要注意。 2.在文档中直接生成表格,首先要获取插入表格的位置,然后添加表格对象: object oEndOfDoc = "\\endofdoc";//WORD中预定义的书签,还有很多,此处就不一一列举。 object oMissing = System.Reflection.Missing.Value; Range wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range;//获取当前文档的末尾位置。 wrdRng.InsertAfter(" ");//插入一行,此处不能用 wrdRng.InsertAfter(""),如果用这个,就不能换行,我也不知道为什么。

    object oCollapseEnd = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd; object oPageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;//分页符 wrdRng.Collapse(ref oCollapseEnd); wrdRng.InsertBreak(ref oPageBreak);//插入了一页 wrdRng.Collapse(ref oCollapseEnd); wrdRng.InsertAfter("图片信息"); wrdRng.Font.Size = 20;//指定操作对象的文字大小 wrdRng.Font.Bold = 1;//指定操作对象的粗体:1为粗体,0为正常 wrdRng.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//指定操作区域的文字布局:居中对齐 //上述代码的意思是:找到当前的末尾位置,然后插入一个分页符,相当于跳到了一个新页,在这个新页的顶端写入文字“图片信息”,并指定文字大小为20,粗体居中显示。 wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range; wrdRng.InsertAfter(" "); wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range; wrdRng.InsertParagraphAfter();//插入一个段落,在此段落上插入一个2行一列的表格。 Microsoft.Office.Interop.Word.Table newTable = doc.Tables.Add(wrdRng, 2, 1, ref oMissing, ref oMissing);

    我们还可以对表格进行格式设置,此处我们就不在一一列举。 3.下面我们分析一下对表格的单元格的操作:合并,拆分。这个就需要我们根据实际表格来进行操作: //获取具体的某个单元格(1,1),获取第一行第一列的单元格 Cell cell = doc.Tables[1].Cell(1,1); cell.Range.Text="Text";//指定当前单元格的内容为Text 在Table的操作中,添加新行: object beforeRow = doc.Tables[1].Rows[2];//此行是先获取到第二行 doc.Tables[1].Rows.Add(ref beforeRow);//效果类似于在WORD中此表格的第二行上进行【插入行】操作,插入的新行将会插入到当前行的上一行中,格式也是和此行一致的。 //合并单元格:感觉在此处合并单元格挺傻瓜的,你只需要指定你要合并的起始单元格和结束单元格,然后通过Merge操作就行了 Cell cell = doc.Tables[1].Cell(iRow, 2);//列合并 cell.Merge(doc.Tables[1].Cell(iRow, 6)); Cell cell1 = doc.Tables[1].Cell(iRow - 1, 1);//行合并 cell1.Merge(doc.Tables[1].Cell(iRow + 1, 1)); 上述操作就是在此程序中用到的一些知识点,还有好多的东西需要去熟悉、理解。 另外,在程序的测试过程中发现,当执行一次文档生成后,在资源管理器中始终有winword.exe进程杀不掉,目前的解决办法是:直接杀进程,代码如下:

    protected void killAllProcess() // 杀掉所有winword.exe进程 { System.Diagnostics.Process[] myPs; myPs = System.Diagnostics.Process.GetProcesses(); foreach (System.Diagnostics.Process p in myPs) { if (p.Id != 0) { string myS = "WINWORD.EXE" + p.ProcessName + " ID:" + p.Id.ToString(); try { if (p.Modules != null) if (p.Modules.Count > 0) { System.Diagnostics.ProcessModule pm = p.Modules[0]; myS += "\n Modules[0].FileName:" + pm.FileName; myS += "\n Modules[0].ModuleName:" + pm.ModuleName; myS += "\n Modules[0].FileVersionInfo:\n" + pm.FileVersionInfo.ToString(); if (pm.ModuleName.ToLower() == "winword.exe") p.Kill(); } } catch { } finally { } } } }

    目前为止,一个WORD文档就生成了。上述为我在这个程序开发中遇到的问题和解决方法,可能有好多地方都是考虑不全的,如果在程序开发中对WORD的操作有新的认识的话,欢迎和我沟通交流,彼此提高! 下边是在网上一些比较好的摘抄: 创建新Word

    object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

    打开文档:

    object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; object fileName = @"E:CCCXCXXTestDoc.doc"; oDoc = oWord.Documents.Open(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);

    导入模板

    object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; object fileName = @"E:XXXCCXTest.doc"; oDoc = oWord.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref oMissing);

    .添加新表

    object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); object start = 0; object end = 0; Word.Range tableLocation = oDoc.Range(ref start, ref end); oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);

    .表插入行

    object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); object start = 0; object end = 0; Word.Range tableLocation = oDoc.Range(ref start, ref end); oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); Word.Table newTable = oDoc.Tables[1]; object beforeRow = newTable.Rows[1]; newTable.Rows.Add(ref beforeRow);

    .单元格合并

    object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); object start = 0; object end = 0; Word.Range tableLocation = oDoc.Range(ref start, ref end); oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); Word.Table newTable = oDoc.Tables[1]; object beforeRow = newTable.Rows[1]; newTable.Rows.Add(ref beforeRow); Word.Cell cell = newTable.Cell(1, 1); cell.Merge(newTable.Cell(1, 2));

    .单元格分离

    object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add( oMissing, ref oMissing, ref oMissing); object start = 0; object end = 0; Word.Range tableLocation = oDoc.Range(ref start, ref end); oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); Word.Table newTable = oDoc.Tables[1]; object beforeRow = newTable.Rows[1]; newTable.Rows.Add(ref beforeRow); Word.Cell cell = newTable.Cell(1, 1); cell.Merge(newTable.Cell(1, 2)); object Rownum = 2; object Columnnum = 2; cell.Split(ref Rownum, ref Columnnum);

    通过段落控制插入

    object oMissing = System.Reflection.Missing.Value; object oEndOfDoc = "\endofdoc"; /**//* endofdoc is a predefined bookmark */ //Start Word and create a new document. Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); //Insert a paragraph at the beginning of the document. Word.Paragraph oPara1; oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing); oPara1.Range.Text = "Heading 1"; oPara1.Range.Font.Bold = 1; oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph. oPara1.Range.InsertParagraphAfter();

  • 相关阅读:
    webStrom 注释模板添加
    匹配正则 url 端口 域名
    监测数据类型封装方法
    base64图片展示(后端给base64数据,前端展示图片)
    倒计时
    机密16位
    mvc与mvvm的区别
    flex表格的使用
    flex中tab页面的实现
    flex中下拉框的实现
  • 原文地址:https://www.cnblogs.com/lann/p/3074447.html
Copyright © 2020-2023  润新知