java excel根据起止日期导出

来源:互联网 发布:中文翻译越南语软件 编辑:程序博客网 时间:2024/05/18 01:22
这是最好用的
//判断是否为空,如果是空,则显示为指定字串public static String isnull(String param,String value){    String result="";    if(param.equals("")||param.equals("null"))param = value;    return param;}

public Result Export() throws ParseException {    Map<String,String[]> map = request().body().asFormUrlEncoded();    String Date0 = isnull(map.get("date0")[0],"");//开始时间    String date1 = Date0 + " 00:00:00";    String Date1 = isnull(map.get("date1")[0],"");//结束时间    String date2 = Date1 + " 23:59:59";    String[] str0 = new String[]{            "id","开始时间       ","结束时间        "    };    List list0 = Arrays.asList(str0);//行    List<Record> list1 = Ebean.find(Record.class).where().ge("create",date1).lt("create",date2).findList();//根据起止日期获取约车记录列表    String fileName =  new SimpleDateFormat("yyyyMMddHHmmssSS").format(new Date());    String filenames = "约车记录" + fileName + ".xls";    try {        // 声明一个工作薄        HSSFWorkbook wb = new HSSFWorkbook();        //声明一个单子并命名        HSSFSheet sheet = wb.createSheet("约车记录");        //给单子名称一个长度        sheet.setDefaultColumnWidth((short)15);        // 生成一个样式        HSSFCellStyle style = wb.createCellStyle();        //创建第一行(也可以称为表头)        HSSFRow row = sheet.createRow(0);        //样式字体居中        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);        //给表头第一行依次创建单元格        //循环第一行        for (short i = 0; i < list0.size(); i++) {            HSSFCell cell = row.createCell((short) i);            cell.setCellValue((String)list0.get(i));//标题行            cell.setCellStyle(style);        }        //向单元格里填充数据        for (short z = 0; z < list1.size(); z++) {  //            HSSFRow row1 = sheet.createRow(1);            HSSFCell cell = row1.createCell((short) z+1);            Record str =  list1.get(z);            row1 = sheet.createRow((short) (z+3));//创建第i+1行  //此处最好不要修改 容易数据出错            row1.setHeight((short)400);//设置行高            cell = row1.createCell(0);//字段依次排列            cell.setCellValue(isnull(String.valueOf(str.id),""));//订单id            cell = row1.createCell(1);//            cell.setCellValue(isnull(String.valueOf(str.order_id),""));//课时                   }        FileOutputStream out = new FileOutputStream(appEnviroment.rootPath().getPath() + "/conf/excel/"+filenames,true);//导出的路径  这个自己设置        out.flush();        wb.write(out);        out.close();    } catch (FileNotFoundException e) {        JOptionPane.showMessageDialog(null, "导出失败!");        e.printStackTrace();    } catch (IOException e) {        JOptionPane.showMessageDialog(null, "导出失败!");        e.printStackTrace();    }    return appok(Json.toJson(filenames),"导出成功,请下载!");}
1 0
原创粉丝点击