package com.zzkx.udp.framework.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.zzkx.business.domain.BusinessOrderInfoDo;
import com.zzkx.business.entity.BusinessOrderInfoPay;
import com.zzkx.business.entity.BussinessOrderItem;
public class OrderExcelUtil {
@SuppressWarnings("deprecation")
public static void export(List<BusinessOrderInfoDo> list, String[] headers,String fileName, HttpServletResponse response) throws IOException {
//参数列表:list导出的集合
// headers: 表头行
//fileName:文件名
//response: 响应
// 创建excel
@SuppressWarnings("resource")
HSSFWorkbook wb = new HSSFWorkbook();
// 创建sheet
HSSFSheet sheet = wb.createSheet(fileName);
// 创建表头行
HSSFRow rowTitle = sheet.createRow(0);
// 创建表头行样式
HSSFCellStyle styleTitle = wb.createCellStyle();
styleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 居中
HSSFFont fontTitle = wb.createFont();
// 宋体加粗
fontTitle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
fontTitle.setFontName("宋体");
fontTitle.setFontHeight((short) 200);
styleTitle.setFont(fontTitle);
for (int i = 0; i < headers.length; i++) {
// 在表头行上创建1列
HSSFCell cellTitle = rowTitle.createCell(i);
// 列标题及样式
cellTitle.setCellValue(headers[i]);
cellTitle.setCellStyle(styleTitle);
}
// 设置文本行的每一列的样式
HSSFCellStyle normalStyle = wb.createCellStyle();
normalStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 居中
//日期格式
HSSFCellStyle dateStyle = wb.createCellStyle();
HSSFDataFormat format = wb.createDataFormat();
//日期格式 年月日 时分秒
dateStyle.setDataFormat(format.getFormat("yyyy-m-d h:mm:ss"));
//数字格式
HSSFCellStyle numberStyle = wb.createCellStyle();
numberStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0"));
HSSFCell cell = null;
for (int i = 0; i < list.size(); i++) {
BusinessOrderInfoDo item = list.get(i);
HSSFRow row = sheet.createRow(i + 1);
//订单id
cell = row.createCell(0);
cell.setCellValue(item.getId());
cell.setCellStyle(numberStyle);
//订单商品ID
cell = row.createCell(1);
BussinessOrderItem bussinessOrderItem = item.getBussinessOrderItem();
if(bussinessOrderItem!=null && bussinessOrderItem.getWareId()!=null){
cell.setCellValue( bussinessOrderItem.getWareId());
}
cell.setCellStyle(numberStyle);
//订单总价
Integer totalPrice = 0;
BusinessOrderInfoPay pay = item.getBusinessOrderInfoPay();
if(pay!=null){
if(pay.getAmountAll() != null){
totalPrice = pay.getAmountAll();
}
}
cell = row.createCell(2);
cell.setCellValue(totalPrice);
cell.setCellStyle(numberStyle);
//订单日期
cell = row.createCell(3);
cell.setCellValue(item.getCreateTime());
cell.setCellStyle(dateStyle);
}
sheet.setColumnWidth(0, 20 * 256);
sheet.setColumnWidth(1, 20 * 256);
//设置第4列单元格长度
sheet.setColumnWidth(3, 20 * 256);
// 不弹出保存框方式
/*
* FileOutputStream fout = new FileOutputStream("e:/numberQuery1.xls");
* wb.write(fout); fout.close(); wb.close();
* System.out.println("导出完成!");
*/
ByteArrayOutputStream os = new ByteArrayOutputStream();
wb.write(os);
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
// 设置response参数,可以打开下载页面
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition",
"attachment;filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
ServletOutputStream out = response.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
// Simple read/write loop.
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
}
}