c# 操作Word總結


c# 操作Word總結

  在醫療管理系統中為保存患者的體檢和治療記錄,方便以后的醫生或其他人查看。當把數據保存到數據庫中,需要新建很多的字段,而且操作很繁瑣,於是想到網頁的信息創建到一個word文本中,在顯示的時,可以在線打開word,也可以把word轉換成html標簽顯示。 這樣使用word代替網頁的原因有:   第一:網頁生成數學公式和特殊符號存儲和顯示比較麻煩(如何操作word生成數學公式,有待測試)   第二:生成Word版的報告更容易存檔和沒有環境下的傳閱及打印   第三:客戶直接操作Word感覺更親切,而且非常熟悉   Msdn上的word操作api(不過只有英文版,英文差的先閃過) Word2007的API:http://msdn.microsoft.com/en-us/library/bb257531(v=office.12).aspx Word2010的API:http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word(v=office.14).aspx
  Word對象模型  (.Net Perspective)   五大對象 Application           :代表Microsoft Word應用程序本身   是Document和Selection的基類。通過Application的屬性和方法,我們可以控制Word的大環境。 Document            :代表一個Word文檔   當你新建一個Word文檔或者打開一個已有的Word文檔,你將創建一個Document對象,該對象被加入到Words Documents Collection中。擁有焦點的Document稱為ActiveDocument,可以通過Application對象的ActiveDocument屬性獲得當前文檔對象 Selection              :代表當前選中的區域(高亮),沒有選中區域時代表光標點   它通常是高亮顯示的(例如,你要改變一段文字的字體,你首先得選中這段文字,那么選中的這塊區域就是當前文檔的Selection對象所包含的區域) Bookmarks           :書簽     1>書簽一般有名字     2>Saved with the document,且文檔關閉了之后書簽繼續存在     3>書簽通常是隱藏的,但也可以通過代碼設置其為可見
Range                  :代表一塊區域,與Selection類似,不過一般不可見     1>包含一個起始位置和一個結束位置     2>它可以包含光標點,一段文本或者整個文檔     3>它包含空格,tab以及paragraph marks   4>它可以是當前選中的區域,當然也可以不是當前選中區域     5>它被動態創建     6>當你在一個Range的末尾插入文本,這將擴展該Range

  word文檔對象的結構圖
關於對象的詳細使用,可以參考msdn api

實例使用  

創建Word 文檔所使用的主要方法是通過微軟公司提供的Microsoft Word X Object Library, 其中X 為版本號。Word2010對應14.0, Word 2007 對應12.0,Word 2003 對應11.0。 通過在項目中添加該組件,即可使用微軟公司提供的方法創建相應版本的Word 文檔。 在實例中我將所要生成word的格式設置為2003版本

新建一個winForm項目文件, Com組件中添加 Microsoft Word 12.0 Object Library,引用面板中多出Microsoft.Office.Core、Microsoft.Office.Interop.Word兩個引用。 在類文件中添加應用如下: using MSWord = Microsoft.Office.Interop.Word; using System.IO; using System.Reflection; using Microsoft.Office.Interop.Word;

  下面從word創建、格式設置、文本添加、圖片添加、表格添加展示部分代碼:

void CreateWord() { object path;//文件路徑 string strContent;//文件內容 MSWord.Application wordApp;//Word應用程序變量 MSWord.Document wordDoc;//Word文檔變量 path = "d:\\myWord.doc";//保存為Word2003文檔 // path = "d:\\myWord.doc";//保存為Word2007文檔 wordApp = new MSWord.ApplicationClass();//初始化 if (File.Exists((string)path)) { File.Delete((string)path); } //由於使用的是COM 庫,因此有許多變量需要用Missing.Value 代替 Object Nothing = Missing.Value; //新建一個word對象 wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); //WdSaveDocument為Word2003文檔的保存格式(文檔后綴.doc)\wdFormatDocumentDefault為Word2007的保存格式(文檔后綴.docx) object format = MSWord.WdSaveFormat.wdFormatDocument; //將wordDoc 文檔對象的內容保存為DOC 文檔,並保存到path指定的路徑 wordDoc.SaveAs(ref path, ref format, 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文檔 wordDoc.Close(ref Nothing, ref Nothing, ref Nothing); //關閉wordApp組件對象 wordApp.Quit(ref Nothing, ref Nothing, ref Nothing); Response.Write("<script>alert('" + path + ": Word文檔創建完畢!');</script>"); }
創建Word文檔

 

private void SetWordStyle() { object path;//文件路徑 string strContent;//文件內容 MSWord.Application wordApp;//Word應用程序變量 MSWord.Document wordDoc;//Word文檔變量 path = "d:\\myWord.doc";//保存為Word2003文檔 // path = "d:\\myWord.doc";//保存為Word2007文檔 wordApp = new MSWord.ApplicationClass();//初始化 if (File.Exists((string)path)) { File.Delete((string)path); } Object Nothing = Missing.Value; wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); //頁面設置 wordDoc.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4;//設置紙張樣式 wordDoc.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientPortrait;//排列方式為垂直方向 wordDoc.PageSetup.TopMargin = 57.0f; wordDoc.PageSetup.BottomMargin = 57.0f; wordDoc.PageSetup.LeftMargin = 57.0f; wordDoc.PageSetup.RightMargin = 57.0f; wordDoc.PageSetup.HeaderDistance = 30.0f;//頁眉位置 //設置頁眉 wordApp.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;//視圖樣式 wordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;//進入頁眉設置,其中頁眉邊距在頁面設置中已完成 wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight; //插入頁眉圖片(測試結果圖片未插入成功) wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter; string headerfile = "d:\\header.jpg"; Microsoft.Office.Interop.Word.InlineShape shape1 = wordApp.ActiveWindow.ActivePane.Selection.InlineShapes.AddPicture(headerfile, ref Nothing, ref Nothing, ref Nothing); shape1.Height = 20; shape1.Width = 80; wordApp.ActiveWindow.ActivePane.Selection.InsertAfter(" 文檔頁眉"); //去掉頁眉的橫線 wordApp.ActiveWindow.ActivePane.Selection.ParagraphFormat.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].LineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleNone; wordApp.ActiveWindow.ActivePane.Selection.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].Visible = false; wordApp.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;//退出頁眉設置 //為當前頁添加頁碼 Microsoft.Office.Interop.Word.PageNumbers pns = wordApp.Selection.Sections[1].Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers;//獲取當前頁的號碼 pns.NumberStyle = Microsoft.Office.Interop.Word.WdPageNumberStyle.wdPageNumberStyleNumberInDash; pns.HeadingLevelForChapter = 0; pns.IncludeChapterNumber = false; pns.RestartNumberingAtSection = false; pns.StartingNumber = 0; object pagenmbetal = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter;//將號碼設置在中間 object first = true; wordApp.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers.Add(ref pagenmbetal, ref first); object format = MSWord.WdSaveFormat.wdFormatDocument; wordDoc.SaveAs(ref path, ref format, 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); Response.Write("<script>alert('" + path + ": Word文檔格式設置完畢!');</script>"); }
設置Word文檔格式

效果圖:

private void AddWordText() { object path;//文件路徑 string strContent;//文件內容 MSWord.Application wordApp;//Word應用程序變量 MSWord.Document wordDoc;//Word文檔變量 path = "d:\\myWord.doc";//保存為Word2003文檔 // path = "d:\\myWord.doc";//保存為Word2007文檔 wordApp = new MSWord.ApplicationClass();//初始化 if (File.Exists((string)path)) { File.Delete((string)path); } Object Nothing = Missing.Value; wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); wordApp.Selection.ParagraphFormat.LineSpacing = 35f;//設置文檔的行間距 //寫入普通文本 wordApp.Selection.ParagraphFormat.FirstLineIndent = 30;//首行縮進的長度 strContent = "c#向Word寫入文本 普通文本:\n"; wordDoc.Paragraphs.Last.Range.Text = strContent; //將文檔的前三個字替換成"asdfasdf",並將其顏色設為藍色 object start = 0; object end = 3; Microsoft.Office.Interop.Word.Range rang = wordDoc.Range(ref start, ref end); rang.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBrightGreen; rang.Text = "我是替換文字"; wordDoc.Range(ref start, ref end); //寫入黑體文本 object unite = Microsoft.Office.Interop.Word.WdUnits.wdStory; wordApp.Selection.EndKey(ref unite, ref Nothing); wordApp.Selection.ParagraphFormat.FirstLineIndent = 0;//取消首行縮進的長度 strContent = "黑體文本\n ";//在文本中使用'\n'換行 wordDoc.Paragraphs.Last.Range.Font.Name = "黑體"; wordDoc.Paragraphs.Last.Range.Text = strContent; // wordApp.Selection.Text = strContent; //寫入加粗文本 strContent = "加粗文本\n "; wordApp.Selection.EndKey(ref unite, ref Nothing); wordDoc.Paragraphs.Last.Range.Font.Bold = 1;//Bold=0為不加粗 wordDoc.Paragraphs.Last.Range.Text = strContent; // wordApp.Selection.Text = strContent; //寫入15號字體文本 strContent = "15號字體文本\n "; wordApp.Selection.EndKey(ref unite, ref Nothing); wordDoc.Paragraphs.Last.Range.Font.Size = 15; wordDoc.Paragraphs.Last.Range.Text = strContent; //寫入斜體文本 strContent = "斜體文本\n "; wordApp.Selection.EndKey(ref unite, ref Nothing); wordDoc.Paragraphs.Last.Range.Font.Italic = 1; wordDoc.Paragraphs.Last.Range.Text = strContent; //寫入藍色文本 strContent = "藍色文本\n "; wordApp.Selection.EndKey(ref unite, ref Nothing); wordDoc.Paragraphs.Last.Range.Font.Color = MSWord.WdColor.wdColorBlue; wordDoc.Paragraphs.Last.Range.Text = strContent; //寫入下划線文本 strContent = "下划線文本\n "; wordApp.Selection.EndKey(ref unite, ref Nothing); wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineThick; wordDoc.Paragraphs.Last.Range.Text = strContent; object format = MSWord.WdSaveFormat.wdFormatDocument; wordDoc.SaveAs(ref path, ref format, 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); Response.Write("<script> alert('" + path + ": Word文檔寫入文本完畢!');</script>"); }
添加文本

效果圖:

    private void AddWordPic() { object path;//文件路徑 string strContent;//文件內容 MSWord.Application wordApp;//Word應用程序變量 MSWord.Document wordDoc;//Word文檔變量 path = "d:\\myWord.doc";//保存為Word2003文檔 // path = "d:\\myWord.doc";//保存為Word2007文檔 wordApp = new MSWord.ApplicationClass();//初始化 if (File.Exists((string)path)) { File.Delete((string)path); } Object Nothing = Missing.Value; wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); string filename = "d:\\kk.jpg"; //定義要向文檔中插入圖片的位置 object range = wordDoc.Paragraphs.Last.Range; //定義該圖片是否為外部鏈接 object linkToFile = false;//默認 //定義插入的圖片是否隨word一起保存 object saveWithDocument = true; //向word中寫入圖片 wordDoc.InlineShapes.AddPicture(filename, ref Nothing, ref Nothing, ref Nothing); object unite = Microsoft.Office.Interop.Word.WdUnits.wdStory; wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;//居中顯示圖片 wordDoc.InlineShapes[1].Height = 130; wordDoc.InlineShapes[1].Width = 200; wordDoc.Content.InsertAfter("\n"); wordApp.Selection.EndKey(ref unite, ref Nothing); wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; wordApp.Selection.Font.Size = 10;//字體大小 wordApp.Selection.TypeText("圖1 測試圖片\n"); object format = MSWord.WdSaveFormat.wdFormatDocument; wordDoc.SaveAs(ref path, ref format, 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); Response.Write("<script>alert('" + path + ": Word文檔創建圖片完畢!');</script>"); }
添加圖片

效果圖:

private void AddWordTable() { object path;//文件路徑 string strContent;//文件內容 MSWord.Application wordApp;//Word應用程序變量 MSWord.Document wordDoc;//Word文檔變量 path = "d:\\myWord.doc";//保存為Word2003文檔 // path = "d:\\myWord.doc";//保存為Word2007文檔 wordApp = new MSWord.ApplicationClass();//初始化 if (File.Exists((string)path)) { File.Delete((string)path); } Object Nothing = Missing.Value; wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); int tableRow = 6; int tableColumn = 6; //定義一個word中的表格對象 MSWord.Table table = wordDoc.Tables.Add(wordApp.Selection.Range, tableRow, tableColumn, ref Nothing, ref Nothing); wordDoc.Tables[1].Cell(1, 1).Range.Text = "列\n行"; for (int i = 1; i < tableRow; i++) { for (int j = 1; j < tableColumn; j++) { if (i == 1) { table.Cell(i, j+1).Range.Text = "Column " + j; } if (j == 1) { table.Cell(i+1, j).Range.Text = "Row " + i; } table.Cell(i+1, j+1).Range.Text = i + "" + j + ""; } } //添加行 table.Rows.Add(ref Nothing); table.Rows[tableRow + 1].Height = 45; //向新添加的行的單元格中添加圖片 string FileName = "d:\\kk.jpg";//圖片所在路徑 object LinkToFile = false; object SaveWithDocument = true; object Anchor = table.Cell(tableRow+1, tableColumn).Range;//選中要添加圖片的單元格 wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor); wordDoc.Application.ActiveDocument.InlineShapes[1].Width = 75;//圖片寬度 wordDoc.Application.ActiveDocument.InlineShapes[1].Height = 45;//圖片高度 // 將圖片設置為四周環繞型 MSWord.Shape s = wordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape(); s.WrapFormat.Type = MSWord.WdWrapType.wdWrapSquare; //設置table樣式 table.Rows.HeightRule = MSWord.WdRowHeightRule.wdRowHeightAtLeast; table.Rows.Height = wordApp.CentimetersToPoints(float.Parse("0.8")); table.Range.Font.Size = 10.5F; table.Range.Font.Bold = 0; table.Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter; table.Range.Cells.VerticalAlignment = MSWord.WdCellVerticalAlignment.wdCellAlignVerticalBottom; //設置table邊框樣式 table.Borders.OutsideLineStyle = MSWord.WdLineStyle.wdLineStyleDouble; table.Borders.InsideLineStyle = MSWord.WdLineStyle.wdLineStyleSingle; table.Rows[1].Range.Font.Bold = 1; table.Rows[1].Range.Font.Size = 12F; table.Cell(1, 1).Range.Font.Size = 10.5F; wordApp.Selection.Cells.Height = 40;//所有單元格的高度 for (int i = 2; i <= tableRow; i++) { table.Rows[i].Height = 20; } table.Cell(1, 1).Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight; table.Cell(1, 1).Range.Paragraphs[2].Format.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft; table.Columns[1].Width = 50; for (int i = 2; i <=tableColumn; i++) { table.Columns[i].Width = 75; } //添加表頭斜線,並設置表頭的樣式 table.Cell(1, 1).Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderDiagonalDown].Visible = true; table.Cell(1, 1).Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderDiagonalDown].Color = Microsoft.Office.Interop.Word.WdColor.wdColorGray60; table.Cell(1, 1).Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderDiagonalDown].LineWidth = Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth050pt; //表格邊框 //表格內容行邊框  SetTableBorderStyle(table, Microsoft.Office.Interop.Word.WdBorderType.wdBorderHorizontal, Microsoft.Office.Interop.Word.WdColor.wdColorGray20, Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth025pt); //表格內容列邊框  SetTableBorderStyle(table, Microsoft.Office.Interop.Word.WdBorderType.wdBorderVertical, Microsoft.Office.Interop.Word.WdColor.wdColorGray20, Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth025pt); SetTableBorderStyle(table, Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft, Microsoft.Office.Interop.Word.WdColor.wdColorGray50, Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth050pt); SetTableBorderStyle(table, Microsoft.Office.Interop.Word.WdBorderType.wdBorderRight, Microsoft.Office.Interop.Word.WdColor.wdColorGray50, Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth050pt); SetTableBorderStyle(table, Microsoft.Office.Interop.Word.WdBorderType.wdBorderTop, Microsoft.Office.Interop.Word.WdColor.wdColorGray50, Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth050pt); SetTableBorderStyle(table, Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom, Microsoft.Office.Interop.Word.WdColor.wdColorGray50, Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth050pt); //合並單元格 table.Cell(4, 4).Merge(table.Cell(4, 5));//橫向合並  table.Cell(2, 3).Merge(table.Cell(4, 3));//縱向合並 object format = MSWord.WdSaveFormat.wdFormatDocument; wordDoc.SaveAs(ref path, ref format, 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); Response.Write("<script>alert('" + path + ": Word文檔創建表格完畢!');</script>"); }
添加表格

附:SetTableBorderStyle函數內容

table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].Visible = true;              table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].Color =  Word.WdColor.wdColorGreen;               table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].LineWidth =  Word.WdLineWidth.wdLineWidth050pt
View Code

 

效果圖:

書簽使用:

使用步驟:1:建立word模板,並且在word中插入要用到的書簽

     2:c#方法中新建word操作類,並且打開硬盤中建立好的word模板

     3:找到word模板中的書簽,並在書簽處寫入要插入的數據

 

    public void AddDocModel() { killWinWordProcess(); wordApp = new ApplicationClass(); wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone; wordApp.Visible = false; object missing = System.Reflection.Missing.Value; object templateName = Application.StartupPath + @"\Report";//最終的word文檔需要寫入的位置 object ModelName = Application.StartupPath + @"\Report\ReportModel_Stand.doc";//word模板的位置 object count = 1; object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//換一行; wordDoc = wordApp.Documents.Open(ref ModelName, 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);//打開word模板 //在書簽處插入文字 object oStart = "PatName";//word中的書簽名 Range range = wordDoc.Bookmarks.get_Item(ref oStart).Range;//表格插入位置 range.Text = "這里是您要輸入的內容";//在書簽處插入文字內容 //在書簽處插入表格 oStart = "PatInfo";//word中的書簽名 range = wordDoc.Bookmarks.get_Item(ref oStart).Range;//表格插入位置 MSWord.Table tab_Pat = wordDoc.Tables.Add(range, 2, 4, ref missing, ref missing);//開辟一個2行4列的表格 tab_Pat.Range.Font.Size = 10.5F; tab_Pat.Range.Font.Bold = 0; tab_Pat.Columns[1].Width = 50; tab_Pat.Columns[2].Width = 65; tab_Pat.Columns[3].Width = 40; tab_Pat.Columns[4].Width = 40; tab_Pat.Cell(1, 1).Range.Text = "病歷號"; tab_Pat.Cell(1, 2).Range.Text = "PatientNO"; tab_Pat.Cell(1, 3).Range.Text = "身高"; tab_Pat.Cell(1, 4).Range.Text = "Height"; tab_Pat.Cell(2, 1).Range.Text = "姓名"; tab_Pat.Cell(2, 2).Range.Text ="PatientName"; tab_Pat.Cell(2, 3).Range.Text = "體重"; tab_Pat.Cell(2, 4).Range.Text = "Weight"; //保存word object format = WdSaveFormat.wdFormatDocument;//保存格式 wordDoc.SaveAs(ref templateName, ref format, 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); //關閉wordDoc,wordApp對象 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); } // 殺掉winword.exe進程 public void killWinWordProcess() { 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(); } } }
word書簽使用

 

 

附: c# 將word文檔顯示在網頁上的方式:

 public void WordToHtml(string wordFileName) { //在此處放置用戶代碼以初始化頁面 Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass(); Type wordType = word.GetType(); Documents docs = word.Documents; //打開文件 Type docsType = docs.GetType(); Document doc = (Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true }); //轉換格式,另存為 Type docType = doc.GetType(); string wordSaveFileName = wordFileName.ToString(); //配置保存htm文件的地址 string strPath = Server.MapPath("~/Model/Word/Files/"); string strSaveFileName = strPath + "a.html"; //wordSaveFileName.Substring(0, wordSaveFileName.Length - 3) + "html"; object saveFileName = (object)strSaveFileName; docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, WdSaveFormat.wdFormatFilteredHTML }); docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null); //退出 Word wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null); Response.Write("<script language='javascript'>window.open ('Files/a.html', 'newwindow', 'height=600, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no')</script>"); }
Word轉換Html

轉換思路:

  >取得Word文檔的本地路徑

  >將Word文檔轉換為html文件

  >將html保存到項目中

  >在當前項目中打開此html文件

局限:

  目前只在IE10測試中可以很好使用,在firefox和chrome測試用均有中文亂碼的問題,有待解決。

 

 引用:

http://blog.csdn.net/ruby97/article/details/7406806

http://wenku.baidu.com/link?url=osPLfzoQc1Tl0mi7MAT5srs7KUmPZ3WLf3Pjs_I9Ahu87UNbJceGsogT5ONBsI87DnndqJX7HI6-mRcWehoGcF2P-gLKkvCtiH5KA3UI13S

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM