Java and Excel (. xls files)---created using JXL to add table files

Source: Internet
Author: User

For some reasons to get the Excel file, personal feeling poi is too difficult, so used JXL (feel relatively simple).

1. Add an external archive Jxl.jar

2.

/*
* Generated XLS file first time need to manually select Excel Open
*
* */


Import Java.io.DataOutputStream;
Import java.io.FileNotFoundException;
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.Label;
Import Jxl.write.WritableSheet;
Import Jxl.write.WritableWorkbook;
Import jxl.write.WriteException;
Import jxl.write.biff.RowsExceededException;

public class Createtest {
public static void Main (string[] args) throws IOException,
Rowsexceededexception, WriteException {


String[] title = {"Time", "seat", "Dish name", "Price", "Quantity", "Total Price"};

Get time

SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd"); Make the generated daily date a filename (month day)
String Newdata=sdf.format (New Date ()) + ". xml";




Excel path for Output
String Path = "d:\\account\\" +newdata;
Writableworkbook WWB;
Create a JXL file that generates a Test.xls on the D disk
OutputStream OS = new FileOutputStream (path);
WWB = Workbook.createworkbook (OS);

Writablesheet sheet = wwb.createsheet ("Product List", 0);
Label label;
for (int i = 0; i < title.length; i++) {
Label = new label (i, 0, title[i]);
Sheet.addcell (label);

}
Fill time seat dish name price quantity total

/* Define public mode for displaying dates
* such as YYYY-MM-DD hh:mm
*/
SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd hh:mm");
String Newdata=sdf.format (New Date ());
Label=new Label (0,1,newdata);
Sheet.addcell (label);
Seat String
Label=new Label (1, 1, "Table 1th");
Sheet.addcell (label);

Dish name String
Label=new Label (2,1, "coffee");
Sheet.addcell (label);

Price number
Jxl.write.Number number_1=new Jxl.write.Number (3,1,20);
Sheet.addcell (number_1);


Quantity number
Jxl.write.Number number_2=new Jxl.write.Number (4,1,2);
Sheet.addcell (number_2);

Total number
Jxl.write.Number number=new Jxl.write.Number (5,1,40);
Sheet.addcell (number);
Wwb.write ();
Wwb.close ();

}
}

Feel small white can understand, not in detail, welcome the previous guidance article may exist error, O (∩_∩) o Thank you!

Java and Excel (. xls files)---created using JXL to add table files

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.