[color=red][quote]附件中有 poi-2.5.1.jar [/quote][/color]
public ActionForward listStudentAllExel(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Throwable {
// studentList
String[] key = {"A", "B", };
String[] value = {"同学", "班级", };
int rowNumber = 0;
HSSFWorkbook wb = new HSSFWorkbook();
/* 字体样式 */
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 12);
font.setFontName("Arial");
HSSFCellStyle style = wb.createCellStyle();//背景有颜色
style.setFont(font);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN); // 左边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN); // 上边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN); // 右边框
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);// 下边框
style.setFillPattern(HSSFCellStyle.BRICKS);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 位于中间
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); // 灰色
style.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index); // 灰色
style.setWrapText(true); // 当文字长于该列的宽度时,自动把宽加长
HSSFCellStyle style2 = wb.createCellStyle();
style2.setFont(font);
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style2.setWrapText(true);
HSSFCellStyle style3 = wb.createCellStyle();
style3.setFont(font);
style3.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style3.setBorderTop(HSSFCellStyle.BORDER_THIN);
style3.setBorderRight(HSSFCellStyle.BORDER_THIN);
style3.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style3.setAlignment(HSSFCellStyle.ALIGN_LEFT);
style3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style3.setWrapText(true);
//----------------------------- Black
HSSFSheet sheet = wb.createSheet("sheet1");
sheet.setColumnWidth((short) 0, (short) 6000); //默认设置宽度
sheet.setColumnWidth((short) 1, (short) 6000); //默认设置宽度
sheet.setColumnWidth((short) 2, (short) 4000); //默认设置宽度
sheet.setColumnWidth((short) 3, (short) 4000); //默认设置宽度
sheet.setColumnWidth((short) 4, (short) 4000); //默认设置宽度
sheet.setColumnWidth((short) 5, (short) 4000); //默认设置宽度
HSSFRow row = sheet.createRow( rowNumber++ );
HSSFCell cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("班级名称");
cell.setCellStyle(style);
sheet.addMergedRegion( new Region(0, (short) 1, 0, (short) 5 ) );
cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue( "南昌dx001" );
cell.setCellStyle(style3);
row = sheet.createRow( rowNumber++ );
cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("计一2021");
cell.setCellStyle(style);
sheet.addMergedRegion( new Region(1, (short) 1, 1, (short) 5 ) );
cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue( fileRegister.getProjectNum() );
cell.setCellStyle(style3);
row = sheet.createRow( rowNumber++ );
cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("姓名");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("年龄");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("地址");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("电话");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("入学时间");
cell.setCellStyle(style);
cell = row.createCell((short) 5);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("毕业时间");
cell.setCellStyle(style);
for (int j = 0; j < key.length; ++j){
String fileType_V = key[j];
String value_V = value[j];
int count = 0;
for (int i = 0; i < studentList.size(); ++i){
Student fr = (Student) studentList.get(i);
if(null!=fr && null!=fr.getFileType()){
count++;
rowNumber = this.addRowExcelInfor(cell, sheet, row, style2, fr, value_V, rowNumber) ;
}
}
}
String title = fileRegister.getProjectNum() + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition","attachment;filename="+title+".xls");
OutputStream os=response.getOutputStream();
//add workbook to OutpustStream
wb.write(os);
os.close();
System.out.print(os);
return null;
}
private int addRowExcelInfor(HSSFCell cell , HSSFSheet sheet , HSSFRow row ,HSSFCellStyle style2, FileRegister fr , String value_V , int rowNumber){
row = sheet.createRow( rowNumber++ );
cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue( fr.getAttachName() );
cell.setCellStyle(style2);
cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue( fr.getFileGlideNum() );
cell.setCellStyle(style2);
cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue( fr.getFileWord());
cell.setCellStyle(style2);
cell = row.createCell((short) 3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(value_V );
cell.setCellStyle(style2);
cell = row.createCell((short) 4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue( new SimpleDateFormat("yyyy-MM-dd").format(fr.getFinishDate() ) );
cell.setCellStyle(style2);
cell = row.createCell((short) 5);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue( fr.getApprovalMoney());
cell.setCellStyle(style2);
return rowNumber;
}