poi导出excel

  1 首先要有poi这个jar包
  2 
  3 然后上代码:(这是个被实现的类:
  4 
  5 。。。。。
  6 
  7 Map<String,Object> map=new HashMap<String,Object>();
  8 /*
  9 :要传递过去的数据
 10 map.put("list",'data1');
 11 map.put("list2",'data2');
 12 map.put("list3",'data3');*/
 13 
 14 AExcelView v=new  AExcelView();
 15 
 16 return new ModelAndView(v,map);
 17 
 18  19 
 20 import java.util.Map;
 21 import java.util.List; 
 22 import java.text.SimpleDateFormat;
 23 import javax.servlet.http.HttpServletRequest;  
 24 import javax.servlet.http.HttpServletResponse;  
 25 import java.io.OutputStream;
 26 
 27 import org.apache.poi.hssf.usermodel.HSSFCell;  
 28 import org.apache.poi.hssf.usermodel.HSSFRichTextString;
 29 import org.apache.poi.hssf.util.Region;
 30 import org.apache.poi.hssf.usermodel.HSSFRow;
 31 import org.apache.poi.hssf.usermodel.HSSFSheet;  
 32 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 33 import org.springframework.web.servlet.view.document.AbstractExcelView;
 34 
 35 @SuppressWarnings("unchecked")
 36 public class AExcelView extends AbstractExcelView{
 37     public void buildExcelDocument(Map model, HSSFWorkbook workbook,HttpServletRequest request, HttpServletResponse response) throws Exception {
 38         response.reset();
 39         response.setContentType("APPLICATION/vnd.ms-excel");
 40         response.setHeader("Content-Disposition", "attachment;filename=\"导出表格名称.xls\"");
 41         
 42         HSSFSheet sheet = workbook.createSheet("创建工作簿");
       
/*
:给表格添加样式
//设置行列
HSSFSheet sheet = workbook.createSheet("list");
sheet.setColumnWidth((short) 0, (short) (123 * 35.7));
//设置行高
HSSFRow r=sheet.createRow(0);
r.setHeightInPoints(15);//也就是第一行的行高为15,excel显示20像素
//设置字体样式
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short)10); //字体高度
font.setColor(HSSFFont.COLOR_RED); //字体颜色
font.setFontName("黑体"); //字体
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //宽度
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
      HSSFCellStyle cellStyle = workbook.createCellStyle();
      cellStyle.setFont(font);
    //垂直方向居中
      cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
    //水平方向居中
      centerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
   //设置边框
      centerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);                   
centerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); centerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
centerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
*/
//开始调用样式:
  HSSFCell c0=r.createCell((short)0);
c0.setCellStyle(center);
c0.setCellValue(new HSSFRichTextString("第一列"));

 

 43         /*
 44         :转换拿到的数据的类型
 45         List list=(List)model.get("list");
 46         List list2=(List)model.get("list2");
 47         List list3=(List)model.get("list3"); */
 48        /* 
 49         :表格的第一行
 50         HSSFRow r=sheet.createRow(0);
 51         HSSFCell c=r.createCell((short)0);
 52         r.createCell((short)1);
 53         r.createCell((short)2);*/
 54 
 55         /*
 56         :强制格式转换,然后做运算
 57         double douNumber1=Double.parseDouble(model.get("list2").toString());
 58         double douNumber2=Double.parseDouble(model.get("list3").toString());
 59         double lastCostTotal=douNumber1-douNumber2;
 60         */
 61         
 62         /* 
 63         :数据连接起来,放在顶行(sheet.createRow(0))
 64         String costStr="合计:"+model.get("list2")+"元";
 65         costStr=costStr+"A:"+model.get("list3")+"元";
 66         c.setCellValue(new HSSFRichTextString(costStr));
 67         */
 68 
 69         /*
 70         :合并3个单元格(四个参数分别是:起始行,起始列,结束行,结束列 )
 71         sheet.addMergedRegion(new Region(0,(short)0,0,(short)2));
 72         */
 73 
 74         /* 
 75         :表格的第二行,一般这行拿来放每一列的标题(如:编号,名称等)
 76         r=sheet.createRow(1);
 77         r.createCell((short)0).setCellValue(new HSSFRichTextString("第一列"));
 78         r.createCell((short)1).setCellValue(new HSSFRichTextString("第二列"));
 79         r.createCell((short)2).setCellValue(new HSSFRichTextString("第三列"));
 80         */
 81 
 82         /*
 83         :日期显示格式
 84         SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
 85         */
 86 
 87         /*
 88         :循环遍历接下来的每一行(所以从i+2开始createRow)
 89         for(int i=0;i<list.size();i++){
 90             Object[] objs=(Object[])list.get(i);
 91             r=sheet.createRow(i+2);
 92             r.createCell((short)0).setCellValue(new HSSFRichTextString('显示的数据'));
 93             r.createCell((short)1).setCellValue(new HSSFRichTextString('显示的数据'));
 94             r.createCell((short)2).setCellValue(new HSSFRichTextString('显示的数据'));    
 95             }
 96         */
 97         OutputStream os = response.getOutputStream();
 98         workbook.write(os);
 99         os.flush();
100         os.close();
101     }
102 }

 

posted @ 2016-05-30 12:11  冷面书生_坤  阅读(149)  评论(0编辑  收藏  举报