• [转贴]一个操作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()


  • 相关阅读:
    首页调单个产品分类的推荐产品,最新产品和热卖商品
    ecshop模板<! TemplateBeginEditable name="左上角主区域" >用法
    复制DataTable数据到新DataTable
    定时任务时间与当前时间比较的方法
    批量删除文件夹下包含指定字段的文件
    SQL 字符串去除空格函数
    Javascript的IE和Firefox(火狐)兼容性的常用例子
    查询表某列的加权平均值
    Jquery实现页面定时跳转
    Date.parse Firefox返回Nan的解决办法
  • 原文地址:https://www.cnblogs.com/goody9807/p/223358.html
Copyright © 2020-2023  润新知