Jakarta POI导出Excel

public void exporterForm(HttpServletResponse response) throws IOException {
//System.out.println("业务层执行导出!");
//创建HSSFWorkbook对象,(Excel文档对象)
HSSFWorkbook wb = new HSSFWorkbook();
//创建sheet对象,excel的表单
HSSFSheet sheet=wb.createSheet("成绩表");
//在sheet李创建一行(参数是行索引)0~65535之间
HSSFRow row1 = sheet.createRow(0);
//创建单元格(excel的单元格,参数为列索引,0~255之间)
HSSFCell cell = row1.createCell(0);
//设置单元格内容
cell.setCellValue("学院考试成绩一览表");
//合并单元格CellRangeAddress  参数依次表示  起始行,截止行,起始列,截止列
sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));

//创建第二行
HSSFRow row2 = sheet.createRow(1);
//创建单元格并设置单元格内容
row2.createCell(0).setCellValue("姓名");
   row2.createCell(1).setCellValue("班级");    
   row2.createCell(2).setCellValue("笔试成绩");
   row2.createCell(3).setCellValue("机试成绩"); 
   HSSFRow row3 = sheet.createRow(2);
//创建单元格并设置单元格内容
   row3.createCell(0).setCellValue("李明");
   row3.createCell(1).setCellValue("As178");
   row3.createCell(2).setCellValue("87");    
   row3.createCell(3).setCellValue("78"); 
   
   //输出Excel文件
//    OutputStream output = null;
// try {
// output = response.getOutputStream();
// response.reset();
//    response.setHeader("Content-disposition", "attachment; filename=details.xls");
//    response.setContentType("application/msexcel");        
//    wb.write(output);
//    output.flush();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }finally{
// if(output != null){
// output.close();
// }
// }
   
   try {
    FileOutputStream output=new FileOutputStream("d:\\work.xls");
wb.write(output);
output.flush();
} catch (IOException e) {
e.printStackTrace();
}

}

http://blog.csdn.net/mike_caoyong/article/details/7874861

http://www.open-open.com/lib/view/open1429847388213.html

猜你喜欢

转载自blog.csdn.net/u012669164/article/details/52789986