zoukankan      html  css  js  c++  java
  • [转贴]一个操作Word的例子

    Dim ThisApplication As New Word.Application()
            Dim ThisDocument As Word.Document

            ThisDocument = ThisApplication.Documents.Open("c:\new.doc")

            ThisDocument.Range.Delete()
            ThisDocument.Sections.PageSetup.Orientation = WdOrientation.wdOrientLandscape

            Dim tabStops() As Single = {4, 6}

            '==========================================
            'Create header

            ThisApplication.ScreenUpdating = False
            Dim rng As Word.Range
            rng = ThisDocument.Range(0, 0)
            rng.InsertBefore("SQL Server data table name exported.")
            rng.Font.Name = "Verdana"
            rng.Font.Size = 16
            rng.InsertParagraphAfter()
            rng.InsertParagraphAfter()

            rng.SetRange(start:=rng.End, End:=rng.End)

            Dim fmt As Word.ParagraphFormat = rng.ParagraphFormat
            fmt.TabStops.ClearAll()
            fmt.TabStops.Add( _
                ThisApplication.InchesToPoints(tabStops(0)), _
                Word.WdTabAlignment.wdAlignTabLeft, _
                Word.WdTabLeader.wdTabLeaderSpaces)
            fmt.TabStops.Add( _
                ThisApplication.InchesToPoints(tabStops(1)), _
                Word.WdTabAlignment.wdAlignTabLeft, _
                Word.WdTabLeader.wdTabLeaderSpaces)

            rng.Text = _
                "Author ID" & ControlChars.Tab & _
                "Last Name" & ControlChars.Tab & _
                "First Name" & ControlChars.Tab '& _
            '"Phone" & ControlChars.Tab & _
            '"Address" & ControlChars.Tab & _
            '"City" & "            " & _
            '"State" & "            " & _
            '"Zip" & "            " & _
            '"Contract" & "  "
            rng.Font.Name = "Verdana"
            rng.Font.Name = 10
            rng.Font.Bold = CLng(True)
            rng.Font.Underline = WdUnderline.wdUnderlineSingle

            rng.InsertParagraphAfter()
            rng.SetRange(Start:=rng.End, End:=rng.End)
            fmt = rng.ParagraphFormat


            ' Set up the tabs for the columns.
            fmt.TabStops.ClearAll()
            fmt.TabStops.Add( _
                ThisApplication.InchesToPoints(tabStops(0)), _
                Word.WdTabAlignment.wdAlignTabLeft, _
                Word.WdTabLeader.wdTabLeaderDots)
            fmt.TabStops.Add( _
                ThisApplication.InchesToPoints(tabStops(1)), _
                Word.WdTabAlignment.wdAlignTabLeft, _
                Word.WdTabLeader.wdTabLeaderDots)

            ' Insert a bookmark to use for the inserted data.
            'ThisDocument.Bookmarks.Add( _
            '    Name:="data", Range:=DirectCast(rng, System.Object))
            rng.InsertParagraphAfter()

            Dim dr As SqlDataReader
            Dim cmd As SqlCommand
            Dim sw As New System.IO.StringWriter()
            cmd = New SqlCommand("select au_id,au_lName,au_fname,phone," & _
                                "address,city,state,zip,contract from authors", cnn)
            cmd.Connection.Open()
            dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)

            ' Loop through the data, creating tab-delimited output:
            While dr.Read()
                sw.WriteLine("{0}{1}{2}{3}{4}", _
                    dr(0), ControlChars.Tab, _
                    dr(1), ControlChars.Tab, _
                    dr(2), ControlChars.Tab)
            End While

            ' Work with the previously created bookmark:
            'rng = ThisDocument.Bookmarks("data").Range
            rng.Text = sw.ToString()
            rng.Font.Name = "Verdana"
            rng.Font.Size = 10

            If Not dr Is Nothing Then
                dr.Close()
            End If
            cmd.Connection.Close()

            ThisApplication.ScreenUpdating = True

            ThisApplication.DisplayAlerts = False
            ThisDocument.Save()

            ThisApplication.Visible = True
            'ThisApplication.Documents.Close()
            'ThisApplication.Quit(Word.WdSaveOptions.wdSaveChanges)

            Dim fileWord As String
            Dim currentdate As Date
            fileWord = "output" & currentdate.ToLongDateString & ".doc"

            Page.Response.ContentType = "Application/unknown"
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileWord, System.Text.Encoding.UTF8))
            Response.WriteFile("c:\\new.doc")
            Response.End()


  • 相关阅读:
    [APM] OneAPM 云监控部署与试用体验
    Elastic Stack 安装
    xBIM 综合使用案例与 ASP.NET MVC 集成(一)
    JQuery DataTables Selected Row
    力导向图Demo
    WPF ViewModelLocator
    Syncfusion SfDataGrid 导出Excel
    HTML Table to Json
    .net core 2.0 虚拟目录下载 Android Apk 等文件
    在BootStrap的modal中使用Select2
  • 原文地址:https://www.cnblogs.com/goody9807/p/223358.html
Copyright © 2011-2022 走看看