POI合并单元格,赋值
public void exportFive() {
HSSFWorkbook workbook = new HSSFWorkbook();
/** ***************** 样式定义开始 ****************** */
// 定义全局⽂本样式,也可为某⾏某列单独定义
HSSFCellStyle style = ateCellStyle();
HSSFCellStyle style2 = ateCellStyle();
HSSFCellStyle style3 = ateCellStyle();
HSSFCellStyle style_title = ateCellStyle();
// style.setBorderTop(HSSFCellStyle.BORDER_THIN);
HSSFFont font_title = ateFont();
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 = ateFont();
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 = ateFont();
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 = ateFont();
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 = ateSheet();// 建⼯作表
workbook.setSheetName(0, "锡通园“五个年”活动任务分解表");// 设置表名
sheet.setColumnWidth(0, 18 * 300);
sheet.setColumnWidth(1, 18 * 200);
sheet.setColumnWidth(2, 18 * 1300);
htmlbordersheet.setColumnWidth(3, 18 * 350);
sheet.setColumnWidth(4, 18 * 700);
// 添加标题⾏
HSSFRow row_title = ateRow(0);
row_title.setHeight((short) (64 * 20));
HSSFCell cell_title = ateCell((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 = ateRow(1);
row_column.setHeight((short) (30 * 20));
for (int i = 0; i < columnArr.length; i++) {
HSSFCell cell_column = ateCell((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 = DepList(activity);
if(deps.size() == 0) {
continue;
}
for(int k = 0; k < deps.size();k++) {
List<FiveYearGzap> list = ListByDepAndActivity((k).getDepName()); if(list.size() == 0) {
continue;
}
for (int i = 0; i < list.size(); i++) {
FiveYearGzap f = (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 = ateRow(total_row+1);
row_column1.setHeight((short) (40 * 20));
for (int o = 0; o < cell.length; o++) {
HSSFCell cell_column = ateCell((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);
cell_column.setCellStyle(style3);
}
}
total++;
total_row++;
}
//设置部门单元格
sheet.addMergedRegion(new CellRangeAddress(start_row, total_row, 3, 3));
HSSFRow row = Row(start_row);
HSSFCell cell_activity = Cell(3);// 获得第三⾏的第⼀个单元格
cell_activity.setCellType(HSSFCell.CELL_TYPE_STRING);
cell_activity.(k).getDepName());
cell_activity.setCellStyle(style);
start_row = total_row+1;
}
//设置最左边活动内容。
sheet.addMergedRegion(new CellRangeAddress(start_row_act, total_row, 0, 0));
HSSFRow row2 = Row(start_row_act);
HSSFCell cell_activity = Cell(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.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel;charset=utf-8");
try {
response.setHeader("Content-Disposition", "attachment;filename=" + de( "锡通园“五个年”活动任务分解表.xls", "UTF-8")); workbook.OutputStream());
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论