C # Read Excel Data to the Gridview

Source: Internet
Author: User

# Region read Excel Data to the Gridview

Public void ReadExcel (string sExcelFile, GridView dgBom)
{

DataTable ExcelTable;
DataSet ds = new DataSet ();

// Excel connection
OleDbConnection objConn = new OleDbConnection ("Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + sExcelFile + ";" + "Extended Properties = Excel 8.0 ;");
ObjConn. Open ();
DataTable schemaTable = objConn. GetOleDbSchemaTable (System. Data. OleDb. OleDbSchemaGuid. Tables, null );

String tableName = schemaTable. Rows [0] [2]. ToString (). Trim (); // obtain the table name in Excel. The default value is sheet1.
String strSql = "select * from [" + tableName + "]";

OleDbCommand objCmd = new OleDbCommand (strSql, objConn );
OleDbDataAdapter myData = new OleDbDataAdapter (strSql, objConn );

// Fill in data
MyData. Fill (ds, tableName );

ObjConn. Close ();

ExcelTable = ds. Tables [tableName];
Int iColums = ExcelTable. Columns. Count; // Number of Columns
Int iRows = ExcelTable. Rows. Count; // number of Rows

// Define a two-dimensional array to store data read from an Excel table
String [,] storedata = new string [iRows, iColums];

ArrayList list = new ArrayList ();

For (int I = 0; I <ExcelTable. Rows. Count; I ++)
{
SupermarketVO vo = new SupermarketVO ();

For (int j = 1; j <ExcelTable. Columns. Count; j ++)
{
// Store data in an Excel table to an array
Storedata [I, j] = ExcelTable. Rows [I] [j]. ToString ();

If (j = 1)
{
Vo. Sup_nm = ExcelTable. Rows [I] [j]. ToString ();
}
Else if (j = 2)
{
Vo. Sup_linker = ExcelTable. Rows [I] [j]. ToString ();
}
Else if (j = 3)
{
Vo. Phone = ExcelTable. Rows [I] [j]. ToString ();
}
}

// Ignore this record if the name, contact, and phone number are empty
If (vo. Sup_nm.Trim () = "" & vo. Sup_linker.Trim () = "" & vo. Phone. Trim () = "")
{
Continue;
}
Else
{
List. Add (vo );
}
}

// Determine the number of records to facilitate the display of empty records
If (list. Count <1)
{
SupermarketVO vo = new SupermarketVO ();
List. Add (vo );

// Set the display (including the display of the header) and binding record when an empty record is set
GridviewControl. GridViewDataBind (dgBom, list );
}
Else
{
DgBom. DataSource = list;
DgBom. DataBind ();
}

LblErrorInfo. Text = "the import operation has been completed. ";
}

# Endregion
 

Related Article

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.