Java exports Excel Java operation files. Java zip

Source: Internet
Author: User
Tags create zip create zip file
/*** Export the instructor information */public static Boolean exportteach (string filepath, string teachname, string grade, string classes, string subject) {// Step 1: Create a webbook, corresponding to an Excel file hssfworkbook WB = new hssfworkbook (); // Step 2: Add a sheet to the webbook, corresponding to the sheet hssfsheet sheet = WB in the Excel file. createsheet (""); // Step 3: Add row 0th to the table header. Note that the old version of POI has a limit on the number of rows in the Excel worksheet. createrow (INT) 0); hssfcell cell = row. createcell (0); For (int s = 0; S <4; s ++) {Cell = row. createcell (s); If (S = 0) {Cell. setcellvalue ("instructor name");} else if (S = 1) {Cell. setcellvalue ("Grade");} else if (S = 2) {Cell. setcellvalue ("class");} else if (S = 3) {Cell. setcellvalue ("subject") ;}// Step 5: Write the object data to the database. Row = sheet. createrow (1); For (Int J = 0; j <4; j ++) {Cell = row. createcell (j); If (j = 0) {Cell. setcellvalue (teachname);} else if (j = 1) {C Ell. setcellvalue (grade);} else if (j = 2) {Cell. setcellvalue (classes);} else if (j = 3) {Cell. setcellvalue (subject) ;}}// Step 6: Save the file to the specified location. Try {If (createdir (filepath + "/teach ")) {fileoutputstream fout = new fileoutputstream (filepath + "/teach/teach.xls"); WB. write (fout); fout. close (); Return true;} else {return false;} catch (exception e) {e. printstacktrace (); Return false ;}}/*** @ see Export course preparation question information * @ para M filepath file path * @ Param problemlist course preparation question information */public static Boolean exporttopic (string filepath, list <problem> problemlist) {// Step 1: Create a webbook, corresponding to an Excel file hssfworkbook WB = new hssfworkbook (); // Step 2: Add a sheet to the webbook, corresponding to the sheet hssfsheet sheet = WB in the Excel file. createsheet ("course preparation question information"); // Step 3: Add row 0th to the table header in sheet. Note that the earlier version of POI has a limit on the number of rows in Excel. createrow (INT) 0); hssfcell cell = row. createcell (0); f Or (int s = 0; S <6; s ++) {Cell = row. createcell (s); If (S = 0) {Cell. setcellvalue ("question content");} else if (S = 1) {Cell. setcellvalue ("Answer");} else if (S = 2) {Cell. setcellvalue ("subject");} else if (S = 3) {Cell. setcellvalue ("Knowledge Point");} else if (S = 4) {Cell. setcellvalue ("");} else if (S = 5) {Cell. setcellvalue ("") ;}// Step 5: Write the object data to the actual application and obtain the data from the database. If (problemlist! = NULL & problemlist. size ()> 0) {int S = 0; For (INT I = 0; I <problemlist. size (); I ++) {problem = (problem) problemlist. get (I); ROW = sheet. createrow (++ S); For (Int J = 0; j <= 5; j ++) {Cell = row. createcell (j); If (j = 0) {Cell. setcellvalue (problem. getcontent ();} else if (j = 1) {Cell. setcellvalue (problem. getanswer ();} else if (j = 2) {Cell. setcellvalue (problem. getsubjects (). getname ()) ;} Else if (j = 3) {Cell. setcellvalue (problem. getknowledgepoints (). getknowledgecontent ();} else if (j = 4) {Cell. setcellvalue (problem. getknowledgepoints (). getremark2 ();} else if (j = 5) {Cell. setcellvalue (problem. getknowledgepoints (). getremark3 () ;}}}// Step 6: Save the file to the specified location. Try {If (createdir (filepath + "/problem ")) {fileoutputstream fout = new fileoutputstream (filepath + "/problem/problem.xls"); WB. W Rite (fout); fout. close (); Return true;} else {return false;} catch (exception e) {e. printstacktrace (); Return false ;}}/*** @ see create folder */public static Boolean createdir (string destdirname) {file dir = new file (destdirname ); if (dir. exists () {system. out. println ("creating directory" + destdirname + "failed, already exists !! ");} If (! Destdirname. endswith (file. separator) {destdirname = destdirname + file. separator;} // create a single directory if (dir. mkdirs () {system. out. println ("created successfully"); Return true;} else {system. out. println ("creation failed !! "); Return false ;}} /*** delete all folders and files in a folder ** @ Param delpath * string * @ throws filenotfoundexception * @ throws ioexception * @ return Boolean */public static Boolean deletefile (string delpath) throws exception {try {file = new file (delpath); // returns trueif (! File. isdirectory () {file. delete ();} else if (file. isdirectory () {string [] filelist = file. list (); For (INT I = 0; I <filelist. length; I ++) {file delfile = new file (delpath + "\" + filelist [I]); If (! Delfile. isdirectory () {delfile. delete (); system. out. println (delfile. getabsolutepath () + "File deleted successfully");} else if (delfile. isdirectory () {deletefile (delpath + "\" + filelist [I]);} system. out. println (file. getabsolutepath () + "deleted successfully"); file. delete () ;}} catch (filenotfoundexception e) {system. out. println ("deletefile () exception:" + E. getmessage ();} return true;}/*** create a zip file ** @ Param sourcepath * file or folder path * @ Param zippath * The generated ZIP file exists in the path (including the file name) */public static void createzip (string sourcepath, string zippath) {fileoutputstream Fos = NULL; zipoutputstream ZOS = NULL; try {Fos = new fileoutputstream (zippath); ZOS = new zipoutputstream (FOS); writezip (new file (sourcepath), "", ZOS);} catch (filenotfoundexception E) {system. out. println ("failed to create ZIP file");} finally {try {If (ZOS! = NULL) {ZOS. close () ;}} catch (ioexception e) {system. out. println ("failed to create the ZIP file "));}}} /*** create a zip package ** @ Param file * @ Param parentpath * @ Param ZOS */Private Static void writezip (File file, string parentpath, zipoutputstream ZOS) {ZOS. setencoding ("GBK"); If (file. exists () {If (file. isdirectory () {// process the folder parentpath + = file. getname () + file. separator; file [] files = file. listfiles (); For (file F: fil Es) {ZOS. setencoding ("GBK"); writezip (F, parentpath, ZOS) ;}} else {fileinputstream FCM = NULL; datainputstream Dis = NULL; try {FD = new fileinputstream (File ); dis = new datainputstream (New bufferedinputstream (FCM); zipentry ze = new zipentry (parentpath + file. getname (); ZOS. putnextentry (ze); ZOS. setencoding ("GBK"); byte [] content = new byte [1024]; int Len; while (LEN = FCM. read (content ))! =-1) {ZOS. write (content, 0, Len); ZOS. flush () ;}} catch (filenotfoundexception e) {system. out. println ("failed to create the ZIP file");} catch (ioexception e) {system. out. println ("failed to create ZIP file");} finally {try {If (DIS! = NULL) {dis. Close () ;}} catch (ioexception e) {system. Out. println ("failed to create ZIP file "));}}}}}

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.