[NET]在asp.net裡面實現對word操作

來源:互聯網
上載者:User

Public Class vbwordapp

    Private oWordApplic As word.ApplicationClass
    Private oDoc As word.Document
    Public Sub vbwordapp()
        '啟用com  word介面
        oWordApplic = New Word.ApplicationClass

    End Sub

    ' Open a file (the file must exists) and activate it
    Public Sub open(ByVal strFilename As String)
        Dim filename As String
        Dim onlyread As Boolean
        Dim isvisible As Boolean
        Dim missing

        filename = strFilename
        onlyread = False
        isvisible = True
        missing = System.Reflection.Missing.Value
        oDoc = oWordApplic.Documents.Open(filename, missing, onlyread, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
        oDoc.Activate()

    End Sub

    '開啟一個文檔
    Public Sub open()
        Dim missing
        missing = System.Reflection.Missing.Value
        oDoc = oWordApplic.Documents.Add(missing, missing, missing, missing)
        oDoc.Activate()

    End Sub

    Public Sub quit()
        Dim missing
        missing = System.Reflection.Missing.Value
        oWordApplic.Quit()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordApplic)
        oWordApplic = Nothing

    End Sub

    Public Sub releaseword(ByVal strfilename As String)
        Dim filename As String
        Dim onlyread As Boolean
        Dim isvisible As Boolean
        Dim missing

        filename = strfilename
        onlyread = False
        isvisible = True
        missing = System.Reflection.Missing.Value
        oWordApplic.Documents.Close()

    End Sub

    Public Sub save()
        oDoc.Save()
    End Sub

    Public Sub saveas(ByVal strfilename As String)
        Dim missing
        Dim filename As String

        missing = System.Reflection.Missing.Value
        filename = strfilename

        oDoc.SaveAs(filename, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)

    End Sub

    Public Sub saveashtml(ByVal strfilename As String)
        Dim missing
        missing = System.Reflection.Missing.Value

        Dim filename As String
        filename = strfilename

        Dim format
        format = CInt(Word.WdSaveFormat.wdFormatHTML)
        oDoc.SaveAs(filename, format, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)

    End Sub

    Public Sub inserttext(ByVal strtext)
        oWordApplic.Selection.TypeText(strtext)

    End Sub

    Public Sub insertlinebreak()
        oWordApplic.Selection.TypeParagraph()

    End Sub

    Public Sub insertlinebreak(ByVal nline As Integer)
        Dim i
        For i = 1 To nline
            oWordApplic.Selection.TypeParagraph()
        Next

    End Sub

    Public Sub inserttable(ByVal table As DataTable)
        Dim oTable As Word.Table
        Dim rowIndex, colIndex As Integer
        rowIndex = 1
        colIndex = 0

        oTable = oWordApplic.Selection.Tables.Add(oWordApplic.Selection.Range(), NumRows:=table.Rows.Count + 1, NumColumns:=table.Columns.Count)

        '將所得到的表的列名,賦值給儲存格
        Dim Col As DataColumn
        Dim Row As DataRow
        For Each Col In table.Columns
            colIndex = colIndex + 1
            oTable.Cell(1, colIndex).Range.InsertAfter(Col.ColumnName)
        Next

        '得到的表所有行,賦值給儲存格

        For Each Row In table.Rows
            rowIndex = rowIndex + 1
            colIndex = 0
            For Each Col In table.Columns
                colIndex = colIndex + 1
                oTable.Cell(rowIndex, colIndex).Range.InsertAfter(Row(Col.ColumnName))
            Next
        Next
        oTable.AllowAutoFit = True
        oTable.ApplyStyleFirstColumn = True
        oTable.ApplyStyleHeadingRows = True
    End Sub

    Public Sub setalignment(ByVal strtype As String)

        Select Case strtype

            Case "center"
                oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
            Case "left"
                oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
            Case "right"
                oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
            Case "justify"
                oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify

        End Select
    End Sub

    Public Sub setfont(ByVal strtype As String)
        Select Case strtype
            Case "bold"
                oWordApplic.Selection.Font.Bold = 1
            Case "italic"
                oWordApplic.Selection.Font.Italic = 1
            Case "underlined"
                oWordApplic.Selection.Font.Subscript = 0

        End Select
    End Sub

    ' disable all the style
    Public Sub SetFont()

        oWordApplic.Selection.Font.Bold = 0
        oWordApplic.Selection.Font.Italic = 0
        oWordApplic.Selection.Font.Subscript = 0

    End Sub

    Public Sub SetFontName(ByVal strType As String)

        oWordApplic.Selection.Font.Name = strType

    End Sub

    Public Sub SetFontSize(ByVal nSize As Integer)

        oWordApplic.Selection.Font.Size = nSize
    End Sub

 

    Public Sub insertpagebreak()
        Dim pBreak As Integer
        pBreak = CInt(Word.WdBreakType.wdPageBreak)
        oWordApplic.Selection.InsertBreak(pBreak)

    End Sub

    ' Go to a predefined bookmark, if the bookmark doesn't exists the application will raise an error

    Public Sub GotoBookMark(ByVal strBookMarkName As String)

        Dim missing
        missing = System.Reflection.Missing.Value
        Dim Bookmark
        Bookmark = CInt(Word.WdGoToItem.wdGoToBookmark)
        Dim namebookmark
        namebookmark = strBookMarkName
        oWordApplic.Selection.GoTo(Bookmark, missing, missing, namebookmark)
    End Sub

    Public Function BookmarkExist(ByVal strBookMarkName As String) As Boolean
        Dim exist As Boolean
        exist = oDoc.Bookmarks.Exists(strBookMarkName)
        Return exist
    End Function

    Public Sub GoToTheEnd()
        Dim missing, unit
        missing = System.Reflection.Missing.Value
        unit = Word.WdUnits.wdStory
        oWordApplic.Selection.EndKey(unit, missing)
    End Sub

    Public Sub GoToTheBeginning()
        Dim missing, unit
        missing = System.Reflection.Missing.Value
        unit = Word.WdUnits.wdStory
        oWordApplic.Selection.HomeKey(unit, missing)
    End Sub

    Public Sub GoToTheTable(ByVal ntable As Integer)

        Dim missing, what, which, count
        missing = System.Reflection.Missing.Value

        what = Word.WdUnits.wdTable

        which = Word.WdGoToDirection.wdGoToAbsolute

        count = 1

        oWordApplic.Selection.GoTo(what, which, 1, missing)
        oWordApplic.Selection.Find.ClearFormatting()

        oWordApplic.Selection.Text = ""

    End Sub

    Public Sub GoToRightCell()

        Dim missing, direction
        missing = System.Reflection.Missing.Value

        direction = Word.WdUnits.wdCell
        oWordApplic.Selection.MoveRight(direction, missing, missing)
    End Sub

    Public Sub GoToLeftCell()

        Dim missing, direction
        missing = System.Reflection.Missing.Value

        direction = Word.WdUnits.wdCell
        oWordApplic.Selection.MoveLeft(direction, missing, missing)
    End Sub

    Public Sub GoToDownCell()
        Dim missing, direction
        missing = System.Reflection.Missing.Value

        direction = Word.WdUnits.wdLine
        oWordApplic.Selection.MoveDown(direction, missing, missing)
    End Sub

    Public Sub GoToUpCell()
        Dim missing, direction
        missing = System.Reflection.Missing.Value

        direction = Word.WdUnits.wdLine
        oWordApplic.Selection.MoveUp(direction, missing, missing)
    End Sub

    ' this function doesn't work
    Public Sub InsertPageNumber(ByVal strType As String, ByVal bHeader As Boolean)
        Dim missing, alignment, bfirstpage, bf
        missing = System.Reflection.Missing.Value

        bfirstpage = False
        bf = True
        Select Case strType

            Case "Center"
                alignment = Word.WdPageNumberAlignment.wdAlignPageNumberCenter
                oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberCenter

            Case "Right"
                alignment = Word.WdPageNumberAlignment.wdAlignPageNumberRight
                oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberRight

            Case "Left"
                alignment = Word.WdPageNumberAlignment.wdAlignPageNumberLeft
                oWordApplic.Selection.HeaderFooter.PageNumbers.Add(alignment, bfirstpage)

        End Select
    End Sub
    Public Sub insertpic(ByVal filename As String)
        Dim missing
        missing = System.Reflection.Missing.Value
        oWordApplic.Selection.InlineShapes.AddPicture(filename, False, True, missing)
    End Sub

End Class

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.