Excel|word| export Excel to export reports in. NET environment Excel and Word lihonggen0 (original)
In the VB6 development environment, I used Excel as a report, in. NET environment development, I use Crystal Report. But vb.net can also export reports to Excel and Word for output, producing professional-level reports.
The actions are as follows: (Note: First you need to add a reference, select Com--> Select Microsoft Word 10.0 Object Library and Microsoft Excel 10.0 Object Library component)
1. Create a DataTable first, either as a data source or as another data source.
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 = "Line 1"
row! column 2 = 1
row! column 3 = "D"
row! column 4 = "a"
Dt. Rows.Add (Row)
Row1 = dt. NewRow ()
row1! column 1 = "Line 2"
row1! Column 2 = 12
row1! column 3 = "B"
row1! column 4 = "C"
Dt. Rows.Add (ROW1)
Return DT
End Function
2. Export the contents of a table to Excel
Dim xlapp as New excel.application ()
Dim Xlbook as Excel.Workbook
Dim Xlsheet as Excel.Worksheet
Dim RowIndex, Colindex as Integer
RowIndex = 1
Colindex = 0
Xlbook = Xlapp.workbooks (). Add
Xlsheet = Xlbook.worksheets ("Sheet1")
Dim Table as New DataTable ()
Table = creatable ()
' Assign the column name of the resulting 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 of the resulting table, assigned to cells
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.