Vb.net excel files

Source: Internet
Author: User

You must display the data in an excel table and set the data format. Even so, it takes a lot of time to implement your own functions. I believe many people like me need to implement this function. Let's make a summary. Correct the error.

1. Add reference -- com --- microsoft excel 11.0 object library

2. Define when you need to access excel

Dim exapp as excel. application 'defines excel applications
Dim exbook as excel. workbook 'defines a workbook
Dim exsheet as excel. worksheet 'defines a worksheet
Dim exrange as excel. range' defines the Work Area

3. With the above definition, the excel operations are basically captured.

Exapp. visible = true' show excel program
Exbook = exapp. workbooks. add () 'add a new workbook or exbook = exapp. workbooks. open ("thin name")/open an existing workbook
Exsheet = exbook. sheets (n) 'gets the control handle of the nth worksheet, which will be processed later
Exsheet. cells (row, col) = value 'is used to assign values to a specified cell. This operation is frequently used.
The exsheet. range (cells, cells). font property is also commonly used. The setting format is:
Exsheet. range (cells, cells). font. colorindex = 3' set the color
Exsheet. range (cells, cells). Borders. LineStyle = 1 'set the border line
Exsheet. range (cells, cells). EntireColumn. AutoFit () 'automatic column width/can be changed to automatic Row Height.
Exsheet. range (cells, cells). merge' merge cells. This is also used for complicated tables.
Exsheet. range (cells, cells). HorizontalAlignment = 3' horizontal center, of course, you can also change to vertical
ExSheet. PageSetup. Orientation = 2' horizontal page

With these skills, you can basically achieve some requirements.

4. The last step is to save the file.

Exsheet. saveas ("file name ")

Or

Exbook. save ()

5. Clear Variables

exsheet=nothing
exbook.close
exbook=nothing
exapp.quit
exapp=nothing

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.