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

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-09-27 11:42:35


点击(此处)折叠或打开


  1. package com.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.springframework.web.servlet.view.document.AbstractXlsView;

  16. import com.fh.util.PageData;
  17. import com.fh.util.Tools;

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



  73. }


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