WinForm importing Excel data into a database

Source: Internet
Author: User

  Public Partial classimportexcel:form {aceesshelpers accesshelper=Newaceesshelpers ();  PublicImportexcel () {InitializeComponent (); }        Private voidBtn_importexceldata (Objectsender, EventArgs e) {Openfiledialog1.title="Open File"; Openfiledialog1.showhelp=true; Openfiledialog1.restoredirectory=true; //openfiledialog1.filter = "Excel file (*.xlsx) | (*.xls) ";            if(Openfiledialog1.showdialog () = =DialogResult.OK) { This. Txt_path. Text =Openfiledialog1.filename; }        }        Private voidBtn_upload_click (Objectsender, EventArgs e) {            stringPath =Openfiledialog1.filename; System.Data.DataTable DT=getdatatable (path); intresult =0;  for(inti =1; i < dt. Rows.Count; i++)            {                stringsql ="INSERT INTO products (Productname,area,productcategory,customer,style,structurestyle,width,square,other) VALUES ('"+convert.tostring (dt. rows[i]["F1"]) +"', '"+ convert.tostring (dt. rows[i]["F2"]) +"', '"+ convert.tostring (dt. rows[i]["F3"]) +"', '"+convert.tostring (dt. rows[i]["F4"]) +"', '"+ convert.tostring (dt. rows[i]["F5"]) +"', '"+ convert.tostring (dt. rows[i]["F6"]) +"', '"+convert.tostring (dt. rows[i]["F7"]) +"', '"+ convert.tostring (dt. rows[i]["F8"]) +"', '"+ convert.tostring (dt. rows[i]["F9"]) +"')"; intj =accesshelper.returnsql (SQL); Result= j + +; }            if(Result >0)            {                 This.            Close (); }        }        /// <summary>        ///read the contents of a specified page in Excel/// </summary>        /// <param name= "Strexcelfilename" >Excel Path</param>        /// <returns></returns>        protectedSystem.Data.DataTable getdatatable (stringstrexcelfilename) {            //DataSet myDs = new DataSet ();System.Data.DataTable dt =NewSystem.Data.DataTable ();            DataRow Myrow; Objectomissing =System.Reflection.Missing.Value; Microsoft.Office.Interop.Excel.Application Application=NewMicrosoft.Office.Interop.Excel.Application ();            Microsoft.Office.Interop.Excel.Workbooks Workbooks; //Microsoft.Office.Interop.Excel.Worksheet Worksheet;Microsoft.Office.Interop.Excel.Workbook Workbook; Workbooks=application.            Workbooks; Workbook=Returnworkbook (strexcelfilename, workbooks); //worksheet = (worksheet) workbook. SHEETS[1];             for(intSheetint =1; Sheetint <2; sheetint++)//DateTime.Now.Day Convert.todatetime (TextBox4.Text.Trim ()). Day;//MyBook.Worksheets.Count;//can get the number of sheet{Worksheet MySheet=(Worksheet) workbook.                Worksheets[sheetint]; intRowsint = MySheet.UsedRange.Cells.Rows.Count;//get the number of rows                intColumnsint = MySheet.UsedRange.Cells.Columns.Count;//get the number of columns                if(Sheetint = =1)                {                     for(inti =1; I <= columnsint; i++) {dt. Columns.Add ("F"+ i.tostring (), System.Type.GetType ("System.String")); }                }                 for(inti =2; I <= rowsint; i++)//first behavior title, not read{myrow=dt.                    NewRow ();  for(intj =1; J <= Columnsint; J + +) {Range R=(Range) Mysheet.cells[i, j]; stringstrvalue =r.text.tostring (); stringColumnName ="F"+j.tostring (); Myrow[columnname]= (Strvalue.length = =0|| Strvalue.contains ("#")) ?" ": strvalue; }                    Try{dt.                    Rows.Add (Myrow); }                    Catch{}}} workbook.            Close (omissing, omissing, omissing); Workbooks.            Close (); Application.            Quit (); returnDT; }        PrivateWorkbook Returnworkbook (stringFileName, Workbooks works)//here is a work table to open{Microsoft.Office.Interop.Excel.Workbook wk=works. Open (filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type .                     Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,            Type.Missing, Type.Missing); returnwk; }    }

WinForm importing Excel data into a database

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.