java导出excel java操作文件、文件夹 java制作zip

  1. /** 
  2.      * 导出老师信息 
  3.      */  
  4.     public static boolean exportTeach(String filePath, String teachName,  
  5.             String grade, String classes, String subject) {  
  6.   
  7.         // 第一步,创建一个webbook,对应一个Excel文件  
  8.         HSSFWorkbook wb = new HSSFWorkbook();  
  9.         // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet  
  10.         HSSFSheet sheet = wb.createSheet("老师信息");  
  11.         // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
  12.         HSSFRow row = sheet.createRow((int0);  
  13.   
  14.         HSSFCell cell = row.createCell(0);  
  15.         for (int s = 0; s < 4; s++) {  
  16.             cell = row.createCell(s);  
  17.             if (s == 0) {  
  18.                 cell.setCellValue("老师名字");  
  19.             } else if (s == 1) {  
  20.                 cell.setCellValue("年级");  
  21.             } else if (s == 2) {  
  22.                 cell.setCellValue("班级");  
  23.             } else if (s == 3) {  
  24.                 cell.setCellValue("科目");  
  25.             }  
  26.         }  
  27.   
  28.         // 第五步,写入实体数据 实际应用中这些数据从数据库得到,  
  29.         row = sheet.createRow(1);  
  30.   
  31.         for (int j = 0; j < 4; j++) {  
  32.   
  33.             cell = row.createCell(j);  
  34.             if (j == 0) {  
  35.                 cell.setCellValue(teachName);  
  36.             } else if (j == 1) {  
  37.                 cell.setCellValue(grade);  
  38.             } else if (j == 2) {  
  39.                 cell.setCellValue(classes);  
  40.             } else if (j == 3) {  
  41.                 cell.setCellValue(subject);  
  42.             }  
  43.   
  44.         }  
  45.         // 第六步,将文件存到指定位置  
  46.         try {  
  47.             if (createDir(filePath + "/teach")) {  
  48.                 FileOutputStream fout = new FileOutputStream(filePath  
  49.                         + "/teach/teach.xls");  
  50.                 wb.write(fout);  
  51.                 fout.close();  
  52.                 return true;  
  53.             } else {  
  54.                 return false;  
  55.             }  
  56.         } catch (Exception e) {  
  57.             e.printStackTrace();  
  58.             return false;  
  59.         }  
  60.     }  
  61.   
  62.     /** 
  63.      * @see 导出备课题目信息 
  64.      * @param filePath 文件路径 
  65.      * @param problemList 备课题目信息 
  66.      */  
  67.     public static boolean exportTopic(String filePath, List<Problem> problemList) {  
  68.   
  69.         // 第一步,创建一个webbook,对应一个Excel文件  
  70.         HSSFWorkbook wb = new HSSFWorkbook();  
  71.         // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet  
  72.         HSSFSheet sheet = wb.createSheet("备课题目信息");  
  73.         // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
  74.         HSSFRow row = sheet.createRow((int0);  
  75.   
  76.         HSSFCell cell = row.createCell(0);  
  77.         for (int s = 0; s < 6; s++) {  
  78.             cell = row.createCell(s);  
  79.             if (s == 0) {  
  80.                 cell.setCellValue("题目内容");  
  81.             } else if (s == 1) {  
  82.                 cell.setCellValue("答案");  
  83.             } else if (s == 2) {  
  84.                 cell.setCellValue("所属科目");  
  85.             } else if (s == 3) {  
  86.                 cell.setCellValue("知识点");  
  87.             } else if (s == 4) {  
  88.                 cell.setCellValue("所属章");  
  89.             } else if (s == 5) {  
  90.                 cell.setCellValue("所属节");  
  91.             }  
  92.         }  
  93.   
  94.         // 第五步,写入实体数据 实际应用中这些数据从数据库得到,  
  95.         if (problemList != null & problemList.size() > 0) {  
  96.             int s = 0;  
  97.             for (int i = 0; i < problemList.size(); i++) {  
  98.                 Problem problem = (Problem) problemList.get(i);  
  99.                 row = sheet.createRow(++s);  
  100.   
  101.                 for (int j = 0; j <= 5; j++) {  
  102.                     cell = row.createCell(j);  
  103.                     if (j == 0) {  
  104.                         cell.setCellValue(problem.getContent());  
  105.                     } else if (j == 1) {  
  106.                         cell.setCellValue(problem.getAnswer());  
  107.                     } else if (j == 2) {  
  108.                         cell.setCellValue(problem.getSubjects().getName());  
  109.                     } else if (j == 3) {  
  110.                         cell.setCellValue(problem.getKnowledgePoints()  
  111.                                 .getKnowledgeContent());  
  112.                     } else if (j == 4) {  
  113.                         cell.setCellValue(problem.getKnowledgePoints()  
  114.                                 .getRemark2());  
  115.                     } else if (j == 5) {  
  116.                         cell.setCellValue(problem.getKnowledgePoints()  
  117.                                 .getRemark3());  
  118.                     }  
  119.                 }  
  120.             }  
  121.   
  122.         }  
  123.   
  124.         // 第六步,将文件存到指定位置  
  125.         try {  
  126.             if (createDir(filePath + "/problem")) {  
  127.                 FileOutputStream fout = new FileOutputStream(filePath  
  128.                         + "/problem/problem.xls");  
  129.                 wb.write(fout);  
  130.                 fout.close();  
  131.                 return true;  
  132.             } else {  
  133.                 return false;  
  134.             }  
  135.         } catch (Exception e) {  
  136.             e.printStackTrace();  
  137.             return false;  
  138.         }  
  139.     }  
  140.   
  141.     /** 
  142.      * @see 创建文件夹 
  143.      */  
  144.     public static boolean createDir(String destDirName) {  
  145.         File dir = new File(destDirName);  
  146.         if (dir.exists()) {  
  147.             System.out.println("创建目录" + destDirName + "失败,已经存在!!");  
  148.         }  
  149.         if (!destDirName.endsWith(File.separator)) {  
  150.             destDirName = destDirName + File.separator;  
  151.         }  
  152.         // 创建单个目录  
  153.         if (dir.mkdirs()) {  
  154.             System.out.println("创建成功");  
  155.             return true;  
  156.         } else {  
  157.             System.out.println("创建失败!!");  
  158.             return false;  
  159.         }  
  160.     }  
  161.   
  162.     /** 
  163.      * 删除某个文件夹下的所有文件夹和文件 
  164.      *  
  165.      * @param delpath 
  166.      *            String 
  167.      * @throws FileNotFoundException 
  168.      * @throws IOException 
  169.      * @return boolean 
  170.      */  
  171.     public static boolean deletefile(String delpath) throws Exception {  
  172.         try {  
  173.   
  174.             File file = new File(delpath);  
  175.             // 当且仅当此抽象路径名表示的文件存在且 是一个目录时,返回 true  
  176.             if (!file.isDirectory()) {  
  177.                 file.delete();  
  178.             } else if (file.isDirectory()) {  
  179.                 String[] filelist = file.list();  
  180.                 for (int i = 0; i < filelist.length; i++) {  
  181.                     File delfile = new File(delpath + "\\" + filelist[i]);  
  182.                     if (!delfile.isDirectory()) {  
  183.                         delfile.delete();  
  184.                         System.out  
  185.                                 .println(delfile.getAbsolutePath() + "删除文件成功");  
  186.                     } else if (delfile.isDirectory()) {  
  187.                         deletefile(delpath + "\\" + filelist[i]);  
  188.                     }  
  189.                 }  
  190.                 System.out.println(file.getAbsolutePath() + "删除成功");  
  191.                 file.delete();  
  192.             }  
  193.   
  194.         } catch (FileNotFoundException e) {  
  195.             System.out.println("deletefile() Exception:" + e.getMessage());  
  196.         }  
  197.         return true;  
  198.     }  
  199.   
  200.     /** 
  201.      * 创建ZIP文件 
  202.      *  
  203.      * @param sourcePath 
  204.      *            文件或文件夹路径 
  205.      * @param zipPath 
  206.      *            生成的zip文件存在路径(包括文件名) 
  207.      */  
  208.     public static void createZip(String sourcePath, String zipPath) {  
  209.         FileOutputStream fos = null;  
  210.         ZipOutputStream zos = null;  
  211.         try {  
  212.             fos = new FileOutputStream(zipPath);  
  213.             zos = new ZipOutputStream(fos);  
  214.             writeZip(new File(sourcePath), "", zos);  
  215.         } catch (FileNotFoundException e) {  
  216.             System.out.println(("创建ZIP文件失败"));  
  217.         } finally {  
  218.             try {  
  219.                 if (zos != null) {  
  220.                     zos.close();  
  221.                 }  
  222.             } catch (IOException e) {  
  223.                 System.out.println(("创建ZIP文件失败"));  
  224.             }  
  225.   
  226.         }  
  227.     }  
  228.   
  229.     /** 
  230.      * 创建zip压缩包 
  231.      *  
  232.      * @param file 
  233.      * @param parentPath 
  234.      * @param zos 
  235.      */  
  236.     private static void writeZip(File file, String parentPath,  
  237.             ZipOutputStream zos) {  
  238.         zos.setEncoding("gbk");  
  239.         if (file.exists()) {  
  240.             if (file.isDirectory()) {// 处理文件夹  
  241.                 parentPath += file.getName() + File.separator;  
  242.                 File[] files = file.listFiles();  
  243.                 for (File f : files) {  
  244.                     zos.setEncoding("gbk");  
  245.                     writeZip(f, parentPath, zos);  
  246.                 }  
  247.             } else {  
  248.                 FileInputStream fis = null;  
  249.                 DataInputStream dis = null;  
  250.                 try {  
  251.                     fis = new FileInputStream(file);  
  252.                     dis = new DataInputStream(new BufferedInputStream(fis));  
  253.                     ZipEntry ze = new ZipEntry(parentPath + file.getName());  
  254.                     zos.putNextEntry(ze);  
  255.                     zos.setEncoding("gbk");  
  256.                     byte[] content = new byte[1024];  
  257.                     int len;  
  258.                     while ((len = fis.read(content)) != -1) {  
  259.                         zos.write(content, 0, len);  
  260.                         zos.flush();  
  261.                     }  
  262.   
  263.                 } catch (FileNotFoundException e) {  
  264.                     System.out.println(("创建ZIP文件失败"));  
  265.                 } catch (IOException e) {  
  266.                     System.out.println(("创建ZIP文件失败"));  
  267.                 } finally {  
  268.                     try {  
  269.                         if (dis != null) {  
  270.                             dis.close();  
  271.                         }  
  272.                     } catch (IOException e) {  
  273.                         System.out.println(("创建ZIP文件失败"));  
  274.                     }  
  275.                 }  
  276.             }  
  277.         }  
  278.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值