First, you add the NPOI.dll assembly Https://yunpan.cn/cMeSTELJSXmJJ access Password 8d83
Add the inside of the assembly to the reference.
The following code is exported from the database to Excel
1 Public voidDaochuexcel (stringpath)2 { //path is the path to save for the exported Excel 3 using(Stream fs =NewFileStream (Path, FileMode.Create, FileAccess.Write))4{//Create a table5Xssfworkbook work =NewXssfworkbook ();6 //Create page7Isheet sheet = work. Createsheet ("ZX");8list<memberinfo> list = Memdal.getallmemberbydelflag (0);9 //Create RowsTen for(inti =0; I < list. Count; i++) One { AIRow row =sheet. CreateRow (i); //define a row in Excel - - - //The Icell below represents the cells in the row -Icell cell2 = row. Createcell (0, celltype.string); + cell2. Setcellvalue (List[i]. MemName); - +Icell Cell3 = row. Createcell (1, celltype.string); A Cell3. Setcellvalue (List[i]. Memmobilephone); at -Icell cell4 = row. Createcell (2, celltype.string); - Cell4. Setcellvalue (List[i]. memaddress); - -Icell cell5 = row. Createcell (3, celltype.numeric); - cell5. Setcellvalue (List[i]. Memtype); in -Icell cell6 = row. Createcell (4, celltype.string); to cell6. Setcellvalue (List[i]. Memnum); + -Icell cell7 = row. Createcell (5, celltype.string); the Cell7. Setcellvalue (List[i]. Memgender); * $Icell Cell8 = row. Createcell (6, celltype.numeric);Panax Notoginseng Cell8. Setcellvalue (List[i]. Memdiscount); - theIcell cell9 = row. Createcell (7, celltype.numeric); + Cell9. Setcellvalue (List[i]. Memmoney); A theIcell cell10 = row. Createcell (8, celltype.numeric); + Cell10. Setcellvalue (List[i]. Delflag); - $Icell cell11 = row. Createcell (9, celltype.string); $ Cell11. Setcellvalue (List[i]. Subtime.toshortdatestring ()); - -Icell cell12 = row. Createcell (Ten, celltype.numeric); the cell12. Setcellvalue (List[i]. memintegral); - WuyiIcell cell13 = row. Createcell ( One, celltype.string); the Cell13. Setcellvalue (List[i]. Memendtime.toshortdatestring ()); - WuIcell cell14 = row. Createcell ( A, celltype.string); - Cell14. Setcellvalue (List[i]. Membirthday.toshortdatestring ()); About } $ Work . Write (FS); //Finally writes the table to the IO stream - - } - A}
The following is the import of a database from Excel
1 Public voidReadexcel (stringpath)2 {3 using(Stream fs =NewFileStream (Path,filemode.open,fileaccess.read)) //Read into Excel table from specified path 4 {5list<memberinfo> list =NewList<memberinfo>();6 //Work Table7Iworkbook Workbook =workbookfactory.create (FS);8 //Get page9Isheet sheet = workbook. Getsheetat (0);Ten //traversing rows One for(inti =0; I <=sheet. Lastrownum; i++) A { -MemberInfo mem =NewMemberInfo (); - //Get Row theIRow row =sheet. GetRow (i); - -Mem. MemName = row. Getcell (0). Stringcellvalue; -Mem. Memmobilephone = row. Getcell (1). Stringcellvalue; +Mem. memaddress = row. Getcell (2). Stringcellvalue; -Mem. Memtype = Convert.ToInt32 (row. Getcell (3). Numericcellvalue); +Mem. Memnum = row. Getcell (4). Stringcellvalue; AMem. Memgender = row. Getcell (5). Stringcellvalue; atMem. Memdiscount = convert.todouble (row. Getcell (6). Numericcellvalue); -Mem. Memmoney = row. Getcell (7). Numericcellvalue; -Mem. Delflag = Convert.ToInt32 (row. Getcell (8). Numericcellvalue); - -Mem. Subtime =convert.todatetime (row. Getcell (9). Stringcellvalue); -Mem. Memintegral = Convert.ToInt32 (row. Getcell (Ten). Numericcellvalue); inMem. Memendtime = Convert.todatetime (row. Getcell ( One). Stringcellvalue); -Mem. Membirthday = Convert.todatetime (row. Getcell ( A). Stringcellvalue); to list. ADD (MEM); + } - memdal.addmemberinfo (list); //Add the data body to the database the } * $ Panax Notoginseng}
Npoi exporting data from a database to Excel