Two methods DataGridView Easy Import into Excel

Source: Internet
Author: User

As we all know, the DataGridView control to handle the table all kinds of data is really "simple, convenient, quick effect", so the majority of people love. Today we will continue to explore the "sister sentiment" about it and Excel.

Almost no one does not understand Excel, but to say that familiarity and understanding of the thorough I am afraid we are still far from doing (this is the outside word), the focus is Excel's powerful data processing ability to bring us the convenience, So it's important to turn the DataGridView Data sheet into an Excel form for all ages.

In fact, this conversion is also very simple, the following for you to introduce the next two methods, easy to connect "sister sentiment."

Method One:

This method does not require any loading and referencing, only pure code, the execution efficiency is high.

 Public Function Exportexcel (ByVal GVW as DataGridView) as Boolean ' export to an Excel function Try If GVW. Rows.Count <= 0 Then ' determine the number of records, if there is no record, exit MessageBox.Show ("No records can be exported", "no items can be exported", MessageBoxButtons.OK, Mes                sageboxicon.information) Return False Else ' If a record is exported to Excel Dim Objcreateexcel As Object Dim Objaddexcel As Object Objcreateexcel = CreateObject ("Excel . Application ") ' Create an Excel object Objaddexcel = ObjCreateExcel.workbooks.add () Dim Inttitle as Intege                R Dim intRow As Integer Dim intcolumn As Integer ' Define loop variable, row and column variable ' writes the header to Excel For inttitle = 0 to GVW. Columns.count-1 objaddexcel.worksheets (1). Cells (1, inttitle + 1) = GVW. Columns (Inttitle). Headercell.value Next ' adds data for intRow = 0 to GVW with loops.                  Rows.count-1 ' line loop  For intcolumn = 0 to GVW. Columns.count-1 ' column loop objaddexcel.worksheets (1). Cells (IntRow + 2, Intcolumn + 1) = GVW. Item (Intcolumn, IntRow). Value.tostring Next Next Objaddexcel.worksheets (1).                Cells.EntireColumn.AutoFit () ' AutoFit Excel column objcreateexcel.visible = True ' setting Excel visible as an Excel worksheet name        Objaddexcel = Nothing ' frees resource objcreateexcel = Nothing ' Frees resource End If Return True Catch ex as Exception ' Error handling MessageBox.Show (Err.Description.ToString, "error", MessageBoxButtons.OK, Messageboxi Con. Error) ' Error prompt Return False end Try End Function

Method Two: You need to refer to Office under the. NET Framework assembly, but with less code, as follows:

1. Add Reference ()


And then select

2. Code

Imports Microsoft ' Add Reference module Mdltext public Function Exportexcel (ByVal GVW as DataGridView) as Boolean ' Export to Excel function ' Establish Excel connection Dim Myexcel as New Microsoft.Office.Interop.Excel.Application () myExcel.Application.Workb Ooks.        Add (true) Myexcel.visible = True ' defines the loop variable, the row and column variable Dim intRow As Integer Dim intcolumn As Integer Dim Inttitle as Integer ' add header for inttitle = 0 to GVW. ColumnCount-1 myexcel.cells (1, inttitle + 1) = GVW. Columns (Inttitle). HeaderText Next Inttitle ' adds the data in the control to the table for IntRow = 0 to GVW by looping. RowCount-1 for intcolumn = 0 to GVW. ColumnCount-1 ' Because the first row is the table header, the data is added from the second line starting with Myexcel.cells (IntRow + 2, Intcolumn + 1) = GVW (intCo Lumn, IntRow). Value.tostring Next Intcolumn Next IntRow return True ' add complete, return True End function 
Endmodule

Although compared with the first time to do a fee system is a lot simpler, but this function due to the existence of universality and good versatility, or we need to seriously treat. How to maximize the user-friendly use is our ultimate goal.

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.