zoukankan      html  css  js  c++  java
  • java(POI):基于模版的Excel导出功能,局部列写保护总结

    需求描述:

      1.导出的Excel中部分列包含有下拉列表,并没有尝试过用代码实现这种功能,个人感觉比较棘手,故采用了模版的形式,直接导出数据到已经创建好的Excel模版中

      2.Excel的第一列需要写保护,不允许用户修改

      3.每一个大类导出到以大类名称命名的Excel中,连同各个对象对应的附件一同拷贝到同Excel的相同目录,然后压缩,下载

      4.下载完成后删除该压缩文件,避免垃圾文件过多

    	public String doDownloadDatas() {
    		String fileStr = null;
    		String ms = String.valueOf(System.currentTimeMillis());//以时间戳创建一个临时文件夹
    		try {// 获取指定的文件
    			UserManager userManager = (UserManager)SpringHelper.getBean("userManager");//获取当前登录人信息
    			UserDTO user = userManager.getCurrentUserInfo();
    			String orgCode = user.getOrgCode();//"ORG00002";// 当前用户所属组织编码
    			String orgName = user.getOrgName();//"勘探与生产分公司";// 当前用户所属组织名称
    			List<PrimaryAllot> dataList = this.getMaterialKinds(orgCode);
    			if (dataList != null && dataList.size() > 0) {
    				for (int i = 0; i < dataList.size(); i++) {
    					PrimaryAllot pa = dataList.get(i);
    					String materialName = pa.getMaterialName();
    					String materialCode = pa.getMaterialCode();
    					// 获取指定的模版文档
    					// Java Excel API既可以从本地文件系统的一个文件(.xls),也可以从输入流中读取Excel数据表。
    					// 构建Workbook对象, 只读Workbook对象
    					// 直接从本地文件创建Workbook
    					// 从输入流创建Workbookx
    //					String filePath = this.getClass().getResource("/conf/standartInformationReport.xls").getPath();
                               //获取文件路径,该方法只在windows环境下好使,但是部署到linux服务器上时报FileNotFoundException // FileInputStream fs=new FileInputStream(filePath); //                         因此修改成如下的方式,可以在Linux和Windows系统中获取到该文件的路径 InputStream fs=this.getClass().getClassLoader().getResourceAsStream("conf/standartInformationReport.xls"); HSSFWorkbook wb=new HSSFWorkbook(fs); // wb.writeProtectWorkbook("pmsapp", "pmsapp");//写保护操作,下载下来以后,打开Excel文件时所用的 用户名密码都是“pmsapp” HSSFSheet sheet=wb.getSheetAt(1); //System.out.println(sheet.getLastRowNum()+" "+row.getLastCellNum()); //创建临时目录 String key = "file.root"; String root = PMSPropertyUtil.getValueOfProperties(key); String tempPath = root + File.separator+"tempprimaryfile" + File.separator + ms + File.separator; File tempFile = new File(tempPath); if (!tempFile.exists()) { tempFile.mkdirs(); } FileOutputStream out=new FileOutputStream(tempPath+orgName+"_"+materialName+".xls"); // List objList = this.getDatas(orgCode,materialCode); //这个是从数据库中取得要导出的数据 // int m = 4;//从第五行、第一列开始写入数据 // for (int j = 0; j < objList.size(); j++) { // HSSFRow row=sheet.getRow(m); // Object[] objs=(Object[])objList.get(j); // for (int n = 0; n<19; n++) { // row.getCell((short)n).setCellValue(objs[n]==null||"null".equals(objs[n])?"":objs[n].toString()); // } // m++; // } List<StandardChooseAdvise> objList = this.getDatas(orgCode,materialCode); //这个是从数据库中取得要导出的数据 int m = 4;//从第五行、第一列开始写入数据 for (int j = 0; j < objList.size(); j++) { HSSFRow row=sheet.createRow(m); CellStyle locked = wb.createCellStyle(); locked.setLocked(true); StandardChooseAdvise objs=(StandardChooseAdvise)objList.get(j); for (int n = 0; n<19; n++) { row.createCell((short)0).setCellValue(objs.getId()==null||"null".equals(objs.getId())?"":objs.getId().toString());//序号 row.getCell((short)0).setCellStyle(locked);//锁定该表格 row.createCell((short)1).setCellValue(objs.getItem().getNodeCode()==null||"null".equals(objs.getItem().getNodeCode())?"":objs.getItem().getNodeCode());//物料分类 row.createCell((short)2).setCellValue(objs.getItem().getNodeName()==null||"null".equals(objs.getItem().getNodeName())?"":objs.getItem().getNodeName());//物料分类描述 String standardSort = objs.getStandardSort()==null||"null".equals(objs.getStandardSort())?"":objs.getStandardSort(); if(standardSort.length()>0){ standardSort = ""; }else if ("00001".equals(standardSort)) { standardSort="国际标准"; }else if ("00002".equals(standardSort)) { standardSort="国外先进标准"; }else if ("00003".equals(standardSort)) { standardSort="国家标准"; }else if ("00004".equals(standardSort)) { standardSort="行业标准"; }else if ("00005".equals(standardSort)) { standardSort="地方标准"; }else if ("00006".equals(standardSort)) { standardSort="集团公司企业标准"; }else if ("00007".equals(standardSort)) { standardSort="外部企业标准"; }else if ("00008".equals(standardSort)) { standardSort="内部企业订货条件及技术规格书"; } row.createCell((short)3).setCellValue(standardSort);//标准信息标准分类 row.createCell((short)4).setCellValue(objs.getStandardNO()==null||"null".equals(objs.getStandardNO())?"":objs.getStandardNO());//标准信息标准代号 row.createCell((short)5).setCellValue(objs.getStandardOrderNO()==null||"null".equals(objs.getStandardOrderNO())?"":objs.getStandardOrderNO());//标准信息标准顺序号 row.createCell((short)6).setCellValue(objs.getPublicNO()==null||"null".equals(objs.getPublicNO())?"":objs.getPublicNO());//标准信息发布年代号 row.createCell((short)7).setCellValue(objs.getStandardName()==null||"null".equals(objs.getStandardName())?"":objs.getStandardName());//标准信息标准名称 row.createCell((short)8).setCellValue(objs.getUsedStandardNO()==null||"null".equals(objs.getUsedStandardNO())?"":objs.getUsedStandardNO());//采标情况标准代号 row.createCell((short)9).setCellValue(objs.getUsedStandardOrderNO()==null||"null".equals(objs.getUsedStandardOrderNO())?"":objs.getUsedStandardOrderNO());//采标情况标准顺序号 row.createCell((short)10).setCellValue(objs.getUsedPublicNO()==null||"null".equals(objs.getUsedPublicNO())?"":objs.getUsedPublicNO());//采标情况发布年代号 row.createCell((short)11).setCellValue(objs.getUsedStandardName()==null||"null".equals(objs.getUsedStandardName())?"":objs.getUsedStandardName());//采标情况标准名称 row.createCell((short)12).setCellValue(objs.getUsedInTheDefree()==null||"null".equals(objs.getUsedInTheDefree())?"":objs.getUsedInTheDefree());//采标程度 row.createCell((short)13).setCellValue(objs.getOuterCompanyName()==null||"null".equals(objs.getOuterCompanyName())?"":objs.getOuterCompanyName());//外部企业名称 String haveRecode = objs.getHaveRecord()==null||"null".equals(objs.getHaveRecord())?"":objs.getHaveRecord(); if(haveRecode.length()>0){ haveRecode = ""; }else if ("00001".equals(haveRecode)) { haveRecode="是"; }else if ("00002".equals(haveRecode)) { haveRecode="否"; } row.createCell((short)14).setCellValue(haveRecode);//是否在当地技术监督部门备案 String outerAttachment = objs.getOuterAttachment()==null||"null".equals(objs.getOuterAttachment())?"":objs.getOuterAttachment(); if(outerAttachment.length()>0){ outerAttachment = ""; }else if ("00001".equals(outerAttachment)) { outerAttachment="是"; }else if ("00002".equals(outerAttachment)) { outerAttachment="否"; } row.createCell((short)15).setCellValue(outerAttachment);//外部企业标准附件 String hasAttr = objs.getHasAttr()==null||"null".equals(objs.getHasAttr())?"":objs.getHasAttr(); if(hasAttr.length()>0){ hasAttr = ""; }else if ("00001".equals(hasAttr)) { hasAttr="是"; }else if ("00002".equals(hasAttr)) { hasAttr="否"; } row.createCell((short)16).setCellValue(hasAttr);//内部技术规格书附件 row.createCell((short)17).setCellValue(objs.getBigorg()==null||"null".equals(objs.getBigorg())?"":objs.getBigorg());//提报单位 row.createCell((short)18).setCellValue(objs.getCountAdvice()==null||"null".equals(objs.getCountAdvice())?"":objs.getCountAdvice());//所属企业创编意见数量 } m++; }           sheet.protectSheet("123");//dui out.flush(); wb.write(out); out.close(); } fileStr = queryAllFiles(dataList,ms); } } catch (Exception e) { fileStr = "导出失败,请联系管理员!"; e.printStackTrace(); } return fileStr;//如果导出成功则为压缩包路径,否则为空值 }

     待续中……

  • 相关阅读:
    hdu 1455 N个短木棒 拼成长度相等的几根长木棒 (DFS)
    hdu 1181 以b开头m结尾的咒语 (DFS)
    hdu 1258 从n个数中找和为t的组合 (DFS)
    hdu 4707 仓鼠 记录深度 (BFS)
    LightOJ 1140 How Many Zeroes? (数位DP)
    HDU 3709 Balanced Number (数位DP)
    HDU 3652 B-number (数位DP)
    HDU 5900 QSC and Master (区间DP)
    HDU 5901 Count primes (模板题)
    CodeForces 712C Memory and De-Evolution (贪心+暴力)
  • 原文地址:https://www.cnblogs.com/lowerCaseK/p/3148872.html
Copyright © 2011-2022 走看看