Chinaunix首页 | 论坛 | 博客
  • 博客访问: 320298
  • 博文数量: 287
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2985
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(287)

文章存档

2022年(85)

2021年(201)

2019年(1)

我的朋友

分类: Java

2022-01-12 15:28:23


点击(此处)折叠或打开


  1. package org.fh.util;

  2. import java.util.Date;
  3. import java.util.List;
  4. import java.util.Map;

  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;

  7. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  8. import org.apache.poi.hssf.usermodel.HSSFFont;
  9. import org.apache.poi.hssf.usermodel.HSSFRow;
  10. import org.apache.poi.hssf.usermodel.HSSFSheet;
  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  12. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  13. import org.apache.poi.ss.usermodel.VerticalAlignment;
  14. import org.apache.poi.ss.usermodel.Workbook;
  15. import org.fh.entity.PageData;
  16. import org.springframework.web.servlet.view.document.AbstractXlsView;

  17. /**
  18.  * 说明:导出到EXCEL
  19.  * 作者:FH Admin
  20.  * from:fhadmin.cn
  21.  */
  22. public class ObjectExcelView extends AbstractXlsView{
  23.     
  24.     @Override
  25.     protected void buildExcelDocument(Map<String, Object> model,
  26.             Workbook workbook, HttpServletRequest request,
  27.             HttpServletResponse response) throws Exception {
  28.         // TODO Auto-generated method stub
  29.         Date date = new Date();
  30.         String filename = DateUtil.date2Str(date, "yyyyMMddHHmmss");
  31.         HSSFSheet sheet;
  32.         response.setContentType("application/octet-stream");
  33.         response.setHeader("Content-Disposition", "attachment;filename="+filename+".xls");
  34.         
  35.         HSSFWorkbook book = (HSSFWorkbook) workbook;
  36.         sheet = book.createSheet("sheet1");
  37.         
  38.         List<String> titles = (List<String>) model.get("titles");
  39.         int len = titles.size();
  40.         HSSFCellStyle headerStyle = book.createCellStyle(); //标题样式
  41.         headerStyle.setAlignment(HorizontalAlignment.CENTER);
  42.         headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
  43.         HSSFFont headerFont = book.createFont();            //标题字体
  44.         headerFont.setBold(true);
  45.         headerFont.setFontHeightInPoints((short)11);
  46.         headerStyle.setFont(headerFont);
  47.         short height=25*20;
  48.         HSSFRow row = sheet.createRow(0);
  49.         for(int i=0; i<len; i++){ //设置标题
  50.             String title = titles.get(i);
  51.             row.setRowStyle(headerStyle);
  52.             row.createCell(i).setCellValue(title);
  53.         }
  54.         sheet.getRow(0).setHeight(height);
  55.         
  56.         HSSFCellStyle contentStyle = book.createCellStyle(); //内容样式
  57.         contentStyle.setAlignment(HorizontalAlignment.CENTER);
  58.         List<PageData> varList = (List<PageData>) model.get("varList");
  59.         int varCount = varList.size();
  60.         for(int i=0; i<varCount; i++){
  61.             PageData vpd = varList.get(i);
  62.             HSSFRow rows = sheet.createRow(i+1);
  63.             for(int j=0;j<len;j++){
  64.                 String varstr = vpd.getString("var"+(j+1)) != null ? vpd.getString("var"+(j+1)) : "";
  65.                 rows.setRowStyle(contentStyle);
  66.                 rows.createCell(j).setCellValue(varstr);
  67.             }
  68.             
  69.         }
  70.         
  71.     }



  72. }


阅读(153) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~