//設置style ICellStyle cellstyle = workbook.CreateCellStyle(); cellstyle.VerticalAlignment = VerticalAlignment.Center; cellstyle.Alignment = HorizontalAlignment.Center; //合並操作 sheet.AddMergedRegion(new CellRangeAddress(index["firstRow"], index["lastRow"], index["firstCol"], index["lastCol"]));//起始行,結束行,起始列,結束列 //設置合並后style var cell = sheet.GetRow(index["firstRow"]).GetCell(index["firstCol"]); cell.CellStyle = cellstyle;
@GetMapping("/a") public void a(HttpServletResponse response) { // 創建工作簿類 XSSFWorkbook wb = new XSSFWorkbook(); // 創建工作表並設置表名 XSSFSheet sheet = wb.createSheet("訂單"); // 創建行,下標從0開始 XSSFRow row = sheet.createRow(0); // 第四步,創建單元格,並設置值表頭 設置表頭居中 XSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.CENTER); // 創建一個居中格式 //聲明列對象 XSSFCell cell = null; //標題 String[] title = {"姓名", "年齡", "性別", "物品名稱", "數量"}; //創建標題 for (int i = 0; i < title.length; i++) { cell = row.createCell(i); cell.setCellValue(title[i]); cell.setCellStyle(style); } //設置合並后居中顯示樣式 XSSFCellStyle cellstyle = wb.createCellStyle(); cellstyle.setVerticalAlignment(VerticalAlignment.CENTER); cellstyle.setAlignment(HorizontalAlignment.CENTER); int RowCount=1; List<order> orders = getOrders(); for (int i = 0; i < orders.size(); i++) { row = sheet.createRow(RowCount); cell = row.createCell(0); cell.setCellValue(orders.get(i).name); cell.setCellStyle(style); cell = row.createCell(1); cell.setCellValue(orders.get(i).age); cell.setCellStyle(style); cell = row.createCell(2); cell.setCellValue(orders.get(i).sex); cell.setCellStyle(style); for(int j=0;j<orders.get(i).goods.size();j++) { if(j==0) { cell = row.createCell(3); cell.setCellValue(orders.get(i).goods.get(j).goodsName); cell.setCellStyle(style); cell = row.createCell(4); cell.setCellValue(orders.get(i).goods.get(j).quantity); cell.setCellStyle(style); RowCount = RowCount + 1; } else{ row = sheet.createRow(RowCount); cell = row.createCell(3); cell.setCellValue(orders.get(i).goods.get(j).goodsName); cell.setCellStyle(style); cell = row.createCell(4); cell.setCellValue(orders.get(i).goods.get(j).quantity); cell.setCellStyle(style); RowCount = RowCount + 1; } } //合並單元格 if(orders.get(i).goods.size()>1) { for (int colnum = 0; colnum < 3; colnum++) { //參數1:起始行 參數2:終止行 參數3:起始列 參數4:終止列 int firstRow = RowCount - orders.get(i).goods.size(); int lastRow = RowCount - 1; sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, colnum, colnum)); //設置樣式 sheet.getRow(firstRow).getCell(colnum).setCellStyle(cellstyle); } } } //響應到客戶端 try { String fileName = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date()) + ".xlsx"; this.setResponseHeader(response, fileName); OutputStream os = response.getOutputStream(); wb.write(os); os.flush(); os.close(); wb.close(); } catch (Exception e) { e.printStackTrace(); } } /** * * @return */ private List<order> getOrders() { List<order> orderList = new ArrayList<order>(); for (int i = 0; i < 10; i++) { order order = new order(String.valueOf(i), String.valueOf(i), String.valueOf(i)); List<goods> goodsList = new ArrayList<goods>(); if (i == 0 || i == 5 || i == 7) { goodsList.add(new goods("籃球", 1)); goodsList.add(new goods("足球", 1)); goodsList.add(new goods("運動鞋", 2)); order.setGoods(goodsList); } else { goodsList.add(new goods("其它", i)); order.setGoods(goodsList); } orderList.add(order); } return orderList; } //發送響應流方法 public void setResponseHeader(HttpServletResponse response, String fileName) { try { try { fileName = new String(fileName.getBytes("iso8859-1"), "utf-8"); System.out.println(fileName); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } response.setContentType("application/octet-stream;charset=ISO8859-1"); response.setHeader("Content-Disposition", "attachment;filename=" + fileName); response.addHeader("Pargam", "no-cache"); response.addHeader("Cache-Control", "no-cache"); } catch (Exception ex) { ex.printStackTrace(); } }