1 /// <summary>2 ///ways to bind data3 /// </summary>4 /// <param name= "Ssfworkbook" >Hssfworkbook</param>5 /// <param name= "Sheet1" >Hssfsheet</param>6 /// <param name= "Catedt" >Data Source</param>7 /// <param name= "SheetName" >page name</param>8 /// <param name= "Dispname" >Display Name</param>9 /// <param name= "Row" >falls into the first few columns</param>Ten /// <param name= "Nextcount" >The position of the next column (as long as there is 1, followed by 1, and so on)</param> One Private voidGetcategorydata (Hssfworkbook Ssfworkbook, Hssfsheet Sheet1, DataTable Catedt,stringSheetName,stringDispname,intRowintnextcount) A { - Try - { theHssfsheet Sheet2 = Ssfworkbook. Createsheet ("Sht"+sheetname); - intIndexcate =0; -Ssfworkbook. Setsheethidden (Row-nextcount,true);//Hidden - + foreach(DataRow Cateiteminchcatedt.rows) - { +Sheet2. CreateRow (indexcate). Createcell (0). Setcellvalue (Cateitem[dispname]. ToString ());//column Data Aindexcate++; at } -Hssfname range = Ssfworkbook. Createname ();//Create a name -Range. Reference ="Sht"+ SheetName +"! $A $: $A $"+ indexcate;//format -Range. NameName =SheetName; - - //Add a drop-down list inCellrangeaddresslist regions =NULL; -Regions =NewCellrangeaddresslist (1,65535, Row-1, Row-1); toDvconstraint constraint =Dvconstraint.createformulalistconstraint (sheetname); +Hssfdatavalidation datavalidate =NewHssfdatavalidation (regions, constraint); - Sheet1. Addvalidationdata (datavalidate); the } * Catch { } $}
1 /// <summary>2 ///Set Excel Header (array length must be the same)3 /// </summary>4 /// <param name= "StrName" >the name in Excel</param>5 /// <param name= "width" >Wide</param>6 /// <param name= "HeaderRow" >Hssfrow</param>7 /// <param name= "Sheet1" >Hssfsheet</param>8 /// <param name= "Hssfworkbook" >Ssfworkbook</param>9 Private stringSetupexceltitle (Hssfworkbook Ssfworkbook,string[] StrName,int[] width, hssfrow headerrow, Hssfsheet Sheet1)Ten { One Try A { - if(Strname.length! =width. Length) - { the return "array length is different! "; - } -Hssffont Fonttitle =Ssfworkbook. CreateFont (); -Fonttitle.fontheightinpoints = One; +Fonttitle.boldweight =Hssffont.boldweight_normal; -Hssfcellstyle Hssfcelltitle =Ssfworkbook. Createcellstyle (); + Hssfcelltitle.setfont (fonttitle); A at for(inti =0; i < strname.length; i++) - { - //set up the table header - Headerrow.createcell (i). Setcellvalue (Strname[i]); - //to set the width of a table header - Sheet1. Setcolumnwidth (i, width[i]); inHeaderrow.getcell (i). CellStyle =Hssfcelltitle; - } to return "OK"; + } - Catch(Exception ex) the { * returnEx. Message; $ }Panax Notoginseng}
1 /// <summary>2 ///set up Excel content3 /// </summary>4 /// <param name= "values" >content</param>5 /// <param name= "DataRow" >Hssfrow</param>6 Private voidSetupexcelcontent (string[] values, Hssfrow dataRow)7 {8 for(inti =0; I < values. Length; i++)9 {Ten //Populating Data One Datarow.createcell (i). Setcellvalue (Values[i]); A } -}
Package Npoi exporting Excel with drop-down list