. NET how to export DataGridView data to Excel tables

Source: Internet
Author: User

First of all, there are many answers to this question on the Internet, and there are many ways to solve it. Second, many people have written similar blogs. My blog is mainly addressed to those who have just contacted or contacted soon. NET and use it to do the program of the person, is to write a self, record the method, convenient for later use.

Add a DataGridView control to the form, DataGridView get the data: Datagridview1.datasource = crlist (generics) or datagridview1.datasource= Dataset.table (0). {Note: By querying the database, put the queried data into the Crlist or dataset and assign the value to DataGridView)

If you want to export data to an EXCEL table, you need to add a reference (project-Add Reference-microsoft.office.interop.excel).

The rest is the code. Here is my code, for reference only. (The Export Excel table is made into a class so that the entire program can call this class directly, implementing export data to Excel table)

<span style= "FONT-SIZE:18PX;" > ' Open Excel Application Dim exceladd As New Excel.Application () ' Add a work to this dim excelwordbook as Excel. Workbook = <span style= "FONT-SIZE:18PX;" >exceladd</span>. Workbooks.Add () ' Add a table Dim excelworksheet as Excel.Worksheet = <span style= "FONT-SIZE:18PX;" >excelwordbook</span>. Sheets ("Sheet1") ' Show Excel <span style= ' font-size:18px; >exceladd</span>. Visible = True ' is used to assign a table in DataGridView to a table in Excel Dim i As Integer Dim j As Integer ' will Datagridvie The field names in W are assigned to the first row in the Excel table Dim Cols as Integer for Cols = 1 to X.columns.count xlworksheet.cells (1, Col s) = X.columns (Cols-1). HeaderText Next ' imports the contents of the DataGridView table into an Excel table for i = 0 to x.rowcount-1 ' Datagrideview                The header row is not calculated as the number of rows for j = 0 to X.columncount-1 ' The first line of Excel is the title, so start with the second line, i+2 note that the coordinates of DataGridView are listed first Xlworksheet.Cells (i + 2, j + 1) = X (J, i).  Value.tostring () Next next</span>

Note: There is a allowuserstoaddrows attribute in the DataGridView attribute to be set to false, otherwise for i = 0 to x.rowcount-1 is rewritten as for i = 0 to X.rowcount-2 , and the system is prone to errors, but you can try it.

Summary: In doing this piece of time, there are a lot of problems, especially when using nested loops, if not set, often the export lost rows, data out of range, or not as you want to export, in short, I appeared a variety of problems, through the inquiry of various information, seniors learn elder sister's blog, resolved out, resolved only to find , the problem is a very small problem, but it is very difficult to find the problem, very small details, but also those you think can not be wrong place. Things to be careful, do more careful procedures, this blog, even to their own, but also to everyone, a word: "Do the procedure must be careful in careful." ”

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.