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


  • 相关阅读:
    openssl rsa 加密
    SVN
    day04-drf认证、限流、权限、过滤、排序、分页、异常处理、自动接口文档生成、Xadmin后台管理
    day03-drf视图相关
    day02-序列化与反序列化
    day01-drf引入、序列化反序列化前序
    restFul接口设计规范
    Mysql优化之innodb_buffer_pool_size篇
    Mysql(CAST)和Oracle(to_char)应用
    Mongo和Mysql查看查询任务并终止
  • 原文地址:https://www.cnblogs.com/goody9807/p/223358.html
Copyright © 2011-2022 走看看