package cn.sh.bzt.kwj.action;

 import java.io.IOException;
 import java.io.OutputStream;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.List;

 import javax.annotation.Resource;

 import org.apache.poi.hssf.usermodel.HSSFCell;
 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
 import org.apache.poi.hssf.usermodel.HSSFFont;
 import org.apache.poi.hssf.usermodel.HSSFRow;
 import org.apache.poi.hssf.usermodel.HSSFSheet;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;

 import cn.sh.bzt.base.action.BaseAction;
 import cn.sh.bzt.base.page.Pager;
 import cn.sh.bzt.base.page.PagerHelp;
 import cn.sh.bzt.kwj.pojo.TEnterprise;
 import cn.sh.bzt.kwj.pojo.TOrgnization;
 import cn.sh.bzt.kwj.pojo.TUser;
 import cn.sh.bzt.kwj.service.IUserService;
 import cn.sh.bzt.kwj.service.TotalFormService;

 /**
  * 统计报表--行政统计
  * @author 姚智韧
  *
  */
 @SuppressWarnings("serial")
 public class TotalFormAction extends BaseAction {

 //    private Log log = LogFactory.getLog(TEnterpriseIdAction.class);// 日志类
     private List<TEnterprise> list = null;
     private TEnterprise enterprise = new TEnterprise();

     private String projectCode = "33";
     private String licenseName = "口岸卫生许可证核发 ";
     private String childNodeCode = "3300";
     private String childNodeName = "无";
     public OutputStream out;

     public TEnterprise getEnterprise() {
         return enterprise;
     }

     public void setEnterprise(TEnterprise enterprise) {
         this.enterprise = enterprise;
     }

     private TotalFormService totalFormService;

     public TotalFormService getTotalFormService() {
         return totalFormService;
     }

     @Resource(name = "totalFormServiceImpl")
     public void setTotalFormService(TotalFormService totalFormService) {
         this.totalFormService = totalFormService;
     }

     private IUserService userService;

     public IUserService getUserService() {
         return userService;
     }

     @Resource(name = "userServiceImpl")
     public void setUserService(IUserService userService) {
         this.userService = userService;
     }

     /**
      * 查询行政统计数据
      *
      * @return
      */
     public String findAllTotalForm() {
         try {
             TUser user = (TUser) request.getSession().getAttribute(
                     "USER_IN_SESSION");
             request.setAttribute("userLoginName", user.getUserLoginName());

             TOrgnization toi = new TOrgnization();
             Integer i = user.getTOrgnization().getOrgId();
             if (i != null) {
                 toi = userService.getAllOrgList(i);
             }
             request.setAttribute("orgName", toi.getOrgName());

             request.getSession().setAttribute("companyName", "");
             request.getSession().setAttribute("stDate", "");
             request.getSession().setAttribute("endDate","");
             // 总行数
             int totalRows = totalFormService.getTotalFormCount(enterprise);
             System.out.println("总行数totalRows----->" + totalRows);

             Pager pager = PagerHelp.getPager(request, totalRows);
             pager.setUrl("tjbb/findAllTotalForm.action");

             list = new ArrayList<TEnterprise>();
             list = ListUtils(enterprise, pager);
             System.out.println("list----->" + list.get(0));
             request.setAttribute("ALL_Total_LIST", list);
             request.setAttribute("PAGE", pager);
             return SUCCESS;
         } catch (Exception e) {
             request.setAttribute("ERROR_CAUSE", "数据库操作失败");
             return ERROR;
         }
     }

     /**
      * 按条件查询
      *
      * @return
      */
     public String findByCondition() {
         try {
             TUser user = (TUser) request.getSession().getAttribute(
                     "USER_IN_SESSION");
             request.setAttribute("userLoginName", user.getUserLoginName());
             TOrgnization toi = new TOrgnization();
             Integer i = user.getTOrgnization().getOrgId();
             if (i != null) {
                 toi = userService.getAllOrgList(i);
                 request.setAttribute("orgName", toi.getOrgName());
             }

             String filingdate1 = request.getParameter("enterprise.filingdate1");
             System.out.println("filingdate1------->" + filingdate1);

             String filingdate2 = request.getParameter("enterprise.filingdate2");
             System.out.println("filingdate2------->" + filingdate2);

             String companyName = request.getParameter("enterprise.companyName");

             enterprise.setFilingdate1(convertFormat(filingdate1));
             enterprise.setFilingdate2(convertFormat(filingdate2));

             request.getSession().setAttribute("companyName", companyName);
             request.getSession().setAttribute("stDate", filingdate1);
             request.getSession().setAttribute("endDate",filingdate2);

             // 总行数
             int totalRows = totalFormService.getTotalFormCount(enterprise);
             System.out.println("总行数totalRows----->" + totalRows);

             Pager pager = PagerHelp.getPager(request, totalRows);

             list = new ArrayList<TEnterprise>();
             list = ListUtils(enterprise, pager);
             request.setAttribute("ALL_Total_LIST", list);
             request.setAttribute("PAGE", pager);
             System.out.println("list---->" + list.get(0));
             return SUCCESS;
         } catch (Exception e) {
             request.setAttribute("ERROR_CAUSE", "数据库操作失败");
             e.printStackTrace();
             return ERROR;
         }

     }

     /**
      * 导出数据到Excel
      * @param model
      * @throws IOException
      * @throws Exception
      */
     public String exportDataToExcel() throws IOException {
         try {
             response.setCharacterEncoding("UTF-8");
             String filename = "统计报表-行政统计.xls";
             HSSFWorkbook wb = new HSSFWorkbook();
             response.setContentType("application/x-download");
             try {
                 response.setHeader("Content-Disposition", "attachment;filename="
                         + new String(filename.getBytes("gbk"), "iso-8859-1"));
             } catch (Exception e1) {
                 e1.printStackTrace();
             }
             HSSFSheet sheet = wb.createSheet("行政统计一");
             // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
             HSSFRow row = sheet.createRow((int) 0);
             // 第四步,创建单元格,并设置值表头 设置表头居中
             HSSFFont font = wb.createFont();
             font.setFontName("宋体");
             font.setFontHeightInPoints((short) 16);

             HSSFCellStyle style = wb.createCellStyle();
             style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

             HSSFCell cell = row.createCell((short) 0);
             cell.setCellValue("许可项目代码");
             cell.setCellStyle(style);
             cell = row.createCell((short) 1);
             cell.setCellValue("许可项目代码");
             cell.setCellStyle(style);
             cell = row.createCell((short) 2);
             cell.setCellValue("子项代码");
             cell.setCellStyle(style);
             cell = row.createCell((short) 3);
             cell.setCellValue("子项名称");
             cell.setCellStyle(style);
             cell = row.createCell((short) 4);
             cell.setCellValue("许可证书编号");
             cell.setCellStyle(style);
             cell = row.createCell((short) 5);
             cell.setCellValue("许可项目及范围");

             cell = row.createCell((short) 6);
             cell.setCellValue("发证日期");
             cell.setCellStyle(style);
             cell = row.createCell((short) 7);
             cell.setCellValue("有效期至");
             cell.setCellStyle(style);
             cell = row.createCell((short) 8);
             cell.setCellValue("生产地址");
             cell.setCellStyle(style);
             cell = row.createCell((short) 9);
             cell.setCellValue("发证机构 ");
             cell.setCellStyle(style);
             cell = row.createCell((short) 10);
             cell.setCellValue("发证机构代码");
             cell.setCellStyle(style);
             cell = row.createCell((short) 11);
             cell.setCellValue("被许可机构");
             cell.setCellStyle(style);
             cell = row.createCell((short) 12);
             cell.setCellValue("被许可机构代码(证件号码) ");
             cell.setCellStyle(style);
             cell = row.createCell((short) 13);
             cell.setCellValue("被许可机构行政区划 ");
             cell.setCellStyle(style);

             String filingdate1 = (String) request.getSession().getAttribute("stDate");
             System.out.println("filingdate1------->" + filingdate1);

             String filingdate2 = (String) request.getSession().getAttribute("endDate");
             System.out.println("filingdate2------->" + filingdate2);

             String companyName = (String) request.getSession().getAttribute("companyName");
             System.out.println("companyName------->" + companyName);

             enterprise.setFilingdate1(convertFormat(filingdate1));
             enterprise.setFilingdate2(convertFormat(filingdate2));
             enterprise.setCompanyName(companyName);
             list = new ArrayList<TEnterprise>();

             list = totalFormService.findExportData(enterprise);

             for (int i = 0; i < list.size(); i++)
             {
                 row = sheet.createRow((int) i + 1);
                 TEnterprise enterprise = (TEnterprise) list.get(i);
                 String managescope = enterprise.getApprovalmanagescope();
                 String managescope1 = null;
                 if(managescope!=null){
                     managescope1 = managescope.substring(1, managescope.length()-1);
                 }else{
                     managescope1 = managescope;
                 }
                 // 第四步,创建单元格,并设置值
                 row.createCell((short) 0).setCellValue(projectCode);  //许可项目代码
                 row.createCell((short) 1).setCellValue(licenseName);  //许可项目代码
                 row.createCell((short) 2).setCellValue(childNodeCode); //子项代码
                 row.createCell((short) 3).setCellValue(childNodeName);//子项名称
                 row.createCell((short) 4).setCellValue(enterprise.getLicenseCode());  //许可证书编号
                 row.createCell((short) 5).setCellValue(managescope1);  //许可项目及范围
                 row.createCell((short) 6).setCellValue(enterprise.getJdTime());  //发证日期
                 row.createCell((short) 7).setCellValue(enterprise.getInDate());  //有效期至
                 row.createCell((short) 8).setCellValue(enterprise.getBusinessAddress()); //生产地址
                 row.createCell((short) 9).setCellValue(enterprise.getEmbranchment());//发证机构
                 row.createCell((short) 10).setCellValue(enterprise.getOrgCode());  //发证机构代码
                 row.createCell((short) 11).setCellValue(enterprise.getCompanyName());  //被许可机构
                 row.createCell((short) 12).setCellValue(enterprise.getLegalPersonCard()); //被许可机构代码 (证件号码)
                 row.createCell((short) 13).setCellValue(enterprise.getAdminZoning());//被许可机构行政区划
             }
             System.out.println("导出表格的数据大小---->"+list.size());
             request.setAttribute("ALL_Total_LIST", list);
             out = response.getOutputStream();
             wb.write(out);
             if(((filingdate1!=null)||(filingdate2!=null)||(companyName!=null))&&((filingdate1!=null)&&(filingdate2!=null))){
                 out.close();
                 return "EXPORT";
             }else{
                 out.close();
                 return SUCCESS;
             }

         } catch (Exception e) {
             e.printStackTrace();
             return ERROR;
         }

     }
     /**
      * 获取经处理后的list
      * @return
      */
     public List<TEnterprise> ListUtils(TEnterprise  enterprise, Pager pager){
         try {
             list = new ArrayList<TEnterprise>();

             // 得到行政统计List
             List<TEnterprise> tList = totalFormService.findAll(enterprise,
                     pager);
             for (int j = 0; j < tList.size(); j++) {
 //                System.out.println("遍历list--------->");
                 enterprise = tList.get(j);
                 String jdTime = enterprise.getJdTime();
 //                System.out.println("jdTime------>" + jdTime);
                 String inDate = enterprise.getInDate();
 //                System.out.println("inDate------>" + inDate);
                 if (inDate != null) {
                     String jzTime2 = convertDate(jdTime, new Integer(inDate));
                     enterprise.setInDate(convertFormat(jzTime2));
                 } else {
                     enterprise.setInDate("");
                 }
                 String managescope = enterprise.getApprovalmanagescope();
                 if(managescope!=null){
                     String managescope1 = managescope.substring(1, managescope.length()-1);
                     enterprise.setApprovalmanagescope(managescope1);
                 }
                 list.add(enterprise);
             }
             return list;
         }catch (Exception e) {
             e.printStackTrace();
             return null;
         }

     }

     /**
      * 日期转换 将yyyy-MM-dd格式日期转换成yyyy年MM月dd日
      *
      * @param jdTime
      * @return
      * @throws Exception
      */
     public String convertFormat(String jdTime) throws Exception {
         // Calendar objCalendar = Calendar.getInstance();
         Date objDate = new SimpleDateFormat("yyyy-MM-dd").parse(jdTime);

         SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");

         String jdTime2 = sdf.format(objDate);
         System.out.println("jdTime2---->" + jdTime2);
         return jdTime2;
     }

     /**
      * 日期转换函数 将数据库中日期格式为“yyyy年MM月日”加上天数得到"yyyy-MM-dd"格式数据
      * @param jdTime  发证日期
      * @param inDate  有效期
      * @return
      * @throws Exception
      */
     public String convertDate(String jdTime, int inDate) throws Exception {
         System.out.println("进入日期转换函数----->" + 1111111);

         Calendar objCalendar = Calendar.getInstance();
         Date objDate = new SimpleDateFormat("yyyy年MM月dd日").parse(jdTime);
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

         SimpleDateFormat sdf0 = new SimpleDateFormat("yyyy");
         SimpleDateFormat sdf1 = new SimpleDateFormat("MM");
         SimpleDateFormat sdf2 = new SimpleDateFormat("dd");
         int year = Integer.parseInt(sdf0.format(objDate));
         int month = Integer.parseInt(sdf1.format(objDate));
         int date = Integer.parseInt(sdf2.format(objDate));
         // System.out.println(Integer.valueOf(inDate).intValue());
         objCalendar.set(year, month - 1, date);
         objCalendar.add(Calendar.DATE, inDate);
         System.out.println("Calendar.DATE---------->" + Calendar.DATE);
         objDate = objCalendar.getTime();
         System.out.println("objDate---->" + objDate);
         String jzTime = sdf.format(objDate);
         System.out.println("截止日期--->" + jzTime);
         return jzTime;
     }

 }

代码仅供参考,在CSDN下有个大牛实现数据导出时,可以实现图片等的导出,还可以既本地调用和在WEB服务器端用Servlet的调用。详情可参考(http://blog.csdn.net/lenotang/article/details/2823230)

使用POI实现数据导出Excel表格的更多相关文章

  1. Java使用POI实现数据导出excel报表

    Java使用POI实现数据导出excel报表 在上篇文章中,我们简单介绍了java读取word,excel和pdf文档内容 ,但在实际开发中,我们用到最多的是把数据库中数据导出excel报表形式.不仅 ...

  2. Java利用POI实现导入导出Excel表格示例代码

    转自:https://www.jb51.net/article/95526.htm 介绍 Jakarta POI 是一套用于访问微软格式文档的Java API.Jakarta POI有很多组件组成,其 ...

  3. 【前端】将前台table数据导出excel表格

    1.首先引用jquery以及table2excel <script type="text/javascript" src="js/jquery.table2exce ...

  4. thinkphp5.0数据导出excel表格

    第一步.创建Model类文件(名称自定) 第二步.在类中写入以下代码 <?php namespace Admin\admin\model; use think\Model; class Mark ...

  5. 一个php文件就可以把数据库的数据导出Excel表格

    数据库内容太多,复制粘贴太麻烦?那就用代码实现把,把代码写好了,导出还不容易吗,访问即可导出. excel.php <?php error_reporting(E_ALL ^ E_DEPRECA ...

  6. 数据导出Excel表格

    public String exportInfoFr(String path,String name,String startdate,String enddate,SysUser user){ Li ...

  7. 使用poi导出Excel表格,jar包冲突

    HTTP Status 500 – Internal Server Error Type Exception Report Message Handler processing failed; nes ...

  8. 百度地图里面搜索到的公司商家电话导出表格?怎样将把百度地图里面搜索到的公司 电话 地址 等数据导出excel里?

    好多人在问:如何将百度地图里面搜索到的公司商家电话导出表格?怎样将把百度地图里面搜索到的公司 电话 地址 等数据导出excel里? 现在,很多人都在网络上找商家,联系业务. 百度地图里有很多的商家联系 ...

  9. 将页面中表格数据导出excel格式的文件(vue)

    近期由于项目需要,需要将页面中的表格数据导出excel格式的文件,折腾了许久,在网上各种百度,虽然资料不少,但是大都不全,踩了许多坑,总算是皇天不负有心人,最后圆满解决了. 1.安装相关依赖(npm安 ...

随机推荐

  1. Scrum4.0+5.0 数独游戏

    1.题目: 1.准备看板. 形式参考图4. 2.任务认领,并把认领人标注在看板上的任务标签上. 先由个人主动领任务,PM根据具体情况进行任务的平衡. 然后每个人都着手实现自己的任务. 3.为了团队合作 ...

  2. C#设计模式——生成器模式(Builder Pattern)

    一.概述在软件系统中,有时候面临着复杂的对象创建,该对象由一定算法构成的子对象组成,由于需求变化,这些子对象会经常变换,但组合在一起的算法却是稳定的.生成器模式可以处理这类对象的构建,它提供了一种封装 ...

  3. UnityShader快速上手指南(四)

    简介 由于其他项目中断了几天更新,继续~~ 这一篇主要是讲光照的(包含漫反射和高光以及多光源的处理) 还是先来看看具体效果(多光源后面单独展示) 有了基本的光照处理之后越来越有立体感了有不有 ╮(╯▽ ...

  4. 将HTML5 Canvas的内容保存为图片

    主要思想是借助Canvas自己的API - toDataURL()来实现,整个实现 HTML + JavaScript的代码很简单. 代码如下: <html> <meta http- ...

  5. [CLR via C#]26. 计算限制的异步操作

    一.CLR线程池基础 前面说过,创建和销毁线程是一个比较昂贵的操作,太多的线程也会浪费内存资源.由于操作系统必须调度可运行的线程并执行上下文切换,所以太多的线程还有损于性能.为了改善这个情况,CLR使 ...

  6. 【jQuery基础学习】11 jQuery性能简单优化

    关于性能优化 合适的选择器 $("#id")会直接调用底层方法,所以这是最快的.如果这样不能直接找到,也可以用find方法继续查找 $("p")标签选择器也是直 ...

  7. php表单中如何获取单选按钮与复选按钮的值

    php代码中获取表单中单选按钮的值:(单选按钮只能让我们选择一个,这里有一个"checked"属性,这是用来默认选取的,我们每次刷新我们的页面时就默认为这个值.) 例:<fo ...

  8. ComboBox的联动(三层架构)

    需求:根据年级下拉框的变化使得科目下拉框绑定次年级下对应有的值 我们用三层架构的模式来实现 1.我们想和数据库交互,我们首先得来先解决DAL数据库交互层 01.获得年级下拉框的数据 在GradeDAL ...

  9. ThreadLocal,Java中特殊的线程绑定机制

    在DRP项目中,我们使用了ThreadLocal来创建Connection连接,避免了一直以参数的形式将Connection向下传递(传递connection的目的是由于jdbc事务要求确保使用同一个 ...

  10. android开发布局文件imageview 图片等比例缩放:

    ImageView的属性scaleType,如果等比缩放的话,就使用CenterInside,如果想固定大小的话,就CenterCrop <?xml version="1.0" ...