Import EXCEL to SQL Server... returns a table in the workbook and forms a datatable.

Source: Internet
Author: User

the format must be unified. Otherwise, the Program cannot be determined based on any criteria,
# region returns a table in the opened workbook and forms a datatable
///


/// returns a table in the opened workbook and form a datatable
///
/// absolute file path
// name of the table to be opened
// returns the datatable database set

Private void readexcel ()
{
Try
{
Datatable rs = new datatable ();
Rs = NULL;
Dataset RDS = NULL;
String strconn = "";
Int COUNT = 0;
String id = request ["ID"];
Datetime createtime = datetime. now;
String date = createtime. year. tostring () + "-" + createtime. month. tostring () + "-" + createtime. day. tostring () + "" + createtime. hour. tostring () + ":" + createtime. minute. tostring () + ":" + createtime. second. tostring ();
String sqlstr = "select * From tab_uppart_file where id =" + ID + "";
String userid = data1.readout (sqlstr, 1 );
String filename = data1.readout (sqlstr, 3 );
String Path = server. mappath ("../upparts/" + filename );

String filepath = ".. \ upparts \" + filename;
If (file. exists (server. mappath (filepath) // checks whether the file exists.
{
Strconn = "provider = Microsoft. jet. oledb.4.0; Data Source = "+ path +"; extended properties = 'excel 8.0; HDR = yes; IMEX = 1'; "; // IMEX = 1 to convert text
Oledbconnection conn = new oledbconnection (strconn );
Oledbdataadapter mycommand = new oledbdataadapter ("select * from [sheet1 $]", strconn );
Dataset mydataset = new dataset ();
Mycommand. Fill (mydataset, "[sheet1 $]");
Foreach (datarow row in mydataset. Tables [0]. Rows)
{
Count ++;
String classid = "";
String name = "";
String Model = "";
String price = "";
String num = "";
String MFG = "";
String package = "";
String remark = "";
Classid = row ["product type"]. tostring ();
Name = row ["Product Name"]. tostring ();
Model = row ["Product Model"]. tostring ();
Price = row ["product price"]. tostring ();
Num = row ["quantity"]. tostring ();
MFG = row ["manufacturer"]. tostring ();
Package = row [""]. tostring ();
Remark = row ["transaction description"]. tostring ();
If (name! = "" & Model! = "")
{
Sqlstr = "insert into tab_part values ('" + name + "', '" + model + "', '" + num + "', '" + package + "', '"+ MFG +"', '"+ Price +"', '"+ remark +"', '"+ classid +"', '"+ createtime + "', '"+ userid +"', '0 ')";
Data1.moddata (sqlstr );
}
}
String updatesql = "Update tab_uppart_file set flag = '1', uploadtime = '" + date + "'where id ='" + ID + "'";
Data1.moddata (updatesql );
Data1.alert2 ("Upload record successful! "," Batchaddparts. aspx ", page );
}
Else
{
Data1.alert2 ("corresponding template file not found", "batchaddparts. aspx", page );
}
}
Catch (system. Data. oledb. oledbexception ex)
{
Response. Write (ex. Message );
}

}
# 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.