How to upload and read data in an Excel file

Source: Internet
Author: User

You can set the cell format in Excel.
If the format is different from the format of the input data, the data cannot be read.

For example, I have set the format of a cell column to date.
If the data I entered is indeed in the date format, such as 2002-1-1 1, it can be correctly read.
However, if the data I input is not in the date format, it is a letter like abc or another
In this case, the read data is empty.

Code for reading Excel files

OleDbConnection objConn = null;
DataSet data = new DataSet ();
Try
{
String strConn = "Provider = Microsoft. Jet. OleDb.4.0;" + "data source =" + Path + "; Extended Properties = Excel 8.0 ;";
ObjConn = new OleDbConnection (strConn );
ObjConn. Open ();
DataTable schemaTable = objConn. GetOleDbSchemaTable (System. Data. OleDb. OleDbSchemaGuid. Tables, null );
String tableName = schemaTable. Rows [0] [2]. ToString (). Trim ();
String strSql = "Select * From [" + tableName + "]";
OleDbCommand objCmd = new OleDbCommand (strSql, objConn );
OleDbDataAdapter sqlada = new OleDbDataAdapter ();
Sqlada. SelectCommand = objCmd;
Sqlada. Fill (data, "BuyPrice ");
ObjConn. Close ();
}
Catch
{
ObjConn. Close ();
Throw;
}
Return data;

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.