Create Excel-DEMO and javaexcel-demo in Java

Source: Internet
Author: User

Create Excel-DEMO and javaexcel-demo in Java

Import java. io. file; import java. io. fileOutputStream; import java. io. IOException; import java. io. outputStream; import java. text. simpleDateFormat; import java. util. date; import jxl. workbook; import jxl. write. dateFormats; import jxl. write. dateTime; import jxl. write. label; import jxl. write. writableCellFormat; import jxl. write. writableSheet; import jxl. write. writableWorkbook; import jxl. write. writeException; public Class SimpleExcelDemo {private static int column = 0; private static int row = 0; // create a working thin public static void createExcelFile (String demand_id) throws WriteException, IOException {// create a File directory String realPath = "C: \ excel \"; File fileDir = new File (realPath); if (! FileDir. exists () {fileDir. mkdirs ();} SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); String date = sdf. format (new Date (System. currentTimeMillis (); String fileName = date + ". xls "; String filePath = realPath + fileName; File file = new File (filePath); WritableCellFormat wf = new WritableCellFormat (DateFormats. FORMAT1); SimpleDateFormat sdf1 = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss" ); String current = sdf1.format (new Date (System. currentTimeMillis (); // create a file output stream to write the table to the local folder OutputStream out = null; WritableWorkbook wwb = null; WritableSheet sheet = null; Label cell; try {if (! File. exists () {out = new FileOutputStream (file); // create an excel file (create a readable and written Workbook) wwb = Workbook. createWorkbook (out); // create a new sheet. createSheet ("processed", 0); // create the content to be displayed and create a cell. The first parameter is the column coordinate, and the second parameter is the row coordinate, the third parameter is content cell = new Label (column, row, "requirement ID"); sheet. addCell (cell); cell = new Label (column + 1, row, "Upload time"); sheet. addCell (cell); cell = new Label (column, ++ row, demand_id); sheet. addCell (cell); Date d = New Date (); d = sdf1.parse (current); DateTime dt = new DateTime (column + 1, row, d, wf); sheet. addCell (dt);} else {File [] files = fileDir. listFiles (); for (File f: files) {if (f. getName (). equals (fileName) {Workbook wb = Workbook. getWorkbook (f); wwb = Workbook. createWorkbook (new File (realPath + fileName), wb); sheet = wwb. getSheet ("processed"); cell = new Label (column, ++ row, demand_id); sheet. addCell (cel L); Date d = new Date (); d = sdf1.parse (current); DateTime dt = new DateTime (column + 1, row, d, wf); sheet. addCell (dt) ;}}} catch (Exception e) {e. printStackTrace ();} wwb. write (); // write to the Excel worksheet wwb. close (); // close the Excel worksheet and close the IO stream.} Public static void main (String [] args) throws Exception {createExcelFile ("1234"); System. out. println (column + "," + row); // create the File directory File fileDir = new File ("C: \ excel"); if (! FileDir. exists () {fileDir. mkdirs ();} SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); String date = sdf. format (new Date (System. currentTimeMillis (); String fileName = date + ". xls "; String filePath =" C: \ excel \ "+ fileName; File file = new File (filePath); // create a File output stream, used to write a table to the local folder OutputStream out = new FileOutputStream (file); // create an excel file (create a readable and written Workbook) WritableWorkbook wwb = Workbook. CreateWorkbook (out); // create a new page named WritableSheet sheet = wwb. createSheet ("processed", 0); // create the content to be displayed and create a cell. The first parameter is the column coordinate, and the second parameter is the row coordinate, the third parameter is content Label cell = new Label (, "requirement ID"); sheet. addCell (cell); cell = new Label (1, 0, "Upload time"); sheet. addCell (cell); cell = new Label (0, 1, "5055"); sheet. addCell (cell); WritableCellFormat wf = new WritableCellFormat (DateFormats. FORMAT1); SimpleDateFormat sdf1 = new SimpleDateForm At ("yyyy-MM-dd HH: mm: ss"); String current = sdf1.format (new Date (System. currentTimeMillis (); Date d = sdf1.parse (current); DateTime birthday = new DateTime (1, 1, d, wf); sheet. addCell (birthday); wwb. write (); // write to the Excel worksheet wwb. close (); // close the Excel worksheet and close the IO stream. }}

 

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.