java导出excel的两种方式(下)

简介: java导出excel的两种方式

8、下面这部分代码主要是把数据传给浏览器,前面部分告诉浏览器该数据流是什么类型的,本例传的是excel格式的,浏览器会自动判定为excel,提示是否保存。

//给文件命名。随机命名
String fileName = "Excel-" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";
     //告诉浏览器数据格式,将头和数据传到前台
      String headStr = "attachment; filename=\"" + fileName + "\"";
      response.setContentType("APPLICATION/OCTET-STREAM");
      response.setHeader("Content-Disposition", headStr);
      OutputStream out = response.getOutputStream();
//调用poi的工具类
      ExportExcel ex = new ExportExcel(title, rowsName, dataList);
      try {
        ex.export(out);
      } catch (Exception e) {
        e.printStackTrace();
      }
      out.flush();
      out.close();
      return;
    }

9、注意:如果从数据库查到的是数字,比如0代表男,1代表女,不加处理,会导出数字,处理方法如下:(主要代码),下面代码又没有的当前页的限制,根据条件查到多少打印多少。

String patientid1 = request.getParameter("patientid");
      int patientid = 0;
      if (patientid1 != null && !patientid1.equals("")) {
        patientid = Integer.parseInt(patientid1);
      }
      String prescriptionid1 = request.getParameter("prescriptionid");
      int prescriptionid = 0;
      if (prescriptionid1 != null && !prescriptionid1.equals("")) {
        prescriptionid = Integer.parseInt(prescriptionid1);
      }
      String patientname = request.getParameter("patientname");
      String starttime = request.getParameter("starttime");
      String endtime = request.getParameter("endtime");
      List<Prescription> prescriptions = new ArrayList<Prescription>();
      prescriptions = sm.queryDao(prescriptionid, patientid, patientname, starttime, endtime);
      String title = "发药信息表";
      String[] rowsName = new String[] { "处方号", "病历号", "姓名", "日期", "状态" };
      List<Object[]> dataList = new ArrayList<Object[]>();
      //导出excel
      Object[] objs = null;
      for (int i = 0; i < prescriptions.size(); i++) {
        Prescription regRum = prescriptions.get(i);
        objs = new Object[rowsName.length];
        objs[0] = regRum.getPrescriptionid();
        objs[1] = regRum.getPatientid();
        objs[2] = regRum.getName();
        objs[3] = regRum.getPrescriptiondate();
        //对数字的操作
        if(regRum.getStatus().equals("1")) {
          objs[4] = "已结算";
        }
        else {
          objs[4]="已发药";
        }
        dataList.add(objs);
      }
      //下面代码主要跟上面一致

10.数据库操作代码如下:(比较简单,只是把满足条件的list集合传到servlet,转变为数组,方便调用poi类)

@Override
  public List<Prescription> query(int prescriptionid,int patientid, String patientname, String starttime, String endtime, int pageon) {
    Connection con = Jndi.getConnection();
    ResultSet rs = null;
    PreparedStatement ps = null;
    String sql = "select p.patientid,p.prescriptionid,r.patientname, p.diagnose ,p.prescriptiondate,sum(chargemoney*number),p.`pstatus` \r\n"
        + "from prescription p,regnum r,chargeitem c,prescriptioncharge pc  \r\n" + "\r\n"
        + " where  p.patientid=r.patientid and pc.prescriptionid=p.prescriptionid\r\n" + "\r\n" + "\r\n"
        + " and c.chargeid=pc.chargeid   ";
    if (patientid != 0) {
      sql = sql + " and p.patientid=" + patientid;
    }
    if (prescriptionid != 0) {
      sql = sql + " and p.prescriptionid=" + prescriptionid;
    }
    if (patientname != null && !patientname.equals("")) {
      sql = sql + " and r.patientname like '%" + patientname + "%'";
    }
    if (starttime != null && !starttime.equals("")) {
      sql = sql + " and p.prescriptiondate>'" + starttime + "'";
    }
    if (endtime != null && !endtime.equals("")) {
      sql = sql + " and p.prescriptiondate<'" + endtime + "'";
    }
sql = sql + "  GROUP BY p.prescriptionid";
if (pageon != 0) {
  sql = sql + " limit " + (pageon - 1) * 4 + ",4";
}
List<Prescription> listregs = new ArrayList<Prescription>();
try {
  ps = con.prepareStatement(sql);
  rs = ps.executeQuery();
  while (rs.next()) {
    Prescription prescription = new Prescription();
    prescription.setPatientid(rs.getInt("patientid"));
    prescription.setPrescriptionid(rs.getInt("p.prescriptionid"));
    prescription.setDiagnose(rs.getString("diagnose"));
    prescription.setPrescriptiondate(rs.getString("prescriptiondate"));
    prescription.setStatus(rs.getString("pstatus"));
    prescription.setSummoney(rs.getDouble(6));
    prescription.setName(rs.getString(3));
    listregs.add(prescription);
  }
} catch (SQLException e) {
  e.printStackTrace();
} finally {
  Jndi.close(rs, ps, con);
}
return listregs;

第一种比较复杂,如果想很快搞定,可以用纯js在前端导出table表格,需要导入四个js文件。引入进去。


二、纯js实现前台导出excel。

1、导入js文件,可能也需要导入jquery文件,自行尝试。所需js文件地址。

链接:https://pan.baidu.com/s/14-riXUTElxWaLHRHLe04SA

提取码:fpnp

<script type="text/javascript" src="<%=path %>/Js/jszip.min.js"></script>
<script type="text/javascript" src="<%=path %>/Js/demo.page.js"></script>
<script type="text/javascript" src="<%=path %>/Js/excel-gen.js"></script>
<script type="text/javascript" src="<%=path %>/Js/FileSaver.js"></script>

2、js代码如下

<script type="text/javascript">
  $(document).ready(function(){
    alert()
    excel = new ExcelGen({
      "src_id":"test_table",//table的id
      "show_header":true
    });
    $("#generate-excel").click(function () {
      excel.generate();//执行导入包中的方法。
    })
  });
</script>

3、body中主要代码

<table class="table table-bordered table-hover definewidth m10" id="test_table">
//id在table中写
//调用方法名在button写
<button type="button" class="btn btn-success" id="generate-excel">导出Excel</button>

注意:第二种方式,只能导出当前页的信息,它是根据table里面tr的数量导的,不会打印下一页。如果希望打印下一页的内容,提供一种思路,可以在按钮上添加跳转页面,跳到另一个页面,让另一个页面返回数据库全查需要的数据,写入新建页的table中,打印新table,有兴趣的可以尝试。 到这儿,可能读累了吧,听懂的给个赞,thanks.


目录
相关文章
|
2天前
|
Java
java导出复杂excel
java导出复杂excel
|
1天前
|
Java
java的excel列行合并模版
java的excel列行合并模版
|
1天前
|
JavaScript
vue导出excel无法打开问题
vue导出excel无法打开问题
|
1天前
|
Java
java导出word
java导出word
|
2天前
|
easyexcel BI
excel合并列导出文件
excel合并列导出文件
|
2天前
|
前端开发
基于jeecgboot的flowable流程任务excel导出功能
基于jeecgboot的flowable流程任务excel导出功能
10 1
|
6天前
|
Java Apache
Java代码使用POI导出的单元格加上边框和背景色
【5月更文挑战第3天】Java代码使用POI导出的单元格加上边框和背景色
22 0
|
6天前
|
Java Apache
Java代码使用POI导出的单元格的字体加粗设置
【5月更文挑战第3天】Java代码使用POI导出的单元格的字体加粗设置
25 1
|
10天前
|
JSON JavaScript 前端开发
使用JavaScript和XLSX.js将数据导出为Excel文件
使用JavaScript和XLSX.js将数据导出为Excel文件
21 0
|
15天前
|
Java Apache
java读取excel数据案例
Java代码示例使用Apache POI库读取Excel(example.xlsx)数据。创建FileInputStream和XSSFWorkbook对象,获取Sheet,遍历行和列,根据单元格类型(STRING, NUMERIC, BOOLEAN)打印值。需引入Apache POI库并确保替换文件路径。
11 1