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