Export reports in the. NET environment Excel and Word lihonggen0 (original)

Source: Internet
Author: User
Tags date chr count integer range
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

For each Row in Table.rows

RowIndex = RowIndex + 1

Colindex = 0

For each Col in Table.columns

Colindex = Colindex + 1

Xlapp.cells (RowIndex, Colindex) = Row (Col.columnname)

Next

Next

With Xlsheet

. Range (. Cells (1, 1),. Cells (1, Colindex)). Font.Name = "Blackbody"

' Set title to bold text

. Range (. Cells (1, 1),. Cells (1, Colindex)). Font.Bold = True

' Title font Bold

. Range (. Cells (1, 1),. Cells (RowIndex, Colindex)). Borders.LineStyle = 1

' Set table border style

End With

With Xlsheet.pagesetup

. LeftHeader = "" & Chr (Ten) & "&" "_gb2312 in italics, General" "&10 Company Name:" ' & GSMC

. CenterHeader = "&" "italics _gb2312, General" "Company Personnel Table &" "Song Body, General" "" & Chr (Ten) & "&" "italics _gb2312, General" "&10 Date:"

. RightHeader = "" & Chr (Ten) & "&" "_gb2312 in italics, general" "&10 Unit:"

. LeftFooter = "&" "italics _gb2312, General" "&10 Watchmaker:"

. CenterFooter = "&" "italics _gb2312, General" "&10 Tabulation Date:"

. RightFooter = "&" "italics _gb2312, General" "&10 &p Page Total &n page"

End With

Xlapp.visible = True

3. Export the contents of a table to Word

Dim WordApp as New Word.Application ()

Dim MyDoc as Word.Document

Dim otable as Word.table

Dim RowIndex, Colindex as Integer

RowIndex = 1

Colindex = 0

WORDAPP.DOCUMENTS.ADD ()

MyDoc = Wordapp.activedocument

Dim Table as New DataTable ()

Table = creatable ()

otable = MyDoc.Tables.Add (Range:=mydoc.range (start:=0, end:=0), Numrows:=table.rows.count + 1, numcolumns:= Table.Columns.Count)

' 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

Otable.cell (1, Colindex). Range.InsertAfter (Col.columnname)

Next

' All rows of the resulting table, assigned to cells

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.Borders.InsideLineStyle = 1

OTable.Borders.OutsideLineStyle = 1

Wordapp.visible = True

Summarize:

E_mail:lihonggen0@163.com



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.