importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.OutputStream;importjava.text.SimpleDateFormat;importjava.util.Date;importjxl.Workbook;importjxl.write.DateFormats;importjxl.write.DateTime;importjxl.write.Label;importjxl.write.WritableCellFormat;importjxl.write.WritableSheet;importjxl.write.WritableWorkbook;importjxl.write.WriteException;public classSimpleExcelDemo {private static int column = 0;private static int row = 0;//创建工作薄
public static void createExcelFile(String demand_id) throwsWriteException,IOException{//创建文件目录
String realPath = "C:\\excel\\";
File fileDir= newFile(realPath);if(!fileDir.exists()){
fileDir.mkdirs();
}
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
String date= sdf.format(newDate(System.currentTimeMillis()));
String fileName= date + ".xls";
String filePath= realPath +fileName;
File file= newFile(filePath);
WritableCellFormat wf= newWritableCellFormat(DateFormats.FORMAT1);
SimpleDateFormat sdf1= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String current= sdf1.format(newDate(System.currentTimeMillis()));//创建一个文件输出流,用于写出表格到本地文件夹
OutputStream out = null;
WritableWorkbook wwb= null;
WritableSheet sheet= null;
Label cell;try{if(!file.exists()){
out= newFileOutputStream(file);//创建excel文件(创建一个可读写入的工作薄)
wwb =Workbook.createWorkbook(out);//创建新的一页
sheet = wwb.createSheet("已处理", 0);//创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行坐标,第三个参数为内容
cell = new Label(column, row, "需求ID");
sheet.addCell(cell);
cell= new Label(column + 1, row, "上传时间");
sheet.addCell(cell);
cell= new Label(column, ++row, demand_id);
sheet.addCell(cell);
Date d= newDate();
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("已处理");
cell= new Label(column, ++row, demand_id);
sheet.addCell(cell);
Date d= newDate();
d=sdf1.parse(current);
DateTime dt= new DateTime(column + 1, row, d, wf);
sheet.addCell(dt);
}
}
}
}catch(Exception e) {
e.printStackTrace();
}
wwb.write();//写入Excel工作表
wwb.close(); //关闭Excel工作表,同时也会关闭IO流。
}public static void main(String[] args) throwsException {
createExcelFile("1234");
System.out.println(column+ "," +row);//创建文件目录
File fileDir = new File("C:\\excel");if(!fileDir.exists()){
fileDir.mkdirs();
}
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
String date= sdf.format(newDate(System.currentTimeMillis()));
String fileName= date + ".xls";
String filePath= "C:\\excel\\" +fileName;
File file= newFile(filePath);//创建一个文件输出流,用于写出表格到本地文件夹
OutputStream out = newFileOutputStream(file);//创建excel文件(创建一个可读写入的工作薄)
WritableWorkbook wwb =Workbook.createWorkbook(out);//创建新的一页
WritableSheet sheet = wwb.createSheet("已处理", 0);//创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行坐标,第三个参数为内容
Label cell = new Label(0,0,"需求ID");
sheet.addCell(cell);
cell= new Label(1,0,"上传时间");
sheet.addCell(cell);
cell= new Label(0, 1, "5055");
sheet.addCell(cell);
WritableCellFormat wf= newWritableCellFormat(DateFormats.FORMAT1);
SimpleDateFormat sdf1= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String current= sdf1.format(newDate(System.currentTimeMillis()));
Date d=sdf1.parse(current);
DateTime birthday= new DateTime(1, 1, d, wf);
sheet.addCell(birthday);
wwb.write();//写入Excel工作表
wwb.close(); //关闭Excel工作表,同时也会关闭IO流。
}
}