using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Text; using System.Data; using Microsoft.Office.Interop.Word; using System.Runtime.InteropServices; using System.Reflection; using Microsoft.Office.Core; using System.IO; /// <summary> ///Report 的摘要說明 /// </summary> public class Report { private _Application wordApp=null; private _Document wordDoc=null; public _Application Application { get { return wordApp; } set { wordApp = value; } } public _Document Document { get { return wordDoc; } set { wordDoc = value; } } //*******通過模版創建新文檔 public void CreateNewDocument(string filePath) { killWinWordProcess(); wordApp = new ApplicationClass(); wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone; wordApp.Visible = false; object missing = System.Reflection.Missing.Value; object templateName = filePath; wordDoc = wordApp.Documents.Open(ref templateName, 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); } public void killWinWordProcess() { try { System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WINWORD"); foreach (System.Diagnostics.Process process in processes) { bool b = process.MainWindowTitle == ""; if (process.MainWindowTitle == "") { process.Kill(); } } } catch (Exception ee) { } } //保存新文檔 public void SaveDocument(string filePath) { object fileName = filePath; object format = WdSaveFormat.wdFormatDocument; object miss = System.Reflection.Missing.Value; wordDoc.SaveAs(ref fileName, ref format, 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); //wordDoc.SaveAs2(ref fileName, ref format, 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 SaveChanges = WdSaveOptions.wdSaveChanges; object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat; object RouteDocument = false; wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument); wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument); } //在書簽處插入值 public bool InsertValue(string bookmark, string value) { object bkObj = bookmark; if (wordApp.ActiveDocument.Bookmarks.Exists(bookmark)) { //wordApp.ActiveDocument.Bookmarks[ref bkOb // wordApp.ActiveDocument.Bookmarks[bookmark].Select(); wordApp.ActiveDocument.Bookmarks.get_Item(ref bkObj).Select(); wordApp.Selection.TypeText(value); return true; } return false; } public void InsertPicture(string bookmark, string picturePath, float width, float height, int index) { object miss = System.Reflection.Missing.Value; object oStart = bookmark; Object LinkToFile = false; Object savewithDocument = true; object rangs = wordApp.ActiveDocument.Bookmarks.get_Item(ref oStart).Range; wordDoc.InlineShapes.AddPicture(picturePath, ref LinkToFile, ref savewithDocument, ref rangs); // var obj = Application.ActiveDocument.InlineShapes[1]; wordDoc.Application.ActiveDocument.InlineShapes[index].Width = width; wordDoc.Application.ActiveDocument.InlineShapes[index].Height = height; } public void InserCell(string Ptname,string x, string y, string z) { int count = wordDoc.Content.Tables[1].Rows.Count; string ss=""; for (int i = 3; i < count; i++) { string text = wordDoc.Content.Tables[1].Cell(i, 1).Range.Text.ToString(); if (text.Substring(0,text.Length-2) == "") { wordDoc.Content.Tables[1].Cell(i, 1).Range.Text = Ptname; wordDoc.Content.Tables[1].Cell(i, 2).Range.Text =x; wordDoc.Content.Tables[1].Cell(i, 3).Range.Text =y; wordDoc.Content.Tables[1].Cell(i, 4).Range.Text =z; break; } } } public void CopyTale() { wordApp.Selection.MoveEnd(); object miss = System.Reflection.Missing.Value; object count = wordDoc.Paragraphs.Count; object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdParagraph; wordApp.Selection.MoveDown(ref WdLine, ref count, ref miss); wordApp.Selection.TypeParagraph(); wordApp.Selection.MoveEnd(); object count1 = wordDoc.Paragraphs.Count; object WdLine1 = Microsoft.Office.Interop.Word.WdUnits.wdParagraph; wordApp.Selection.MoveDown(ref WdLine, ref count1, ref miss); wordApp.Selection.TypeParagraph(); object count2 = wordDoc.Paragraphs.Count; object WdLine2= Microsoft.Office.Interop.Word.WdUnits.wdParagraph; wordApp.Selection.MoveDown(ref WdLine, ref count2, ref miss); wordApp.Selection.TypeParagraph(); object count3 = wordDoc.Paragraphs.Count; object WdLine3 = Microsoft.Office.Interop.Word.WdUnits.wdParagraph; wordApp.Selection.MoveDown(ref WdLine, ref count3, ref miss); wordApp.Selection.TypeParagraph(); object count4 = wordDoc.Paragraphs.Count; // wordDoc.Tables[1].Select(); // wordApp.Selection.Copy(); // wordApp.Selection.MoveDown(ref WdLine, ref count, ref miss); // wordApp.Selection.Paste(); } public void Copupage(int num) { //復制模板中的table 到下一頁 for(int i=0;i<num;i++) { wordDoc.Tables[1].Select(); wordApp.Selection.Copy(); object mymissing = System.Reflection.Missing.Value; object myunit = Microsoft.Office.Interop.Word.WdUnits.wdStory; wordApp.Selection.EndKey(ref myunit,ref mymissing); object pBreak=Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak; wordApp.Selection.InsertBreak(ref pBreak); wordApp.Selection.Paste(); Microsoft.Office.Interop.Word.Paragraph opara1; opara1 = wordDoc.Content.Paragraphs.Add(ref mymissing); opara1.Range.Text = " "; // opara1.Format.SpaceAfter = 24; opara1.Range.InsertParagraphAfter(); wordDoc.Tables[2].Select(); wordApp.Selection.Copy(); object dumm = System.Reflection.Missing.Value; object what = WdGoToItem.wdGoToLine; object which = WdGoToDirection.wdGoToLast; object count = 99999999; wordApp.Selection.GoTo(ref what, ref which, ref count, ref dumm); wordApp.Selection.Paste(); } } public void PutDateTableIntoWord( System.Data.DataTable dt) { int Dtcount = dt.Rows.Count; for(int i=0;i<Dtcount;i++) { string NAME = dt.Rows[i]["NAME"].ToString(); string LOCATION = dt.Rows[i]["LOCATION"].ToString(); string TARGETTYPE = dt.Rows[i]["TARGETTYPE"].ToString(); string SIGNMATERIAL = dt.Rows[i]["SIGNMATERIAL"].ToString(); string GRADE = dt.Rows[i]["GRADE"].ToString(); string SURVEYTIME = dt.Rows[i]["SURVEYTIME"].ToString(); string DISTANCE1 = dt.Rows[i]["DISTANCE1"].ToString(); string DISTANCE2 = dt.Rows[i]["DISTANCE2"].ToString(); string DISTANCE3 = dt.Rows[i]["DISTANCE3"].ToString(); string SURVEYUNIT = dt.Rows[i]["SURVEYUNIT"].ToString(); string BURYPERSON = dt.Rows[i]["BURYPERSON"].ToString(); string SURVEYPERSON = dt.Rows[i]["SURVEYPERSON"].ToString(); string INSPECTOR = dt.Rows[i]["INSPECTOR"].ToString(); int j = i + 1; wordDoc.Tables[j].Cell(1,2).Range.Text = NAME; wordDoc.Tables[j].Cell(1,4).Range.Text = LOCATION; wordDoc.Tables[j].Cell(2,5).Range.Text = DISTANCE1; wordDoc.Tables[j].Cell(2,7).Range.Text = TARGETTYPE; wordDoc.Tables[j].Cell(3,5).Range.Text = DISTANCE2; wordDoc.Tables[j].Cell(3,7).Range.Text = SIGNMATERIAL; wordDoc.Tables[j].Cell(4,5).Range.Text = DISTANCE3; wordDoc.Tables[j].Cell(4,7).Range.Text = GRADE; wordDoc.Tables[j].Cell(5,7).Range.Text = SURVEYTIME; wordDoc.Tables[j].Cell(7,4).Range.Text = SURVEYUNIT; wordDoc.Tables[j].Cell(8,4).Range.Text = BURYPERSON; wordDoc.Tables[j].Cell(9,4).Range.Text = SURVEYPERSON; wordDoc.Tables[j].Cell(10,4).Range.Text = INSPECTOR; wordDoc.Tables[j].Cell(2, 2).Select(); string pictureName1 = System.Web.HttpContext.Current.Server.MapPath("Ext407test/image/" + NAME + "-2.jpg"); object LinkToFile = false; object SaveWithDocument = true; object Anchor1 = wordDoc.Application.Selection.Range; if (File.Exists(pictureName1)) { //report.InsertPicture("bookMark_picLJFS", picPth1, 150, 100, 1); wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(pictureName1, ref LinkToFile, ref SaveWithDocument, ref Anchor1); int index = Application.ActiveDocument.InlineShapes.Count; Application.ActiveDocument.InlineShapes[index].Width = 100f; Application.ActiveDocument.InlineShapes[index].Height = 62f; } wordDoc.Tables[j].Cell(6, 2).Select(); object Anchor2 = wordDoc.Application.Selection.Range; string pictureName2 = System.Web.HttpContext.Current.Server.MapPath("Ext407test/image/" + NAME + "-1.jpg"); if (File.Exists(pictureName2)) { //report.InsertPicture("bookMark_picLJFS", picPth1, 150, 100, 1); wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(pictureName2, ref LinkToFile, ref SaveWithDocument, ref Anchor2); int index = Application.ActiveDocument.InlineShapes.Count; Application.ActiveDocument.InlineShapes[index].Width = 200f; Application.ActiveDocument.InlineShapes[index].Height = 170f; } wordDoc.Tables[j].Cell(6, 4).Select(); object Anchor3 = wordDoc.Application.Selection.Range; string pictureName3 = System.Web.HttpContext.Current.Server.MapPath("Ext407test/image/" + NAME + "-3.jpg"); if (File.Exists(pictureName3)) { //report.InsertPicture("bookMark_picLJFS", picPth1, 150, 100, 1); wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(pictureName3, ref LinkToFile, ref SaveWithDocument, ref Anchor3); int index = Application.ActiveDocument.InlineShapes.Count; Application.ActiveDocument.InlineShapes[index].Width = 100f; Application.ActiveDocument.InlineShapes[index].Height = 100f; } } } public void PutXYZIntoWord(string names) { } public string insetvalue() { int cnt= wordDoc.Content.Tables[1].Rows.Count; int col = wordDoc.Content.Tables[1].Columns.Count; //for (int i = 1; i <= cnt; i++) //{ // for (int j = 1; j <=4; j++) // { // if (wordDoc.Content.Tables[1].Cell(i, j) != null) // { // string str = wordDoc.Content.Tables[1].Cell(i, j).Range.Text; // System.Diagnostics.Trace.TraceInformation(str); // } // } //} string str = ""; str += wordDoc.Content.Tables[1].Cell(1, 2).Range.Text; System.Diagnostics.Trace.TraceInformation("12"+str+"\n"); str += wordDoc.Content.Tables[1].Cell(1, 4).Range.Text; System.Diagnostics.Trace.TraceInformation("14" + str + "\n"); str += wordDoc.Content.Tables[1].Cell(2, 2).Range.Text; System.Diagnostics.Trace.TraceInformation("22" + str + "\n"); str += wordDoc.Content.Tables[1].Cell(2, 5).Range.Text; System.Diagnostics.Trace.TraceInformation("25" + str + "\n"); str += wordDoc.Content.Tables[1].Cell(2, 7).Range.Text; System.Diagnostics.Trace.TraceInformation("27" + str + "\n"); str += wordDoc.Content.Tables[1].Cell(2, 5).Range.Text; System.Diagnostics.Trace.TraceInformation("35" + str + "\n"); str += wordDoc.Content.Tables[1].Cell(3, 7).Range.Text; System.Diagnostics.Trace.TraceInformation("37" + str + "\n"); return str; //for (int i=0;i<cnt;i++) //{ // int col = wordDoc.Content.Tables[1].Rows[i].Cells.Count; // for (int j = 1; j <= col; j++) // { // string str = wordDoc.Content.Tables[1].Cell(i, j).Range.Text; // System.Diagnostics.Trace.TraceInformation(str); // } //} } }
ASP.NET使用WEB關閉進程process.Kill(); 的時候,出現拒絕訪問的提示解決方案
在web.config中加入
<identity impersonate="true" userName="Administrator" password="123456" />