String title=new String[] { orgName + "任务统计汇总表(" + searchRange +")"};//标题
String title[] = { "序号", "党组织名称", "任务总数", "按时完成任务数", "超时完成任务数", "未完成任务数" };//表头
HSSFWorkbook workbook = new HSSFWorkbook();//创建一个excel文件
HSSFSheet sheet = workbook.createSheet();//创建一个excel的sheet
sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 5));//合并单元格(1到5列)
HSSFRow createRow = sheet.createRow(0);//创建行
Row row0=sheet.getRow(0);
row0.setHeightInPoints(30);//设置行高为30像素
HSSFRow createRow1 = sheet.createRow(1);
HSSFCellStyle style = workbook.createCellStyle();//创建样式变量
HSSFFont fonts = workbook.createFont();//创建字体变量
fonts.setFontHeightInPoints((short) 11);
style.setFont(fonts);//把字体应用到当前样式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中方式
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中方式
style.setWrapText(true);//设置自动换行
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);//设置下边框
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//设置左边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);//设置右边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);//设置上边框
sheet.setDefaultColumnWidth(14);//设置默认列宽
sheet.setColumnWidth(0, 256*5+184);//设置某一列的宽度(256表示一个字符)
sheet.setColumnWidth(9, 256*24+184);
HSSFCellStyle styletop = workbook.createCellStyle();
styletop.setAlignment(HSSFCellStyle.ALIGN_CENTER);
styletop.setWrapText(true);
for (int i = 0; i < titletop.length; i++) {
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 18);
HSSFCell cell = createRow.createCell(i);
cell.setCellValue(titletop[i]);//设置单元格的值
styletop.setFont(font);
cell.setCellStyle(styletop);
}
for (int i = 0; i < title.length; i++) {
HSSFCell cell = createRow1.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
if(resultList!=null&&resultList.size()>0) {//resultList是结果集,即表格内容体
for(int i = 0;i < resultList.size();i++) {
HSSFRow row = sheet.createRow(i + 2);
row.createCell(0).setCellValue(i + 1);
row.getCell(0).setCellStyle(style);
row.createCell(1).setCellValue(resultList.get(i).getOrgName());
row.getCell(1).setCellStyle(style);
row.createCell(2).setCellValue(resultList.get(i).getCountNum());
row.getCell(2).setCellStyle(style);
row.createCell(3).setCellValue(resultList.get(i).getCountCompleted());
row.getCell(3).setCellStyle(style);
row.createCell(4).setCellValue(resultList.get(i).getCountChaoCompleted());
row.getCell(4).setCellStyle(style);
row.createCell(5).setCellValue(resultList.get(i).getCountRed());
row.getCell(5).setCellStyle(style);
}
}
//将文件存到浏览器的下载地址
ServletOutputStream outputStream;
try {
outputStream = response.getOutputStream();
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-disposition",
"attachment; filename=" + URLEncoder.encode(orgName + "工作任务统计汇总表", "utf-8") + ".xls");
workbook.write(outputStream);
outputStream.close();
} catch (IOException e) {
e.print
String title[] = { "序号", "党组织名称", "任务总数", "按时完成任务数", "超时完成任务数", "未完成任务数" };//表头
HSSFWorkbook workbook = new HSSFWorkbook();//创建一个excel文件
HSSFSheet sheet = workbook.createSheet();//创建一个excel的sheet
sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 5));//合并单元格(1到5列)
HSSFRow createRow = sheet.createRow(0);//创建行
Row row0=sheet.getRow(0);
row0.setHeightInPoints(30);//设置行高为30像素
HSSFRow createRow1 = sheet.createRow(1);
HSSFCellStyle style = workbook.createCellStyle();//创建样式变量
HSSFFont fonts = workbook.createFont();//创建字体变量
fonts.setFontHeightInPoints((short) 11);
style.setFont(fonts);//把字体应用到当前样式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中方式
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中方式
style.setWrapText(true);//设置自动换行
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);//设置下边框
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//设置左边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);//设置右边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);//设置上边框
sheet.setDefaultColumnWidth(14);//设置默认列宽
sheet.setColumnWidth(0, 256*5+184);//设置某一列的宽度(256表示一个字符)
sheet.setColumnWidth(9, 256*24+184);
HSSFCellStyle styletop = workbook.createCellStyle();
styletop.setAlignment(HSSFCellStyle.ALIGN_CENTER);
styletop.setWrapText(true);
for (int i = 0; i < titletop.length; i++) {
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 18);
HSSFCell cell = createRow.createCell(i);
cell.setCellValue(titletop[i]);//设置单元格的值
styletop.setFont(font);
cell.setCellStyle(styletop);
}
for (int i = 0; i < title.length; i++) {
HSSFCell cell = createRow1.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
if(resultList!=null&&resultList.size()>0) {//resultList是结果集,即表格内容体
for(int i = 0;i < resultList.size();i++) {
HSSFRow row = sheet.createRow(i + 2);
row.createCell(0).setCellValue(i + 1);
row.getCell(0).setCellStyle(style);
row.createCell(1).setCellValue(resultList.get(i).getOrgName());
row.getCell(1).setCellStyle(style);
row.createCell(2).setCellValue(resultList.get(i).getCountNum());
row.getCell(2).setCellStyle(style);
row.createCell(3).setCellValue(resultList.get(i).getCountCompleted());
row.getCell(3).setCellStyle(style);
row.createCell(4).setCellValue(resultList.get(i).getCountChaoCompleted());
row.getCell(4).setCellStyle(style);
row.createCell(5).setCellValue(resultList.get(i).getCountRed());
row.getCell(5).setCellStyle(style);
}
}
//将文件存到浏览器的下载地址
ServletOutputStream outputStream;
try {
outputStream = response.getOutputStream();
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-disposition",
"attachment; filename=" + URLEncoder.encode(orgName + "工作任务统计汇总表", "utf-8") + ".xls");
workbook.write(outputStream);
outputStream.close();
} catch (IOException e) {
e.print
本文介绍了一种使用Java生成Excel表格的方法。具体步骤包括定义表头、设置样式、填充数据及导出文件等。通过示例代码展示了如何创建含有合并单元格、自定义样式和特定列宽的工作表。
2646

被折叠的 条评论
为什么被折叠?



