搜档网
当前位置:搜档网 › java导出excel合并单元格

java导出excel合并单元格

@RequestMapping("exportWells")
public View exportWells(Model model, HttpServletRequest request) {
View view = new AbstractExcelView() {

@Override
protected void buildExcelDocument(Map model,
HSSFWorkbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception {

// 设置response方式,使执行此controller时候自动出现下载页面,而非直接使用excel打开
response.reset();
response.setContentType("application/vnd.ms-excel");
String filename = getFileName() + ".xls";
// 注意,如果去掉下面一行代码中的attachment; 那么也会使IE自动打开文件。
response.setHeader("Content-disposition",
"attachment;filename=" + filename);

String boardId_aod = request.getParameter("boardId_aod");
// 建立 Sheet
HSSFSheet sheet = workbook.createSheet("sheet1");

int rowNum = 0;
int idx = 0;

// 建立标题
HSSFRow header = sheet.createRow(rowNum++);
HSSFCellStyle cellStyle = workbook.createCellStyle();
HSSFCellStyle centerCellStyle = workbook.createCellStyle();
centerCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
centerCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
centerCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
centerCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
centerCellStyle
.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐
cellStyle.setWrapText(true);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);

HSSFCellStyle boldCellStyle = workbook.createCellStyle();

boldCellStyle.setWrapText(true);
boldCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
boldCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
boldCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
boldCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
boldCellStyle.setFont(font);
HSSFCell cell = header.createCell(idx++);
cell.setCellValue("纯化板号");
cell.setCellStyle(cellStyle);

cell = header.createCell(idx++);
cell.setCellValue("订单编号");
cell.setCellStyle(cellStyle);

cell = header.createCell(idx++);
cell.setCellValue("孔位");
cell.setCellStyle(cellStyle);

cell = header.createCell(idx++);
cell.setCellValue("引物流水号");
cell.setCellStyle(cellStyle);

cell = header.createCell(idx++);
cell.setCellValue("实测OD");
cell.setCellStyle(cellStyle);

cell = header.createCell(idx++);

cell.setCellValue("ul/1OD");
// 加粗
cell.setCellStyle(boldCellStyle);

cell = header.createCel

l(idx++);
cell.setCellValue("总量");
cell.setCellStyle(cellStyle);

cell = header.createCell(idx++);
cell.setCellValue("分装");
cell.setCellStyle(cellStyle);

cell = header.createCell(idx++);
cell.setCellValue("用户孔位");
cell.setCellStyle(cellStyle);

cell = header.createCell(idx++);
cell.setCellValue("跟单标志");
cell.setCellStyle(cellStyle);

cell = header.createCell(idx++);
cell.setCellValue("打回次数");
cell.setCellStyle(cellStyle);

sheet.setColumnWidth(0, 3066);
sheet.setColumnWidth(1, 3966);
sheet.setColumnWidth(2, 1200);
sheet.setColumnWidth(3, 3766);
sheet.setColumnWidth(4, 1266);
sheet.setColumnWidth(5, 1266);
sheet.setColumnWidth(6, 1266);
sheet.setColumnWidth(7, 1266);
sheet.setColumnWidth(8, 1266);
sheet.setColumnWidth(9, 1266);
sheet.setColumnWidth(10, 1266);

// 输出内容
List wells = crudeOdService
.findWellsByBoardId(boardId_aod);

String orderNumber = "";
int mergecount = 0;
int firstMergeRow = 1;
boolean canMerge = false;
HSSFRow row;
for (Well p : wells) {
idx = 0;
row = sheet.createRow(rowNum++);
cell = row.createCell(idx++);
cell.setCellValue(p.getBoardNumber());
cell.setCellStyle(cellStyle);
cell = row.createCell(idx++);
cell.setCellValue(p.getOrderNumber());
if (orderNumber.equals(p.getOrderNumber())) {
mergecount++;
if (rowNum == wells.size() + 1 && canMerge == false) {
sheet.addMergedRegion(new Region(1, (short) 1,
mergecount + 1, (short) 1));
}
if (rowNum == wells.size() + 1 && canMerge == true) {
sheet.addMergedRegion(new Region(rowNum - 1
- mergecount, (short) 1, rowNum - 1,
(short) 1));
}
} else {
if (rowNum != wells.size() + 1) {
orderNumber = p.getOrderNumber();
if (rowNum != 2) {
canMerge = true;
sheet.addMergedRegion(new Region(firstMergeRow,
(short) 1, firstMergeRow + mergecount,
(short) 1));
}
mergecount = 0;
firstMergeRow = rowNum - 1;
}
}
cell.setCellStyle(centerCellStyle);
cell = row.createCell(idx++);
cell.setCellValue(p.getWells());
cell.setCellStyle(cellStyle);
cell = row.createCell(idx++);
cell.setCellValue(p.getPrimerNo());
cell.setCellStyle(cellStyle);
cell = row.createCell(idx++);
cell.setCellValue(p.getOdValue());
cell.setCellStyle(cellStyle);

// 加粗
cell = row.createCell(idx++);
cell.setCellValue(p.getUL10D());
cell.setCellStyle(boldCellStyle);
cell = row.createCell(idx++);
cell.setCellValue(p.getTotal());
cell.setCellStyle(cellStyle);
cell = row.createCell(idx++);
cell.setCellValue(p.getSplitN());
cell.setCellStyle(cellStyle);
cell = row

.createCell(idx++);
cell.setCellValue(p.getCustomerWell());
cell.setCellStyle(cellStyle);
cell = row.createCell(idx++);
cell.setCellValue(p.getFollowOrderFlag());
cell.setCellStyle(cellStyle);
cell = row.createCell(idx++);
cell.setCellValue(p.getPushBackCount());
cell.setCellStyle(cellStyle);
}

}
};
return view;
}

相关主题