用excel2007直接画出表格,然后拖拽到项目对应的目录下,之后直接引用这个路径,或者用框架封装的配置文件去调用这个文件。

方法一、

Excel 2007版本的(后缀.xlsx)  用 XSSFWorkbook这个poi 方法一

Excel 2003版本的(后缀名是.xls) 用HSSFWorkbook这个poi  方法二

这两个方法都用了ExcelExpUtils---框架封装好的,方法三自己设置的Excel

@Overridepublic String getXDSJCDL_Excel(List<String> a0000s, EPrintConfig tpid, EPrintType type, String ids, List<String> result, String userId){//这里是获取文件  框架封装的用配置文件调用的,如果自己直接获取文件参考方法二  String expFile = DownFileDirUtil.getTempFilePath(EHyFile.DOWNLOAD).getAbsolutePath();String expName = "选调生基层锻炼考核评定表";File file = new File(expFile + "/" + expName + Consiants.Excel_XLSX);ArrayList<Map<String, Object>> list = new ArrayList<>();// 根据字段查数据库的 这里是直接调用sql 返回全部字符串  可以用方法二 自己拼接sql 返回不同的实体类对象,在取值List<Map<String, Object>> maps = dao.getMcListByXdsjcdlkhpd(ids);ArrayList<String[]> listArr = new ArrayList<>();int y=0;int n=1;for (Map<String, Object> tmp : maps) {//循环主键tmp.put("num",n++);//序号暂时没用String[] arr = {"A0101", tmp.get("a0101") + "", Consiants.String_Null, "第" + (y++) + "人员"};listArr.add(arr);list.add(tmp);}try {FileInputStream in=new FileInputStream(this.getRootPath()+tpid.name);XSSFWorkbook wook=new XSSFWorkbook(in);XSSFSheet sheet=wook.getSheetAt(0);if(list.size()>0) {for(int i=0;i<list.size();i++) {//对列做操作转换成xml,下面就可以直接设值XSSFRow row = sheet.createRow(i+4);//前4行无数据for (int j=0; j< 14;j++){row.createCell(j);}//姓名  给对应的列插入值 字段名和sql查出的字段名相同row.getCell(0).setCellValue(list.get(i).get("a0101").toString());if(list.get(i).get("a0192")!=null){//防止空指针row.getCell(1).setCellValue(list.get(i).get("a0192").toString());} if(list.get(i).get("a0104")!=null){row.getCell(2).setCellValue(list.get(i).get("a0104").toString());}}//输出Excel文件FileOutputStream output = new FileOutputStream(file);wook.write(output);wook.close();} catch (Exception e) {e.printStackTrace();} finally {}return file.getParent();}

方法二、

    @Override@Transactionalpublic String ExportDXXDExcel(JSONObject pagedata,String userId,String xdsRoleResult) throws IOException {Session session = entityManager.unwrap(org.hibernate.Session.class);//根据路径获取文件File file = new File("./xdsdxxd.xls");String rootpath = StringUtils.substringBefore(file.getCanonicalPath(),"xdsdx") + "target\\classes\\static\\exportexcel\\";FileInputStream in=new FileInputStream(rootpath+"xdsdxxd.xls");//Excel 2003 版本的HSSFWorkbook wook=new HSSFWorkbook(in);HSSFSheet sheet=wook.getSheetAt(0);//用HBsession获取数据 封装成对象 Query query = session.createQuery("from SysUser s where s.userId = ?");query.setParameter(0,userId);List<SysUser> list = query.list();Query query1 = session.createQuery("from XA01 x where x.b0111 = ?");query1.setParameter(0,list.get(0).getOtherinfo());List<XA01> list2 = query1.list();String b0114 = "";for (int i =0;i<list2.size();i++){if (!list2.get(i).getB0114().equals(null))b0114 = list2.get(i).getB0114(); break;}sb.append("and x.x0196='1' and substr(x.x0198,0,4) = '2022' order by  x.b0111 asc,x.x0102 asc   ");//执行这个拼接好的sql  这里面都是一个表的字段Query query2 = session.createQuery(sb.toString());List<XA01> listResult = query2.list();String path = null;if(listResult.size()>0) {for(int i=0;i<listResult.size();i++) {HSSFRow row = sheet.createRow(i+5);for (int j=0; j< 13;j++){row.createCell(j);}//单位名称row.getCell(0).setCellValue(listResult.get(i).getX0101());//职位row.getCell(1).setCellValue(listResult.get(i).getX0102());//计划数row.getCell(2).setCellValue(listResult.get(i).getX0103());//学历要求String xl= listResult.get(i).getX0104();switch (xl){//不是设值数值 是√case "001": row.getCell(3).setCellValue("√");break;case "002": row.getCell(4).setCellValue("√");break;case "003": row.getCell(5).setCellValue("√");}row.getCell(9).setCellValue("0".equals(listResult.get(i).getX0106())?"否":"是");row.getCell(10).setCellValue(listResult.get(i).getX0107());row.getCell(11).setCellValue(listResult.get(i).getX0108());row.getCell(12).setCellValue(listResult.get(i).getX0109());}String xdsExcel = StringUtils.substringBefore(file.getCanonicalPath(),"xdsd")+"temp\\download_file\\XDSJHSB\\";File xdsFile = new File(xdsExcel);if(!xdsFile.exists() || !xdsFile.isDirectory()){xdsFile.mkdirs();}path = xdsExcel+UUID.randomUUID()+".xls";FileOutputStream fileOutputStream = new FileOutputStream(path);wook.write(fileOutputStream);fileOutputStream.close();return path;}return "";}

方法三、

获取文件比较长,没有用ExcelUptil  ,自己设置excel格式

方法分离,没把列转换成xml,sql没有分开写

@SuppressWarnings("unused")@PageEvent("ExpGird")public int ExpGird(String param) throws RadowException{param = this.request.getParameter("param");;FileInputStream fis = null;FileOutputStream fos = null;FileInputStream is = null;try{String path = FileUtil.getPath4Excelload();File file1=new File(path);if  (!file1 .exists()  && !file1 .isDirectory())      {       file1 .mkdirs();    }String pathdata=path+"个人重大事项汇总表.xls";String ss=File.separatorChar+"";pathdata=pathdata.replace("\\", ss);pathdata=pathdata.replace("/", ss);File file2=new File(pathdata);if(!file2.exists()||!file2.isFile()){file2.createNewFile();}///gbdjdsys/WebContent/pages/grzdsx/grzdsxcxlb.xlsString mbpath=this.request.getSession().getServletContext().getRealPath("/")+"pages\\grzdsx\\个人重大事项汇总表.xls";mbpath=mbpath.replace("\\", ss);mbpath=mbpath.replace("/", ss);File file =  new File(mbpath);fis = new FileInputStream(file);String file_copy=pathdata;fos = new FileOutputStream(file_copy);byte[] buf = new byte[1024*10];int len = -1;while((len = fis.read(buf)) != -1){fos.write(buf, 0, len);}fis.close();fos.close();is=new FileInputStream(file_copy);@SuppressWarnings("resource")Workbook workbook = new HSSFWorkbook(is);      //默认读取2003版的Excelint sheetNums=workbook.getNumberOfSheets();Font font2 =workbook.createFont();  font2.setFontName("仿宋_GB2312");  font2.setFontHeightInPoints((short) 11);  CellStyle cellstyle=workbook.createCellStyle(); cellstyle.setAlignment(HorizontalAlignment.CENTER);        cellstyle.setVerticalAlignment(VerticalAlignment.CENTER);  cellstyle.setBorderLeft(BorderStyle.THIN);                 cellstyle.setBorderRight(BorderStyle.THIN);                cellstyle.setBorderLeft(BorderStyle.THIN);                 cellstyle.setBorderBottom(BorderStyle.THIN);cellstyle.setFont(font2);cellstyle.setWrapText(true);Sheet sheet = null;sheet=workbook.getSheetAt(0);exportData(sheet,workbook, cellstyle,param.replace("^", "'"));                fos = new FileOutputStream(file_copy);workbook.write(fos);     fos.flush();fos.close();workbook.close();System.gc();       //压缩文件String zippath=path+"个人重大事项汇总表.xls";this.setSelfDefResData("2@@@"+zippath);return EventRtnType.NORMAL_SUCCESS;}catch(Exception e){e.printStackTrace();throw new RadowException(e.getMessage());}finally{try{if(fis!=null){fis.close();}if(fos!=null){fos.close();}if(is!=null){is.close();}}catch(Exception e){e.printStackTrace();}}}
/*** 写入数据* @param sheet* @param workbook* @param cellstyle* @param grzdsxid* @throws Exception*/public void exportData(Sheet sheet,Workbook workbook,CellStyle cellstyle,String grzdsxid) throws Exception{try{CommQuery cq=new CommQuery();//selec DOCNO,to_char(ACCEPTDATE,'yyyymmdd') ACCEPTDATE,ACCEPTDEPT,REPORTERNAME,REPORTERDUTY,UNITTYPE,(select code_name from code_value where code_type='ZDSX001' and code_value=UNITTYPE) UNITTYPENAME,ITEMTYPE,(select code_name from code_value where code_type='ZDSX002' and code_value=ITEMTYPE) ITEMTYPENAME,CONTENT,to_char(AGGRPROCTIME,'yyyymmdd') AGGRPROCTIME,APPROVELEADER,PISHICONTENT,to_char(APPROVETIME,'yyyymmdd') APPROVETIME,MEMO from ZDSX_PERSONALRPTString sql="select DOCNO,to_char(ACCEPTDATE,'yyyymmdd') ACCEPTDATE,ACCEPTDEPT,REPORTERNAME, "+ "(select code_name from code_value where code_type='ZDSX001' and code_value=UNITTYPE) UNITTYPE,REPORTERDUTY, "+ "(select code_name from code_value where code_type='ZDSX002' and code_value=ITEMTYPE) ITEMTYPE, "+ "CONTENT,to_char(AGGRPROCTIME,'yyyymmdd') AGGRPROCTIME,APPROVELEADER,PISHICONTENT,to_char(APPROVETIME,'yyyymmdd') APPROVETIME,MEMO "+ " from ZDSX_PERSONALRPT where oid in ( "+grzdsxid+") order by DOCNO desc";            System.out.println(sql);List<HashMap<String, Object>>  list=cq.getListBySQL(sql);Cell cell=null;HashMap<String, Object> map=null;for(int i=0;i<list.size();i++){             map=list.get(i);Row row=sheet.createRow(i+2);row.setHeight((short)(25*20));int m=0;cell=row.createCell(m++);cell.setCellStyle(cellstyle);cell.setCellValue(i+1);  cell=row.createCell(m++);cell.setCellStyle(cellstyle);cell.setCellValue((String)map.get("docno"));  cell=row.createCell(m++);cell.setCellStyle(cellstyle);cell.setCellValue((String)map.get("acceptdate")); cell=row.createCell(m++);cell.setCellStyle(cellstyle);cell.setCellValue((String)map.get("acceptdept"));  cell=row.createCell(m++);cell.setCellStyle(cellstyle);cell.setCellValue((String)map.get("reportername"));}}catch(Exception e){e.printStackTrace();throw new Exception(e.getMessage());}}

导出Excel三种方法相关推荐

  1. [转]asp.net导出数据到Excel的三种方法

    原文出处:asp.net导出数据到Excel的几种方法(1/3) .asp.net导出数据到Excel的几种方法(2/3).asp.net导出数据到Excel的几种方法(3/3) asp.net导出到 ...

  2. oracle数据迁移过程中,把表中数据导出为txt文件的三种方法

    在数据迁过程中需求需要将oracle数据库数据导出程txt格式然后再导入db2库中,经查询实验汇总三种方法: 1.plsqldev 里面有一个选项可以把表以excel格式到时 2.使用spool sq ...

  3. C#实现将excel转换成pdf的三种方法

    文章目录 1.使用免费版的spire 2.使用Microsoft.Office.Interop.Excel 3.使用Aspose.Cells 本人经过一上午的搜索,总结了C#将excel转pdf的三种 ...

  4. 数据库数据用Excel导出的3种方法

    数据库数据用Excel导出的3种方法 分类: .Net 2008-06-30 11:07 173人阅读 评论(2) 收藏 举报 将数据库数据用Excel导出主要有3种方法:用Excel.Applica ...

  5. 计算机启动应用程序的方法,excel的程序_Excel2010中启动应用程序的三种方法

    使用Excel时,需要先启动应用程序,怎么去进行操作启动它?今天,学习啦小编就教大家在Excel2010中启动应用程序的三种方法. Excel2010中启动应用程序的三种步骤如下: 1.开始菜单 在桌 ...

  6. 怎么把Excel转换成PDF格式?这三种方法轻松完成转换

    如何将excel表格转换成PDF格式呢?大家在日常的办公过程中也会经常使用这两种文件格式,对文件的格式转换需求我们也是屡见不鲜,其中就有把excel表格转换成PDF格式的需求,遇到这种情况该怎么转换呢 ...

  7. excel添加列下拉框票价_在Excel中制作下拉列表的三种方法

    下拉列表在Excel中的用途十分广泛.在Excel中制作下拉列表可以通过数据有效性.使用窗体控件和VBA控件工具箱中的组合框来制作.下面用一个具体的例子来进行说明(在文章结尾处可下载xls格式的示例文 ...

  8. excel 删除空白列的三种方法,以及如何批量删除空行空列

    详见:excel删除空白列的三种方法,以及如何批量删除空行空列 本人更倾向于第三种方法: excel删除空白列方法三: 按ALT+F11,打开VBE编辑器,插入--模块,复制下面代码,然后按F5键,运 ...

  9. Excel批量在单元格前添加文本的三种方法

    Excel批量在单元格前添加文本的三种方法 在日常工作中我们可能要批量给单元格添加文本,下面就给大家介绍一下添加文本的三种方法. 方法一. 1.如下图,是某老师录入的学生成绩表,但是他在录入忘了录入年 ...

最新文章

  1. 奇异值的物理意义是什么?
  2. 机器学习算法与Python实践之(一)k近邻(KNN)
  3. Qt Creator使用Clang代码模型解析C ++文件
  4. shell 学习笔记(四)
  5. GraphQL的query只返回所请求的字段的实现原理
  6. MySQL 5.6 for Windows 解压缩版配置安装
  7. mysql增加布尔字段_JDBC对MySQL数据库布尔字段的操作方法
  8. Document API
  9. php发送邮件时间间隔,在使用phpmailer群发邮件时如何设置发送的时间间隔?
  10. extjs Grid(一)
  11. 计算机设计思想 —— 类比、建模与隐喻(同构)
  12. python opencv 利用分水岭算法实现对物体的分割 图文详细注释版 以分割官网提供的硬币为例
  13. 打造自己的数据访问层(一)
  14. dfs.datanode.directoryscan.throttle.limit.ms.per.
  15. console.log、toString方法与js判断变量类型
  16. C语言_字符串转数字
  17. 交通部 808 协议2019版 - 免费下载 - 官网下载
  18. WPF3D贴图:用C#搓一个地球
  19. Hadoop Yarn 安装
  20. ConcurrentHashMap源码深度解析(一)(java8)——不可不知的基本概念(助你拿下源码事半功倍)

热门文章

  1. ReiserFS 与LVM
  2. 【07 响应(response) 学习笔记】
  3. nginx反向代理及负载均衡使用详解
  4. [python3 实用教程]使用ctypes模块调用运行c代码
  5. 抽象数据类型Triplet的表示与实现
  6. PostgreSQL基础语法
  7. 【docker】docker的安装教程
  8. 音质出众通话降噪,2020五款高性价比蓝牙耳机测评推荐
  9. InfluxDB 数据备份与恢复
  10. DDD实战与进阶 - 值对象