java excel匯出(表頭合併,多行表頭)
@RequestMapping(value="orderExcelList2")
public void orderExcelList2forJava(Order order,HttpServletResponse response){
Map<String,Object> map = toOrderExcelJSONList2(order);
List<Bottle> bottleList = (List<Bottle>) map.get("bottleList");
List<Order> orderList = (List<Order>) map.get("orderList");
List<Order> backOrderList = (List<Order>) map.get("backOrderList");
HSSFWorkbook book = new HSSFWorkbook();
HSSFSheet sheet = book.createSheet();
String[] head0 = new String[]{"訂單號","收貨人","電話","固話","地址","下單時間","完成時間","所屬店",
"出貨品牌數/18.9L","出貨品牌數/18.9L","出貨品牌數/18.9L","出貨品牌數/18.9L","出貨品牌數/18.9L",
"出貨品牌數/18.9L","出貨品牌數/18.9L","出貨品牌數/18.9L","出貨品牌數/18.9L","出貨品牌數/18.9L",
"價格","型別","協議號",
"付款","付款","付款","付款","付款","送氣工","送氣工","租金","使用者型別","優惠券","紅包","紅包","紅包","備註"};
String[] headnum0 = new String[] { "1,3,0,0", "1,3,1,1", "1,3,2,2","1,3,3,3","1,3,4,4","1,3,5,5","1,3,6,6","1,3,7,7",
"1,1,8,17", "1,3,18,18", "1,3,19,19","1,3,20,20","1,1,21,25","1,1,26,27","1,3,28,28","1,3,29,29","1,3,30,30","1,1,31,33","1,3,34,34" }; //對應excel中的行和列,下表從0開始{"開始行,結束行,開始列,結束列"}
String[] head1 = new String[]{"10KG/瓶數","單價","15KG/瓶數","單價","2KG/瓶數","單價","5KG/瓶數","單價","50KG/瓶數","單價"};
String[] headnum1 = new String[] { "2,3,8,8", "2,3,9,9", "2,3,10,10","2,3,11,11","2,3,12,12","2,3,13,13","2,3,14,14","2,3,15,15","2,3,16,16","2,3,17,17"};
String[] head1_2 = new String[]{"收現","收現","收現","收現","欠款","姓名","樓層費"};
String[] headnum1_2 = new String[] { "2,2,21,24","2,2,25,25","2,3,26,26","2,3,27,27"};
String[] head1_3 = new String[]{"微支付","現金","押金","其它","賬期"};
String[] headnum1_3 = new String[] { "3,3,21,21","3,3,22,22","3,3,23,23","3,3,24,24","3,3,25,25"};
String[] head1_4 = new String[]{"金額","接收人","狀態"};
String[] headnum1_4 = new String[] { "2,3,31,31","2,3,32,32","2,3,33,33"};
// 表頭標題樣式
HSSFFont headfont = book.createFont();
headfont.setFontName("宋體");
headfont.setFontHeightInPoints((short) 22);// 字型大小
HSSFCellStyle headstyle = book.createCellStyle();
headstyle.setFont(headfont);
headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
headstyle.setLocked(true);
// 表頭標題樣式2
HSSFFont headfont2 = book.createFont();
headfont.setFontName("宋體");
headfont.setFontHeightInPoints((short) 15);// 字型大小
HSSFCellStyle headstyle2 = book.createCellStyle();
headstyle2.setFont(headfont2);
headstyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
headstyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
headstyle2.setLocked(true);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, head0.length-1));
HSSFRow row = sheet.createRow(0); //第一行
row.setHeight((short) 0x200);
HSSFCell cell = row.createCell(0);
cell.setCellValue("訂單詳情");
cell.setCellStyle(headstyle);
row = sheet.createRow(1); //第二行
for(int i =0;i < head0.length;i++){
cell = row.createCell(i);
cell.setCellStyle(headstyle2);
cell.setCellValue(head0[i]);
}
row = sheet.createRow(2); //第三行
for (int i = 0; i < head0.length; i++) {
if (i > 1 && i < 18) {
for (int j = 0; j < head1.length; j++) {
cell = row.createCell(j + 8); //在8起步所以+8
cell.setCellValue(head1[j]);
cell.setCellStyle(headstyle2);
}
}
if(i > 21 && i < 27){ //大於起步,小於止步
for (int j = 0; j < head1_2.length; j++) {
cell = row.createCell(j + 21); ///在22起步所以+21
cell.setCellValue(head1_2[j]);
cell.setCellStyle(headstyle2);
}
}
if(i > 28 && i < 34){ //大於起步,小於止步
for (int j = 0; j < head1_4.length; j++) {
cell = row.createCell(j + 31);
cell.setCellValue(head1_4[j]);
cell.setCellStyle(headstyle2);
}
}
}
row = sheet.createRow(3); //第四行
for (int i = 0; i < head0.length; i++) {
if(i > 21 && i < 25){ //大於起步,小於止步
for (int j = 0; j < head1_3.length; j++) {
cell = row.createCell(j + 21); ///在21起步所以+21
cell.setCellValue(head1_3[j]);
cell.setCellStyle(headstyle2);
}
}
}
//動態合併單元格
for (int i = 0; i < headnum0.length; i++) {
String[] temp = headnum0[i].split(",");
Integer startrow = Integer.parseInt(temp[0]);
Integer overrow = Integer.parseInt(temp[1]);
Integer startcol = Integer.parseInt(temp[2]);
Integer overcol = Integer.parseInt(temp[3]);
sheet.addMergedRegion(new CellRangeAddress(startrow, overrow,
startcol, overcol));
}
//動態合併單元格
for (int i = 0; i < headnum1.length; i++) {
String[] temp = headnum1[i].split(",");
Integer startrow = Integer.parseInt(temp[0]);
Integer overrow = Integer.parseInt(temp[1]);
Integer startcol = Integer.parseInt(temp[2]);
Integer overcol = Integer.parseInt(temp[3]);
sheet.addMergedRegion(new CellRangeAddress(startrow, overrow,
startcol, overcol));
}
//動態合併單元格
for (int i = 0; i < headnum1_2.length; i++) {
String[] temp = headnum1_2[i].split(",");
Integer startrow = Integer.parseInt(temp[0]);
Integer overrow = Integer.parseInt(temp[1]);
Integer startcol = Integer.parseInt(temp[2]);
Integer overcol = Integer.parseInt(temp[3]);
sheet.addMergedRegion(new CellRangeAddress(startrow, overrow,
startcol, overcol));
}
//動態合併單元格
for (int i = 0; i < headnum1_3.length; i++) {
String[] temp = headnum1_3[i].split(",");
Integer startrow = Integer.parseInt(temp[0]);
Integer overrow = Integer.parseInt(temp[1]);
Integer startcol = Integer.parseInt(temp[2]);
Integer overcol = Integer.parseInt(temp[3]);
sheet.addMergedRegion(new CellRangeAddress(startrow, overrow,
startcol, overcol));
}
//動態合併單元格
for (int i = 0; i < headnum1_4.length; i++) {
String[] temp = headnum1_4[i].split(",");
Integer startrow = Integer.parseInt(temp[0]);
Integer overrow = Integer.parseInt(temp[1]);
Integer startcol = Integer.parseInt(temp[2]);
Integer overcol = Integer.parseInt(temp[3]);
sheet.addMergedRegion(new CellRangeAddress(startrow, overrow,
startcol, overcol));
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int count = 4; //從第五行開始綁資料
for (Order order1 : orderList) {
row = sheet.createRow(count);
row.createCell(0).setCellValue(order1.getOrderNo());
row.createCell(1).setCellValue(order1.getName());
row.createCell(2).setCellValue(order1.getPhone());
row.createCell(3).setCellValue(order1.getFixedline());
row.createCell(4).setCellValue(order1.getAddress());
if(order1.getCreateTime()!=null)
row.createCell(5).setCellValue(sdf.format(order1.getCreateTime()));
if(order1.getEndTime()!=null)
row.createCell(6).setCellValue(sdf.format(order1.getEndTime()));
row.createCell(7).setCellValue(order1.getStoreName());
List<OrderGas> gasOrderList = order1.getGasOrderList();
int j = 8;
for(int i=0;i<gasOrderList.size();i++){
if (gasOrderList.get(i).getNumber() != 0) {
row.createCell(j).setCellValue(gasOrderList.get(i).getNumber());
}
if (gasOrderList.get(i).getPrice() != 0) {
// double d = gasOrderList.get(i).getPrice() * 0.01;
// int te = Integer.parseInt(String.valueOf(d));
row.createCell(j+1).setCellValue(gasOrderList.get(i).getPrice() * 0.01 + "元/瓶");
}
j+=2;
}
row.createCell(7).setCellValue(order1.getSpec());
row.createCell(8).setCellValue(order1.getStoreName());
row.createCell(9).setCellValue(order1.getStoreName());
row.createCell(10).setCellValue(order1.getStoreName());
row.createCell(11).setCellValue(order1.getStoreName());
row.createCell(12).setCellValue(order1.getStoreName());
row.createCell(13).setCellValue(order1.getStoreName());
row.createCell(14).setCellValue(order1.getStoreName());
row.createCell(15).setCellValue(order1.getStoreName());
row.createCell(16).setCellValue(order1.getStoreName());
row.createCell(17).setCellValue(order1.getStoreName());
row.createCell(18).setCellValue(order1.getStoreName());
row.createCell(19).setCellValue(order1.getStoreName());
row.createCell(20).setCellValue(order1.getStoreName());
row.createCell(21).setCellValue(order1.getStoreName());
row.createCell(22).setCellValue(order1.getStoreName());
row.createCell(23).setCellValue(order1.getStoreName());
count++;
}
private static void out(HSSFWorkbook book, HttpServletResponse response) {
try {
response.addHeader("Content-Disposition", "attachment;filename=" + new String((new Date().getTime() +".xls").getBytes()));
response.setContentType("application/octet-stream");
book.write(response.getOutputStream());
} catch (Exception e) {
logger.error("匯出出錯", e);
}
}