Npoi importing data from Excel into a database

Source: Internet
Author: User

    using (FileStream fs=file.openread (@ "D:/my.xls")) {iworkbook wk = new Hssfworkbook (FS); if (wk. numberofsheets>0) {Isheet sheet = wk.                    Getsheetat (0); for (int i = 0; I <= sheet. Lastrownum; i++) {IRow row = sheet.                        GetRow (i); Id, Name, ParentID//string name=row. Getcell (1).                        Celltype==celltype.string.stringcellvalue; String Name = row. Getcell (1). Stringcellvalue = = "Null"? Null:row. Getcell (1).                        Stringcellvalue; Int? ParentID = row. Getcell (2) = = null? Null: (int?) Row. Getcell (2).                        Numericcellvalue;                        String sql = "INSERT into TreeView values (@Name, @ParentID)"; sqlparameter[] PMS = new sqlparameter[]{new SqlParameter ("@Name", Name==null? DBNull.Value: (object) Name), NEW SqlParameter ("@ParentID", Parentid==null?                        DBNull.Value: (object) parentid)};                    Sqlhelper.exectenonquery (System.Data.CommandType.Text, SQL, PMS); }                }                            }
   Public classDoexcel { Public Static voidLogetoexcel (string[] STRs) {DateTime dt=DateTime.Now; stringDIR ="/apilogs/"+ DateTime.Now.ToString ("YYYY-MM-DD") +". xls"; stringPath =HttpContext.Current.Server.MapPath (dir); intRowNum =0; if(!file.exists (path)) {                //directory.createdirectory (path); //Create a workbookIworkbook wk =NewHssfworkbook (); //Create sheetIsheet sh = wk. Createsheet ("Sheet1"); IRow Row=Sh.                CreateRow (RowNum);  for(inti =0; I < STRs. Length; i++) {Icell Icell=row.                    Createcell (i);                Icell.setcellvalue (Strs[i]); }                using(FileStream fs =file.openwrite (path)) {wk.                Write (FS); }            }            Else{FileStream FSS=file.openread (path); Iworkbook wk=NewHssfworkbook (FSS); intInsertrowindex = wk. Getsheetat (0). lastrownum+1; Exportexcelbytemple (STRs, path, path, -, Insertrowindex); }        }        /// <summary>        ///export Excel with a template/// </summary>        /// <param name= "table" ></param>        /// <param name= "strFileName" >Export Path</param>        /// <param name= "Templetpath" >Template Path</param>        /// <param name= "StartRow" >start writing data from the first few lines, starting with 1</param>         Public Static voidExportexcelbytemple (string[] STRs,stringstrFileName,stringTempletpath,intRowHeight,intStartRow) {            Try{Hssfworkbook Workbook=Getworkbook (Templetpath); Isheet sheet=getsheet (Workbook);                WriteData (Workbook, Sheet, STRs, strFileName, RowHeight, StartRow);            SaveData (workbook, strFileName); }            Catch(Exception ex) {Throwex; }        }        /// <summary>        ///parse Excel template, return workbook/// </summary>        /// <param name= "Templetpath" ></param>        /// <returns></returns>        Private StaticHssfworkbook Getworkbook (stringTempletpath) {FileStream file=NewFileStream (Templetpath, FileMode.Open, FileAccess.Read); Hssfworkbook Workbook=Newhssfworkbook (file); returnworkbook; }        /// <summary>        ///Back to sheet/// </summary>        /// <param name= "Workbook" ></param>        /// <returns></returns>        Private Staticisheet getsheet (Hssfworkbook workbook) {returnWorkbook. Getsheetat (0); }        /// <summary>        ///         /// </summary>        /// <param name= "Workbook" ></param>        /// <param name= "sheet" ></param>        /// <param name= "Dtsource" ></param>        /// <param name= "strFileName" ></param>        /// <param name= "RowHeight" ></param>        /// <param name= "StartRow" ></param>        /// <param name= "Size" ></param>        Private Static voidWriteData (Hssfworkbook workbook, Isheet sheet,string[] STRs,stringstrFileName,intRowHeight,intStartRow) {            // //filling the table headerIRow DataRow =NewHssfrow (); //Populating contentDataRow =sheet.            CreateRow (StartRow); Datarow.height= ( Short) (RowHeight * -);  for(intj =0; J < STRs. Length; J + +)            {                stringDrvalue =Strs[j]; Datarow.createcell (j).            Setcellvalue (Drvalue); }        }        /// <summary>        ///Save Data/// </summary>        /// <param name= "Workbook" ></param>        /// <param name= "strFileName" ></param>        Private Static voidSaveData (Hssfworkbook workbook,stringstrFileName) {            //Save            using(MemoryStream ms =NewMemoryStream ()) {                using(FileStream fs =NewFileStream (strFileName, FileMode.Create, FileAccess.Write)) {Workbook.                Write (FS); }            }        }    }
Add a piece of information to an existing Excel

  

Npoi importing data from Excel 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.