@Override
public boolean exportOrders(List<Order> orders, HttpServletResponse response) {
try {
ArrayList<String[]> contentsArrayList = new ArrayList<String[]>();
String[] titles = new String[14];
titles[0] = "订单日期";
titles[1] = "订单支付日期";
titles[2] = "订单编号";
titles[3] = "产品名称";
titles[4] = "产品类型";
titles[5] = "单价";
titles[6] = "购买数量";
titles[7] = "总金额";
titles[8] = "付款状态";
titles[9] = "收件人名称";
titles[10] = "收件人电话";
titles[11] = "配送地址";
titles[12] = "配送日期";
titles[13] = "发票信息";
contentsArrayList.add(titles);
for (int i = 0; i < orders.size(); i++) {
List<BuyList> butlists = orders.get(i).getBuyList();
for (BuyList buyList : butlists) {
String[] content = new String[14];
content[0] = orders.get(i).getCreateDate();
content[1] = orders.get(i).getPayDate();
content[2] = orders.get(i).getId();
content[3] = buyList.getProductName();
content[4] = buyList.getProductTypeStr();
if(buyList.getProductPrice()!=null){
content[5]=buyList.getProductPrice().toString();
}
content[6] = String.valueOf(buyList.getProductCount());
content[7] = orders.get(i).getTotalMoney();
content[8] = orders.get(i).getPayStatus() == Order.PAY_PAIY ? "已付款"
: "未付款";
content[9] = orders.get(i).getName();
content[10] = orders.get(i).getMobile();
content[11] = orders.get(i).getAddress();
OrderStatus orderStatus=orders.get(i).getOrderStatus();
if(orderStatus!=null){
HistoryStatus historyStatus=orderStatus.getCurrent();
if(historyStatus!=null){
if(historyStatus.getStatus()==OrderStatus.STATUS_SEND_OUT){
content[12] =historyStatus.getDatetime();
}
}
}
content[13] = orders.get(i).isHasInvoice() == true ? "是"
: "否";
contentsArrayList.add(content);
}
}
String[][] contents = ExcelUtils.changeToArray(contentsArrayList);
String fileName = ExcelUtils.writeExcel(contents, "订单",response);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static String writeExcel(String[][] titles, String title, HttpServletResponse response) throws IOException {
String fileName =title
+ Calendar.getInstance().getTimeInMillis();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName + ".xls").getBytes(), "iso-8859-1"));
//根据传进来的file对象创建可写入的Excel工作薄
OutputStream os = response.getOutputStream();
WritableWorkbook wwb = null;
try {
// 首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象
wwb = Workbook.createWorkbook(os);
} catch (IOException e) {
e.printStackTrace();
}
if (wwb != null) {
// 创建一个可写入的工作表
// Workbook的createSheet方法有两个参数,第一个是工作表的名称,第二个是工作表在工作薄中的位置
WritableSheet ws = wwb.createSheet(title, 1);
// 下面开始添加单元格
for (int row = 0; row < titles.length; row++) {
// System.out.println(row);
for (int j = 0; j < titles[row].length; j++) {
// 这里需要注意的是,在Excel中,第一个参数表示列,第二个表示行
Label labelC = new Label(j, row, titles[row][j]);
try {
// 将生成的单元格添加到工作表中
ws.addCell(labelC);
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
}
try {
// 从内存中写入文件中
wwb.write();
// 关闭资源,释放内存
wwb.close();
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
return fileName;
}
public static String[][] changeToArray(ArrayList<String[]> contentsArrayList) {
String[][] contents = new String[contentsArrayList.size()][];
for (int i = 0; i < contentsArrayList.size(); i++) {
contents[i] = contentsArrayList.get(i);
}
return contents;
} |