首先要搞清楚流程,个人理解(错了请不要怪我)就是control-->Service-->dao 还会涉及到实体类
control层
public String Exportable(String beginDate,String endDate,String selforfree,HttpServletResponse resp){
List<Order> export = orderService.export(beginDate,endDate);
new exportExcle().export(export,selforfree, resp);
return "/admin/order/exportData";
}
Service层
List<Order> export(String begin,String end);
Serviceimpl层
public List<Order> export(String begin, String end) {
List<Order> order=orderDao.Export(begin, end);
return order;
}
dao层
List<Order> Export(String begin,String end);
daoImpl层
@Override
public List<Order> Export(String begin, String end) {
String jpql="from Order where createDate between :begin and :end";
List<Order> p = null;
try {
p = (List<Order>) entityManager.createQuery(jpql).setFlushMode(FlushModeType.COMMIT).setParameter("begin", new SimpleDateFormat("yyyy-MM-dd").parse(begin),TemporalType.DATE).setParameter("end", new SimpleDateFormat("yyyy-MM-dd").parse(end),TemporalType.DATE).getResultList();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return p;
}
.setParameter("begin", new SimpleDateFormat("yyyy-MM-dd").parse(begin),TemporalType.DATE) 因为这里是时间类型所以需要转换
com.until 导出文件需要的类
public class exportExcle {
public void export(List<Order> order,String selforfree,HttpServletResponse resp) {
System.out.println("export-Start");
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("订单信息");
//设置每一行的宽度
sheet.setColumnWidth(0, 50 * 100);
sheet.setColumnWidth(1, 45 * 100);
sheet.setColumnWidth(2, 20 * 100);
sheet.setColumnWidth(3, 25 * 100);
sheet.setColumnWidth(4, 120 * 100);
sheet.setColumnWidth(5, 35 * 100);
sheet.setColumnWidth(6, 20 * 100);
sheet.setColumnWidth(7, 20 * 100);
sheet.setColumnWidth(8, 20 * 100);
sheet.setColumnWidth(9, 20 * 100);
sheet.setColumnWidth(10, 20 * 100);
sheet.setColumnWidth(11, 20 * 100);
sheet.setColumnWidth(12, 80 * 100);
sheet.setColumnWidth(13, 80 * 100);
HSSFRow row = sheet.createRow(0);
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
row.createCell(0).setCellValue("订单号");
row.createCell(1).setCellValue("买家");
row.createCell(2).setCellValue("买家id");
row.createCell(3).setCellValue("收件人");
row.createCell(4).setCellValue("收货人地址");
row.createCell(5).setCellValue("收货人联系方式");
row.createCell(6).setCellValue("订单价格");
row.createCell(7).setCellValue("运费");
row.createCell(8).setCellValue("手续费");
for (int i =0; i < order.size(); i++)
{
row = sheet.createRow(i+1);
Order stu = order.get(i);
row.createCell(0).setCellValue(stu.getSn());
row.createCell(1).setCellValue(new Base64().get(stu.getSeller().getUsername()));
row.createCell(2).setCellValue(stu.getBuyer().getId());
row.createCell(3).setCellValue(stu.getConsignee());
row.createCell(4).setCellValue(stu.getAreaName()+stu.getAddress());
row.createCell(5).setCellValue(stu.getPhone());
row.createCell(6).setCellValue(new DecimalFormat("#.00").format(stu.getAmount()));
row.createCell(7).setCellValue(new DecimalFormat("#.00").format(stu.getFreight()));
row.createCell(8).setCellValue(new DecimalFormat("#.00").format(stu.getFee()));
}
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String tablename = null;
if(selforfree.equals("2")){
tablename="会员";
}
else if(selforfree.equals("1")){
tablename="大牌";
}else{
tablename="免单";
}
try
{
String msg = new String(
(tablename+"订单信息_" + format.format(new Date()) + ".xls").getBytes(),
"ISO-8859-1");
OutputStream fout = resp.getOutputStream();
resp.reset();
resp.setContentType("application/vnd.ms-excel");
resp.addHeader("Content-Disposition", "attachment;filename="
+ msg);
wb.write(fout);
fout.flush();
fout.close();
return;
}
catch (Exception e)
{
e.printStackTrace();
}
finally{
System.out.println("export-End");
}
}