Export reports to Excel and Word in. NET Environment

Source: Internet
Author: User

In VB. NET, you can export reports to Excel and Word for output and generate professional reports. The procedure is as follows: (Note: first, add reference, select COM --> Microsoft Word Object Library and Microsoft Excel Object Library)
Private Function CreaTable () As DataTable
Dim dt As New DataTable ()
Dt. Columns. Add ("column 1", GetType (String ))
Dt. Columns. Add ("column 2", GetType (Integer ))
Dt. Columns. Add ("column 3", GetType (String ))
Dt. Columns. Add ("column 4", GetType (String ))
Dim row, row1 As DataRow
Row = dt. NewRow ()
Row! Column 1 = "Row 1"
Row! Column 2 = 1
Row! Column 3 = "d"
Row! Column 4 = ""
Dt. Rows. Add (row)
Row1 = dt. NewRow ()
Row1! Column 1 = "Row 2"
Row1! Column 2 = 12
Row1! Column 3 = "B"
Row1! Column 4 = "c"
Dt. Rows. Add (row1)
Return dt
End Function
'2. Export the table content to Excel
Dim xlApp As New Excel. Application ()
Dim xlBook As Excel. Workbook
Dim xlSheet As Excel. Worksheet
Dim rowIndex As Integer = 1
Dim colIndex As Integer = 0
XlBook = xlApp. Workbooks (). Add
XlSheet = xlBook. Worksheets ("sheet1 ")
Dim Table As New DataTable ()
Table = CreaTable ()
'Assign the column name of the obtained table to the cell.
Dim Col As DataColumn
Dim Row As DataRow
For Each Col In Table. Columns
ColIndex = colIndex + 1
XlApp. Cells (1, colIndex) = Col. ColumnName
Next
'All rows in the table are assigned to the cell.
For Each Row In Table. Rows
RowIndex = rowIndex + 1
ColIndex = 0
For Each Col In Table. Columns

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.