// 表头
String[] title = new String[] { "序号", "船名", "船检登记号", "船籍港", "总长", "垂线间长", "总吨位", "主机总功率", "航区", "海河船", "船型", "建造完工日期", "检验状态", "归属类别", "船级状态", "净吨位", "载重量", "船舶所有人", "船舶经营人",
"船舶管理公司", "船舶制造厂" };
HSSFWorkbook book = new HSSFWorkbook();
HSSFSheet sheet = book.createSheet("船舶基本信息");
HSSFRow titleRow0 = sheet.createRow(0);// 第一行 实施部门
titleRow0.setHeightInPoints(20);// 第一行行高
// 字体
HSSFFont font = book.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 10);// 字体大小
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗
// 风格
HSSFCellStyle style = book.createCellStyle();
style.setFont(font);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
for (int i = 0; i < title.length; i++) {
HSSFCell cell = titleRow0.createCell(i);
// 设置列宽
if (i > 16 || i == 1) {
sheet.setColumnWidth(i, title[i].getBytes().length * 456 * 2);
} else {
sheet.setColumnWidth(i, title[i].getBytes().length * 456);
}
cell.setCellStyle(style);
cell.setCellValue(title[i]);
}
HSSFCellStyle cellStringStyle = book.createCellStyle();
cellStringStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
cellStringStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
for (int i = 0; i < problems.size(); i++) {
Object[] content = (Object[]) problems.get(i);
HSSFRow titleRow = sheet.createRow(i + 1);
for (int j = 0; j < content.length; j++) {
HSSFCell cell = titleRow.createCell(j);
String str = String.valueOf(content[j]);
cell.setCellStyle(cellStringStyle);
if (str != null && !"null".equals(str)) {
cell.setCellValue(str);
}
}
}
response.setHeader("Content-Disposition", "attachment; filename=" + new String("船舶基本信息导出".getBytes("gb18030"), "iso-8859-1") + ".xls");
response.setContentType("application/vnd.ms-excel;charset=utf-8");
ServletOutputStream out = response.getOutputStream();
book.write(out);
out.flush();
out.close();