private void exportExcel(String fileName, List<Customer> customers) {
WritableWorkbook wwb; FileOutputStream fos;
try {
String savePath = ConfigUtil.pro.get("excelPath").toString(); //配置文件配置路径
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
String newDate = dateFormat.format(date);
File f = new File(savePath); //选择保存服务器路径
f = new File(f, newDate+".xls"); //该路径下放的文件EXCEL
//f.createNewFile();//文件夹不存在创建
fos = new FileOutputStream(f); wwb = Workbook.createWorkbook(fos);
WritableSheet ws = wwb.createSheet("会员列表", 0); // 创建一个工作表
// 设置单元格的文字格式
WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
wcf.setAlignment(Alignment.CENTRE);
ws.setRowView(1, 500);
jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#,##0.00");
// 设置数字格式
jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf);
// 设置格式 // 设置标题行
ws.addCell(new Label(0, 0, "姓名", wcf));
ws.addCell(new Label(1, 0, "手机号", wcf));
ws.addCell(new Label(2, 0, "账户余额", wcf));
int rowsCount = customers.size();
// 填充数据的内容 Customer customer;
for (int i = 0; i < rowsCount; i++) { customer = customers.get(i);
WritableCellFeatures x; ws.addCell(new Label(0, i + 1, customer.getName(), wcf)); ws.addCell(new Label(1, i + 1, customer.getMobile(), wcf)); double orderCost = customer.getRemainmoney() == null ? 0.00 : customer.getRemainmoney().doubleValue(); jxl.write.Number labelOrderCost = new jxl.write.Number(2, i + 1, orderCost, wcfN); // 格式化数值 ws.addCell(labelOrderCost); } wwb.write(); wwb.close(); FileInputStream fileInputStream = new FileInputStream(new File(fileName)); Date dt = new Date();// 如果不需要格式,可直接用dt,dt就是当前系统时间 DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");// 设置显示格式 String nowTime = ""; nowTime = df.format(dt);// 用DateFormat的format()方法在dt中获取并以yyyy/MM/dd fileName = "用户信息" + nowTime + ".xls"; String finalFileName = null; // 保存EXCEL内容 CownExcel ce = new CownExcel(); ce.setDownloaddate(new Date()); ce.setUrl(f.getPath()); customerService.insertSelective(ce);
} catch (IOException e) { } catch (RowsExceededException e) { } catch (WriteException e) { } }