VB.NET 创建WORD文档
?? ?1、添加引用->COM->Microsoft Word 11.0 Object Library
?? ?2、Imports Word?
? ''' <summary>''' 創建word文檔''' </summary>''' <remarks></remarks>Public Sub CreateDoc()Dim docApp As New Word.ApplicationDim doc As Word.DocumentDim miss As Object = System.Reflection.Missing.Valuedoc = docApp.Documents.Add(miss, miss, miss, miss)Dim strContext As String = ""'文件內容strContext = "你好,我簡單的幸福."'行距docApp.Selection.ParagraphFormat.LineSpacing = 15'頁眉With docApp.ActiveWindow.View.Type = Word.WdViewType.wdOutlineView.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader.ActiveWindow.ActivePane.Selection.InsertAfter("[頁眉內容]").Selection.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphRight.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument '跳出頁眉設置End With'頁尾With docApp.ActiveWindow.View.Type = Word.WdViewType.wdOutlineView.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryFooter.ActiveWindow.ActivePane.Selection.InsertAfter("[頁尾內容]").Selection.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphRight.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocumentEnd With'表格Dim table As Word.Table = doc.Tables.Add(docApp.Selection.Range, 10, 3)'表格樣式With table.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle.Columns(1).Width = 100.Columns(2).Width = 220.Columns(3).Width = 105End With'填充表格內容With table.Cell(1, 1).Range.Text = "產品詳細信息表".Cell(1, 1).Range.Bold = 2 '粗體.Cell(1, 1).Range.Font.Color = WdColor.wdColorBrown'合并單元格.Cell(1, 1).Merge(.Cell(2, 3)) '橫向合并End WithWith docApp.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter '垂直居中.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter '水平居中End WithWith table.Cell(2, 1).Range.Text = "品牌名稱:".Cell(3, 1).Range.Text = "史丹奴"End With'縱向合并單元格table.Cell(3, 3).Select() '選中一行Dim moveUnit As Object = Word.WdUnits.wdLineDim moveCount As Object = 5Dim moveExtend As Object = WdMovementType.wdExtendWith docApp.Selection.MoveDown(moveUnit, moveCount, moveExtend).Selection.Cells.Merge()End With'插入圖片Dim strPicPath As String = "http://www.google.com.hk/intl/zh-CN/images/logo_cn.png"Dim linkToFile As Object = FalseDim saveWithDoc As Object = TrueDim anchor As Object = doc.Application.Selection.RangeWith doc.Application.ActiveDocument.InlineShapes.AddPicture(strPicPath, linkToFile, saveWithDoc, anchor).InlineShapes(1).Width = 100 '圖片寬度 .InlineShapes(1).Height = 100 '圖片高度End With'設置圖片為四周環繞型Dim s As Word.Shapes = doc.Application.ActiveDocument.InlineShapes(1).ConvertToShape()s.WrapFormat.Type = WdWrapType.wdWrapSquare'添加新行doc.Content.Tables(1).Rows.Add()doc.Paragraphs.Last.Range.Text = strContextdoc.Paragraphs.Last.Range.Text = "文檔創建時間:" & Format(Now, "yyyy-MM-dd HH:mm:ss") '“落款”doc.Paragraphs.Last.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRightDim f As Object = doc.SaveFormat'保存文件doc.SaveAs("c:/新年第二天.doc")doc.Close()docApp.Quit()End Sub?
3 效果預覽
總結
以上是生活随笔為你收集整理的VB.NET 创建WORD文档的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VC文件扩展名解读大全
- 下一篇: 软件天才与技术民工