The name of the table in the C # cmd.xls file.

Source: Internet
Author: User
Private void ExcelToDS (string Path)
{
String clientFilePath = FileUpload1.PostedFile. FileName; // the physical path of the Client File

Int I = clientFilePath. LastIndexOf ("."); // retrieve the index of the last "." In the file name
String extension = clientFilePath. Substring (I); // get the file extension

If (extension! = ". Xls ")
{
MsgBox. Alert ("the file format is. xls! "," ImportTeacherDate. aspx ");
Return;
}
Else
{
String myConn = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source = '" + clientFilePath + "'; Extended Properties = Excel 8.0 ";

OleDbConnection cnnxls = new OleDbConnection (myConn );

DataSet myDataSet = new DataSet (); // create a DataSet object

Cnnxls. Open ();
DataTable dt = cnnxls. GetOleDbSchemaTable (OleDbSchemaGuid. Tables, null );
String StyleSheet = dt. Rows [0] [2]. ToString (). Trim (); // The first table name of. xls

String StrSql = string. Format ("SELECT * FROM [{0}]", StyleSheet );
OleDbDataAdapter myCommand = new OleDbDataAdapter (StrSql, myConn );
MyCommand. Fill (myDataSet, string. Format ("[{0}]", StyleSheet ));
MyCommand. Dispose ();
DataTable DT = myDataSet. Tables [string. Format ("[{0}]", StyleSheet)];

For (int j = 0; j <DT. Rows. Count; j ++)
{
Try
{
String userLogin = BusinessFacadeFrameWork. get_table_fileds ("FrameWork_Member", "Name", "Name", DT. Rows [j] [0]. ToString (). Trim ());
If (! String. IsNullOrEmpty (userLogin ))
{
MsgBox. Alert (string. Format ("failed to add the instructor! {0} login name already exists! ", DT. Rows [j] [0]. ToString ()," ImportTeacherDate. aspx ");
Return;
}
Else
{
// Add basic instructor information to the user table
FrameWork_MemberEntity me = new FrameWork_MemberEntity ();
Me. DataTable_Action _ = DataTable_Action.Insert;
Me. Name = DT. Rows [j] [0]. ToString (). Trim ();
Me. PSW = System. Web. Security. FormsAuthentication. HashPasswordForStoringInConfigFile ("123456", "MD5"). ToUpper ();
Me. RegTime = DateTime. Now;
Me. TrueName = DT. Rows [j] [1]. ToString (). Trim ();
Me. MemberRoleID = 6;
Int rInt = BusinessFacadeFrameWork. FrameWork_MemberInsertUpdateDelete (me );
If (rInt <1)
{
MsgBox. Alert ("An error occurred while adding basic instructor information to the user table! "," ImportTeacherDate. aspx ");
Return;
}
Else
{
// Add the instructor details to the instructor table
FrameWork_TeacherEntity te = new FrameWork_TeacherEntity ();
Te. DataTable_Action _ = DataTable_Action.Insert;
Te. cLogin = DT. Rows [j] [0]. ToString (). Trim ();
Te. cName = DT. Rows [j] [1]. ToString (). Trim ();
Te. cIntro = DT. Rows [j] [2]. ToString (). Trim ();
Te. cPos = DT. Rows [j] [3]. ToString (). Trim ();
Te. cPhoto = DT. Rows [j] [4]. ToString (). Trim ();
Te. cTeacherCode = AdminLoginInfo. CCode;
Te. cTeachTel = DT. Rows [j] [5]. ToString (). Trim ();
Int rInt_te = BusinessFacadeFrameWork. FrameWork_TeacherInsertUpdateDelete (te );
If (rInt_te <1)
{
MsgBox. Alert ("An error occurred while adding basic instructor information to the user table! "," ImportTeacherDate. aspx ");
Return;
}
}
}
}
Catch (Exception ex)
{
MsgBox. Alert ("the table format provided by the user is incorrect", "ImportTeacherDate. aspx ");
Return;
}
}
Cnnxls. Close ();
MsgBox. Alert ("instructor data imported successfully! "," TeacherList. aspx? ModelID = 500 ");
}
}
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.