直接上代码:
/**
* 生成excel
*
* @throws UnsupportedEncodingException
*/
private void moveToControlExcel(OutputStream os, List<SupervisionProblem> entityList)
throws UnsupportedEncodingException {
String HSSFname = "自动监控设施现场检查存在问题详表";
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(HSSFname);
sheet.getPrintSetup().setLandscape(true);
// 创建标题样式
HSSFCellStyle style = wb.createCellStyle();
style.setWrapText(true); // 设置允许换行
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
// 设置字体
HSSFFont font = wb.createFont();
font.setFontName("华文楷体");
font.setFontHeightInPoints((short) 16);// 设置字体大小
style.setFont(font);// 样式,居中
// 创建正文数据样式
HSSFCellStyle style2 = wb.createCellStyle();
style2.setWrapText(true); // 设置允许换行
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
HSSFFont font2 = wb.createFont();
font2.setFontName("宋体");
font2.setFontHeightInPoints((short) 8);
style2.setFont(font2);// 选择需要用到的字体格式
String fileName = new String(HSSFname.getBytes(), System.getProperty("file.encoding"));
fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
// 创建第一行 标题
// 设置第一行第一到第7个单元格合并
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
HSSFRow rowTitle = sheet.createRow(0);
for (int i = 0; i < 7; i++) {
HSSFCell cellone = rowTitle.createCell(i); // 第一行第一列
cellone.setCellValue(HSSFname);// 表格的第一行第一列显示的数据
cellone.setCellStyle(style);// 样式,居中
}
rowTitle.setHeight((short) 1100);
// 第二行
HSSFRow row2 = sheet.createRow(1);
row2.setHeight((short) 480);
String colums[] = new String[] { "序号", "企业名称", "问题发现时间", "问题开始时间", "整改期限", "问题类别", "现场检查发现的问题" };
// 插入列名
for (int i = 0; i < colums.length; i++) {
HSSFCell row21 = row2.createCell(i);
row21.setCellValue(new HSSFRichTextString(colums[i]));// 表格的第一行第一列显示的数据
// 数据字体
row21.setCellStyle(style2);
if (i == 0) {
sheet.setColumnWidth(i, 2000);// 设置单元格宽度
} else if (i == 1) {
sheet.setColumnWidth(i, 6500);// 设置单元格宽度
} else if (i == 2) {
sheet.setColumnWidth(i, 3000);// 设置单元格宽度
} else if (i == 3) {
sheet.setColumnWidth(i, 3000);// 设置单元格宽度
} else if (i == 4) {
sheet.setColumnWidth(i, 3000);// 设置单元格宽度
} else if (i == 5) {
sheet.setColumnWidth(i, 4500);// 设置单元格宽度
} else if (i == 6) {
sheet.setColumnWidth(i, 7300);// 设置单元格宽度
}
}
// 插入数据
for (int j = 0; j < entityList.size(); j++) {
SupervisionProblem sp = entityList.get(j);
HSSFRow rowData = sheet.createRow((2 + j));
rowData.setHeight((short) 480);
for (int i = 0; i < colums.length; i++) {
HSSFCell cData = rowData.createCell(i);
cData.setCellStyle(style2);
if (i == 0) {
cData.setCellValue(j + 1); // 序号
} else if (i == 1) {
cData.setCellValue(sp.getEnterpriseName());
} else if (i == 2) {
cData.setCellValue(sp.getCreateTime());
} else if (i == 3) {
cData.setCellValue(sp.getProblemStartTime());
} else if (i == 4) {
cData.setCellValue(sp.getRecEndTime());
} else if (i == 5) {
cData.setCellValue(sp.getProblemTypeName());
} else if (i == 6) {
cData.setCellValue(sp.getProblemDescription());
}
}
}
try {
wb.write(os);
os.close();
os.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
if (wb != null) {
try {
wb.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}