@SuppressWarnings ("deprecation")
public void Exportstudent () {
String filename= "Examination room Information table" +datekit.getcurrentdatetime ("YYYYMMDDHHMMSS") + ". xls";
String path=getrequest (). Getrealpath ("\ \") + "upload\\excel\\" +filename;
/*file Desktopdir = Filesystemview.getfilesystemview (). Gethomedirectory ();
String DesktopPath = Desktopdir.getabsolutepath (); */
SYSTEM.OUT.PRINTLN (path);
File File=new file (path);
Hssfworkbook wb = new Hssfworkbook (); ---> Created an Excel file
Hssfsheet sheet = wb.createsheet ("Examination room Information table"); ---> Create a workbook
Hssfdataformat format= Wb.createdataformat (); ---> Cell content format
Sheet.setcolumnwidth (short) 3, 20* 256); ---to set the cell width, because the width of one cell is fixed so the following cell height is determined, so this method is sheet.
Sheet.setcolumnwidth (short) 4, 40* 256); ---> The first parameter refers to which cell, the second argument is the width of the cell
Sheet.setdefaultrowheight ((short) 150); ----> When you want to set the height of a uniform cell, use this method
Sheet.setcolumnwidth (short) 0, 20* 256);
Sheet.setcolumnwidth (short) 1, 30* 256);
Sheet.setcolumnwidth (short) 2, 40* 256);
Sheet.setcolumnwidth (short) 5, 20* 256);
Sheet.setcolumnwidth (short) 6, 20* 256);
Sheet.setcolumnwidth (short) 7, 20* 256);
Sheet.setcolumnwidth (short) 8, 20* 256);
Sheet.setcolumnwidth (short) 9, 20* 256);
Sheet.setcolumnwidth (short) 10, 20* 256);
Sheet.setcolumnwidth (short) 11, 20* 256);
Sheet.setcolumnwidth (short) 12, 20* 256);
Sheet.setcolumnwidth (short) 13, 20* 256);
Style 1
Hssfcellstyle style = Wb.createcellstyle (); Style object
Style.setverticalalignment (hssfcellstyle.vertical_center);//Vertical
Style.setalignment (hssfcellstyle.align_center);//Level
Formatting the title font
Hssffont font = Wb.createfont ();
Set font style
Font.setfontheightinpoints ((short) 12); ---> Set font size
Font.setfontname ("Courier New"); ---"Set the font, what type is it for example: Arial
Font.setitalic (FALSE); ---> Whether the setting is bold
Style.setfont (font); ---> Add font formatting to the style
Style.setfillforegroundcolor (IndexedColors.DARK_YELLOW.getIndex ());
Style.setfillpattern ("YELLOW");//Set cell color
Style.setwraptext (FALSE); Sets whether the line can be wrapped to change the behavior true
Third row of the table
Hssfrow row4 = sheet.createrow (0);
Row4.setheightinpoints ((short) 25);
Hssfcell cell4_1 = Row4.createcell ((short) 0);
Cell4_1.setcellstyle (style);
Cell4_1.setcellvalue ("matches");
Hssfcell cell4_2 = Row4.createcell ((short) 1);
Cell4_2.setcellstyle (style);
Cell4_2.setcellvalue ("Land City");
Hssfcell Cell4_3 = Row4.createcell ((short) 2);
Cell4_3.setcellstyle (style);
Cell4_3.setcellvalue ("school");
Hssfcell cell4_4 = Row4.createcell ((short) 3);
Cell4_4.setcellstyle (style);
Cell4_4.setcellvalue ("Student name");
Hssfcell cell4_5 = Row4.createcell ((short) 4);
Cell4_5.setcellstyle (style);
Cell4_5.setcellvalue ("public number");
Hssfcell cell4_6 = Row4.createcell ((short) 5);
Cell4_6.setcellstyle (style);
Cell4_6.setcellvalue ("code name");
try{
Long Competitionid = Getparatolong ("Competitionid");
QB QB = new QB ("Select A.XSID,A.CODE,A.USERCODE,B.XM,C.NAME,D.CITY,D.XXMC from Cttparticipant a left joins Cttxs B on A.xsi D = b.ID left joins Cttronda C on A.rondaid=c.id left joins Cttxx D on B.xxid=d.id WHERE a.competitionid=? ", Competitionid);
if (Strkit.isnotempty (Getpara ("Xsname"))) {
Qb.append ("and b.xm like", Qb.wraplike (Getpara ("Xsname"));
}
if (Strkit.isnotempty (Getpara ("City"))) {
Qb.append ("and D.city", Qb.wraplike (Getpara ("City"));
}
if (Strkit.isnotempty (Getpara ("Xxname"))) {
Qb.append ("and D.XXMC like", Qb.wraplike (Getpara ("Xxname"));
}
if (Strkit.isnotempty (Getpara ("Usercode"))) {
Qb.append ("and A.usercode like", Qb.wraplike (Getpara ("Usercode"));
}
Qb.append (Getorderby ("C.name,a.code"));
list<record> list = Db.find (QB);
int j = 0; Increase row
for (Record record:list) {
Hssfrow Rown = Sheet.createrow (1+j);
Rown.setheightinpoints ((short) 25);
Hssfcell Cell_1 = Rown.createcell ((short) 0);
Cell_1.setcellstyle (style);
if (Strkit.isnotempty (Record.getstr ("name"))) {
Cell_1.setcellvalue (Record.getstr ("name"));
}else{
Cell_1.setcellvalue ("");
}
Hssfcell cell_2 = Rown.createcell ((short) 1);
Cell_2.setcellstyle (style);
if (Strkit.isnotempty (Record.getstr ("City"))) {
Cell_2.setcellvalue (Record.getstr ("City"));
}else{
Cell_2.setcellvalue ("");
}
Hssfcell cell_3 = Rown.createcell ((short) 2);
Cell_3.setcellstyle (style);
if (Strkit.isnotempty (Record.getstr ("XXMC"))) {
Cell_3.setcellvalue (Record.getstr ("XXMC"));
}else{
Cell_3.setcellvalue ("");
}
Hssfcell cell_4 = Rown.createcell ((short) 3);
Cell_4.setcellstyle (style);
if (Strkit.isnotempty (Record.getstr ("XM"))) {
Cell_4.setcellvalue (Record.getstr ("XM"));
}else{
Cell_4.setcellvalue ("");
}
Hssfcell cell_5 = Rown.createcell ((short) 4);
Cell_5.setcellstyle (style);
if (Strkit.isnotempty (Record.getstr ("Code"))) {
Cell_5.setcellvalue (Record.getstr ("Code"));
}else{
Cell_5.setcellvalue ("");
}
Hssfcell cell_6 = Rown.createcell ((short) 5);
Cell_6.setcellstyle (style);
if (Strkit.isnotempty (Record.getstr ("Usercode"))) {
Cell_6.setcellvalue (Record.getstr ("Usercode"));
}else{
Cell_6.setcellvalue ("");
}
j + +;
}
}catch (Exception e) {
E.printstacktrace ();
}
FileOutputStream fileout = null;
try{
Fileout = new FileOutputStream (file);
Wb.write (fileout);
Rendereasyuisuccess (FileName);
System.out.println (FileName);
}catch (Exception e) {
E.printstacktrace ();
Rendereasyuisuccess ("Export failed!");
}
finally{
if (fileout! = null) {
try {
Fileout.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}
-----------------------------------------------
Import
public void Tolead () throws exception{
UploadFile file = getFile ("file");
File tempxlsfile = File.getfile ();
Reading files from Excel
Long Comid = Getparatolong ("Comid");
String Xmlpath = Pathkit.getwebrootpath () + "/zjvexml/gwh.xml";
list<?> list = Easyxls.xls2list (Xmlpath, tempxlsfile);
for (Object object:list) {
map<string, object> map = (map<string, object>) Object;
Record re = Db.findfirst ("Select P.id pid, S.XM, X.XXMC from Cttparticipant P left joins Cttxs s on P.xsid=s.id left join C Ttxx x on S.xxid =x.id where s.xm=? and X.XXMC like? and P.competitionid =? ", Map.get (" XM "). ToString (). Trim (), Qb.wraplike (Map.get (" XXMC "). ToString (). Trim ()), comid);
if (re!=null) {
Re.set ("Code", Map.get ("GWh"). ToString (). Trim ());
Db.update ("update cttparticipant SET code =?") WHERE id =? ", Map.get (" GWh "). ToString (). Trim (), Re.getlong (" pid "));
}else{
Re = Db.findfirst ("Select P.id pid, S.XM, X.XXMC from Cttparticipant P left joins Cttxs s on p.xsid=s.id left join CTTXX x On S.xxid =x.id where s.xm=? and P.competitionid =? ", Map.get (" XM "). ToString (). Trim (), comid);
if (re!=null) {
Re.set ("Code", Map.get ("GWh"). ToString (). Trim ());
Db.update ("update cttparticipant SET code =?") WHERE id =? ", Map.get (" GWh "). ToString (). Trim (), Re.getlong (" pid "));
}
}
}
Tempxlsfile.delete ();
Rendereasyuisuccess ("Import succeeded! ");
}
Jfinal Import, Export excl