操作Word的例子

来源:互联网 发布:最好的比价软件 编辑:程序博客网 时间:2024/05/17 01:59

  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()