Poi 地址:https://blog.csdn.net/qq_29870555/article/details/80493612
public void exportFive() {
HSSFWorkbook workbook = new HSSFWorkbook();
/** ***************** 样式定义开始 ****************** */
// 定义全局文本样式,也可为某行某列单独定义
HSSFCellStyle style = workbook.createCellStyle();
HSSFCellStyle style2 = workbook.createCellStyle();
HSSFCellStyle style3 = workbook.createCellStyle();
HSSFCellStyle style_title = workbook.createCellStyle();
// style.setBorderTop(HSSFCellStyle.BORDER_THIN);
HSSFFont font_title = workbook.createFont();
font_title.setFontName("方正小标宋_GBK");// 设置字体
font_title.setFontHeightInPoints((short) 24);// 设置字体大小
style_title.setFont(font_title);
style_title.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 设置垂直居中
style_title.setWrapText(true);
style_title.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 设置文本居中
// 字体设置
HSSFFont font = workbook.createFont();
font.setFontName("宋体");// 设置字体
font.setFontHeightInPoints((short) 18);// 设置字体大小
/*font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);*/ // 设置字体加粗
style.setFont(font);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 设置垂直居中
style.setWrapText(true);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 设置文本居中
style.setBorderBottom((short) 1);
style.setBorderLeft((short) 1);
style.setBorderTop((short) 1);
style.setBorderRight((short) 1);
// 字体设置
HSSFFont font2 = workbook.createFont();
font2.setFontName("宋体");// 设置字体
font2.setFontHeightInPoints((short) 16);// 设置字体大小
style2.setFont(font2);
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 设置垂直居中
style2.setWrapText(true);
style2.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 设置文本居左
style2.setBorderBottom((short) 1);
style2.setBorderLeft((short) 1);
style2.setBorderTop((short) 1);
style2.setBorderRight((short) 1);
HSSFFont font3 = workbook.createFont();
font3.setFontName("宋体");// 设置字体
font3.setFontHeightInPoints((short) 16);// 设置字体大小
style3.setFont(font3);
style3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 设置垂直居中
style3.setWrapText(true);
style3.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 设置文本居中
style3.setBorderBottom((short) 1);
style3.setBorderLeft((short) 1);
style3.setBorderTop((short) 1);
style3.setBorderRight((short) 1);
/** ***************** 样式定义结束 ****************** */
HSSFSheet sheet = workbook.createSheet();// 建工作表
workbook.setSheetName(0, "锡通园“五个年”活动任务分解表");// 设置表名
sheet.setColumnWidth(0, 18 * 300);
sheet.setColumnWidth(1, 18 * 200);
sheet.setColumnWidth(2, 18 * 1300);
sheet.setColumnWidth(3, 18 * 350);
sheet.setColumnWidth(4, 18 * 700);
// 添加标题行
HSSFRow row_title = sheet.createRow(0);
row_title.setHeight((short) (64 * 20));
HSSFCell cell_title = row_title.createCell((short) 0);
cell_title.setCellType(HSSFCell.CELL_TYPE_STRING);
cell_title.setCellValue("锡通园“五个年”活动任务分解表");
cell_title.setCellStyle(style_title);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 4)); // 合并单元格显示
// 统计数据展现
String column = "";
column = "活动内容,序号,具体项目,责任部门,推进情况";
String[] columnArr = column.split(",");
HSSFRow row_column = sheet.createRow(1);
row_column.setHeight((short) (30 * 20));
for (int i = 0; i < columnArr.length; i++) {
HSSFCell cell_column = row_column.createCell((short) i);
cell_column.setCellType(HSSFCell.CELL_TYPE_STRING);
cell_column.setCellValue(columnArr[i]);
cell_column.setCellStyle(style);
}
// 序号
int total = 1;
// 行号
int total_row = 1;
// 单元格合并起始行(部门)
int start_row = 2;
// 单元格合并起始行(活动)
int start_row_act = 2;
String[] s = {"重大项目突破年","生态建设攻坚年","民生工程提质年","党建工作创新年","作风建设提效年"};
for(int p = 0;p < s.length;p++ ) {
String activity = s[p];
List<DepVo> deps = gzapService.getDepList(activity);
if(deps.size() == 0) {
continue;
}
for(int k = 0; k < deps.size();k++) {
List<FiveYearGzap> list = gzapService.getListByDepAndActivity(activity,deps.get(k).getDepName());
if(list.size() == 0) {
continue;
}
for (int i = 0; i < list.size(); i++) {
FiveYearGzap f = list.get(i);
String project = f.getProject();
String pushState = f.getPushState();
String[] cell = new String[5];
cell[0] = "";
cell[1] = String.valueOf(total);
cell[2] = project;
cell[3] = "";
cell[4] = pushState;
HSSFRow row_column1 = sheet.createRow(total_row+1);
row_column1.setHeight((short) (40 * 20));
for (int o = 0; o < cell.length; o++) {
HSSFCell cell_column = row_column1.createCell((short) o);
cell_column.setCellType(HSSFCell.CELL_TYPE_STRING);
cell_column.setCellValue(cell[o]);
if(o == 2 || o == 4) {
cell_column.setCellStyle(style2);
}else {
cell_column.setCellStyle(style3);
}
}
total++;
total_row++;
}
//设置部门单元格
sheet.addMergedRegion(new CellRangeAddress(start_row, total_row, 3, 3));
HSSFRow row = sheet.getRow(start_row);
HSSFCell cell_activity = row.getCell(3);// 获得第三行的第一个单元格
cell_activity.setCellType(HSSFCell.CELL_TYPE_STRING);
cell_activity.setCellValue(deps.get(k).getDepName());
cell_activity.setCellStyle(style);
start_row = total_row+1;
}
//设置最左边活动内容。
sheet.addMergedRegion(new CellRangeAddress(start_row_act, total_row, 0, 0));
HSSFRow row2 = sheet.getRow(start_row_act);
HSSFCell cell_activity = row2.getCell(0);// 获得第三行的第一个单元格
cell_activity.setCellType(HSSFCell.CELL_TYPE_STRING);
cell_activity.setCellValue(activity);
cell_activity.setCellStyle(style);
start_row_act = total_row+1;
}
HttpServletResponse response = getResponse();
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel;charset=utf-8");
try {
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode( "锡通园“五个年”活动任务分解表.xls", "UTF-8"));
workbook.write(response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}