在醫(yī)療管理系統(tǒng)中為保存患者的體檢和治療記錄,方便以后的醫(yī)生或其他人查看。當(dāng)把數(shù)據(jù)保存到數(shù)據(jù)庫(kù)中,需要新建很多的字段,而且操作很繁瑣,于是想到網(wǎng)頁(yè)的信息創(chuàng)建到一個(gè)word文本中,在顯示的時(shí),可以在線打開word,也可以把word轉(zhuǎn)換成html標(biāo)簽顯示。 這樣使用word代替網(wǎng)頁(yè)的原因有:
實(shí)例使用 創(chuàng)建Word 文檔所使用的主要方法是通過微軟公司提供的Microsoft Word X Object Library, 新建一個(gè)winForm項(xiàng)目文件, 下面從word創(chuàng)建、格式設(shè)置、文本添加、圖片添加、表格添加展示部分代碼:
創(chuàng)建Word文檔
設(shè)置Word文檔格式
效果圖:
添加文本
效果圖:
添加圖片
效果圖: private void AddWordTable() { object path;//文件路徑 string strContent;//文件內(nèi)容 MSWord.Application wordApp;//Word應(yīng)用程序變量 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; //定義一個(gè)word中的表格對(duì)象 MSWord.Table table = wordDoc.Tables.Add(wordApp.Selection.Range, tableRow, tableColumn, ref Nothing, ref Nothing); wordDoc.Tables[1].Cell(1, 1).Range.Text = "列\(zhòng)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;//圖片高度 // 將圖片設(shè)置為四周環(huán)繞型 MSWord.Shape s = wordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape(); s.WrapFormat.Type = MSWord.WdWrapType.wdWrapSquare; //設(shè)置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; //設(shè)置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; } //添加表頭斜線,并設(shè)置表頭的樣式 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; //表格邊框 //表格內(nèi)容行邊框 SetTableBorderStyle(table, Microsoft.Office.Interop.Word.WdBorderType.wdBorderHorizontal, Microsoft.Office.Interop.Word.WdColor.wdColorGray20, Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth025pt); //表格內(nèi)容列邊框 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文檔創(chuàng)建表格完畢!');</script>"); } 附:SetTableBorderStyle函數(shù)內(nèi)容
View Code
效果圖: 書簽使用: 使用步驟:1:建立word模板,并且在word中插入要用到的書簽 2:c#方法中新建word操作類,并且打開硬盤中建立好的word模板 3:找到word模板中的書簽,并在書簽處寫入要插入的數(shù)據(jù)
word書簽使用
附: c# 將word文檔顯示在網(wǎng)頁(yè)上的方式:
Word轉(zhuǎn)換Html
轉(zhuǎn)換思路: >取得Word文檔的本地路徑 >將Word文檔轉(zhuǎn)換為html文件 >將html保存到項(xiàng)目中 >在當(dāng)前項(xiàng)目中打開此html文件 局限: 目前只在IE10測(cè)試中可以很好使用,在firefox和chrome測(cè)試用均有中文亂碼的問題,有待解決。
引用:
|
|