用POI加载模板生成excel报表

加载模板生成excel报表

----------------------------------------------------------------------------------------------------------------------------------------

 

  1. package com.stake.mis.baobiao;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import javax.servlet.ServletOutputStream;
  9. import org.apache.poi.hssf.usermodel.HSSFCell;
  10. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  11. import org.apache.poi.hssf.usermodel.HSSFFont;
  12. import org.apache.poi.hssf.usermodel.HSSFFooter;
  13. import org.apache.poi.hssf.usermodel.HSSFRow;
  14. import org.apache.poi.hssf.usermodel.HSSFSheet;
  15. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  16. import org.apache.poi.hssf.util.HSSFColor;
  17. import org.apache.poi.hssf.util.Region;
  18. import com.stake.mis.bean.User ;
  19. public class ExeclManager {
  20. /**
  21. * list 存放查询返回得结果列表
  22. * sheetname excel模板中sheet得名字
  23. * out 输出流对象
  24. /
  25.  public void getMainCardExcel(List list,String filepath,String sheetname,ServletOutputStream out) throws FileNotFoundException, IOException
  26.  {
  27. //  读取工作簿
  28.   System.out.println(filepath);
  29.   HSSFWorkbook workbook =new HSSFWorkbook(new FileInputStream(filepath));
  30.   try
  31.   {
  32.    
  33.    //读取工作表
  34.    HSSFSheet sheet = workbook.getSheet(sheetname);
  35.    HSSFRow row;
  36.    HSSFCell cell = null;
  37.    int a = 8;
  38.    HSSFCellStyle style = this.getStyle(workbook);
  39.    for(int i =0;i<list.size();i++)
  40.    {
  41.    //从查询结果列表中获取对象
  42.     User user = (User )list.get(i);
  43.    //该行以前得部分从模板中取得;
  44.     row = sheet.createRow(a);
  45.     cell = row.createCell((short0,HSSFCellStyle.ALIGN_CENTER);
  46.     cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  47.     cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  48.     cell.setCellValue(i+1);
  49.     cell.setCellStyle(style);
  50.     
  51.     cell = row.createCell((short1,HSSFCellStyle.ALIGN_CENTER);
  52.     cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  53.     cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  54.     cell.setCellValue(mc.getUserName());
  55.     cell.setCellStyle(style);
  56.     
  57.     cell = row.createCell((short2,HSSFCellStyle.ALIGN_CENTER);
  58.     cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  59.     cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  60.     cell.setCellValue(mc.getUserAge());
  61.     cell.setCellStyle(style);
  62.     
  63.     cell = row.createCell((short3,HSSFCellStyle.ALIGN_CENTER);
  64.     cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  65.     cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  66.     cell.setCellValue(mc.get某属性());
  67.     cell.setCellStyle(style);
  68.     
  69.     cell = row.createCell((short4,HSSFCellStyle.ALIGN_CENTER);
  70.     cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  71.     cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  72.     cell.setCellValue(mc.getEq某属性());
  73.     cell.setCellStyle(style);
  74.     
  75.     a++;
  76.    }
  77.    workbook.write(out);
  78.    out.flush();
  79.    out.close();
  80.   }
  81.   catch(Exception e)
  82.   {
  83.    e.printStackTrace();
  84.   }
  85.  }
  86.   public HSSFCellStyle getStyle(HSSFWorkbook workbook)
  87.  {
  88. //     设置字体;
  89.   HSSFFont font = workbook.createFont();
  90.   //设置字体大小;
  91.   font.setFontHeightInPoints((short)9);
  92.   //设置字体名字;
  93.   font.setFontName("Courier New");
  94.   //font.setItalic(true);
  95.   //font.setStrikeout(true);
  96. //     设置样式;
  97.   HSSFCellStyle style = workbook.createCellStyle();
  98.   //设置底边框;
  99.   style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  100.   //设置底边框颜色;
  101.   style.setBottomBorderColor(HSSFColor.BLACK.index);
  102.   //设置左边框;
  103.   style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  104.   //设置左边框颜色;
  105.   style.setLeftBorderColor(HSSFColor.BLACK.index);
  106.   //设置右边框;
  107.   style.setBorderRight(HSSFCellStyle.BORDER_THIN);
  108.   //设置右边框颜色;
  109.   style.setRightBorderColor(HSSFColor.BLACK.index);
  110.   //设置顶边框;
  111.   style.setBorderTop(HSSFCellStyle.BORDER_THIN);
  112.   //设置顶边框颜色;
  113.   style.setTopBorderColor(HSSFColor.BLACK.index);
  114.   //在样式用应用设置的字体;
  115.   style.setFont(font);
  116.      //设置自动换行;
  117.   style.setWrapText(false);
  118.   //设置水平对齐的样式为居中对齐;
  119.   style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  120.   //设置垂直对齐的样式为居中对齐;
  121.   style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  122.   return style;
  123.  }
  124. }

strut的action某方法中在获取查询结果后的部分代码

-------------------------------------------------------------------------------------------------------------------------------------------

  

  1.   List list = null;
  2.   String filename = "";
  3.    //action中调用service对象的方法,查询User信息;
  4.    list = service.queryUser(查询约束);
  5.    ExeclManager ma = new ExeclManager();
  6.    //设置响应的编码方式;
  7.    response.setCharacterEncoding("gb2312");
  8.    filename = filename + "设备台账.xls";
  9.    response.addHeader("Content-Disposition","attachment;filename="+java.net.URLEncoder.encode(filename, "utf-8"));
  10.    response.setContentType("application/vnd.ms-excel;charset=gb2312");
  11.    //excel模板所在的位置;
  12.    String filepath = this.getServlet().getServletContext().getRealPath("/bbmanage");
  13.    ServletOutputStream out = response.getOutputStream();
  14.   //模板名称为example.xls;Sheet1为模板中sheet的名称
  15.    //调用方法生成excel;
  16.    ma.getMainCardExcel(list, filepath+"/example.xls""Sheet1",out);
 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值