/** * 匯出老師資訊 */public static boolean exportTeach(String filePath, String teachName,String grade, String classes, String subject) {// 第一步,建立一個webbook,對應一個Excel檔案HSSFWorkbook wb = new HSSFWorkbook();// 第二步,在webbook中添加一個sheet,對應Excel檔案中的sheetHSSFSheet sheet = wb.createSheet("老師資訊");// 第三步,在sheet中添加表頭第0行,注意老版本poi對Excel的行數列數有限制shortHSSFRow row = sheet.createRow((int) 0);HSSFCell cell = row.createCell(0);for (int s = 0; s < 4; s++) {cell = row.createCell(s);if (s == 0) {cell.setCellValue("老師名字");} else if (s == 1) {cell.setCellValue("年級");} else if (s == 2) {cell.setCellValue("班級");} else if (s == 3) {cell.setCellValue("科目");}}// 第五步,寫入實體資料 實際應用中這些資料從資料庫得到,row = sheet.createRow(1);for (int j = 0; j < 4; j++) {cell = row.createCell(j);if (j == 0) {cell.setCellValue(teachName);} else if (j == 1) {cell.setCellValue(grade);} else if (j == 2) {cell.setCellValue(classes);} else if (j == 3) {cell.setCellValue(subject);}}// 第六步,將檔案存到指定位置try {if (createDir(filePath + "/teach")) {FileOutputStream fout = new FileOutputStream(filePath+ "/teach/teach.xls");wb.write(fout);fout.close();return true;} else {return false;}} catch (Exception e) {e.printStackTrace();return false;}}/** * @see 匯出備課題目資訊 * @param filePath 檔案路徑 * @param problemList 備課題目資訊 */public static boolean exportTopic(String filePath, List<Problem> problemList) {// 第一步,建立一個webbook,對應一個Excel檔案HSSFWorkbook wb = new HSSFWorkbook();// 第二步,在webbook中添加一個sheet,對應Excel檔案中的sheetHSSFSheet sheet = wb.createSheet("備課題目資訊");// 第三步,在sheet中添加表頭第0行,注意老版本poi對Excel的行數列數有限制shortHSSFRow row = sheet.createRow((int) 0);HSSFCell cell = row.createCell(0);for (int s = 0; s < 6; s++) {cell = row.createCell(s);if (s == 0) {cell.setCellValue("題目內容");} else if (s == 1) {cell.setCellValue("答案");} else if (s == 2) {cell.setCellValue("所屬科目");} else if (s == 3) {cell.setCellValue("知識點");} else if (s == 4) {cell.setCellValue("所屬章");} else if (s == 5) {cell.setCellValue("所屬節");}}// 第五步,寫入實體資料 實際應用中這些資料從資料庫得到,if (problemList != null & problemList.size() > 0) {int s = 0;for (int i = 0; i < problemList.size(); i++) {Problem problem = (Problem) problemList.get(i);row = sheet.createRow(++s);for (int j = 0; j <= 5; j++) {cell = row.createCell(j);if (j == 0) {cell.setCellValue(problem.getContent());} else if (j == 1) {cell.setCellValue(problem.getAnswer());} else if (j == 2) {cell.setCellValue(problem.getSubjects().getName());} else if (j == 3) {cell.setCellValue(problem.getKnowledgePoints().getKnowledgeContent());} else if (j == 4) {cell.setCellValue(problem.getKnowledgePoints().getRemark2());} else if (j == 5) {cell.setCellValue(problem.getKnowledgePoints().getRemark3());}}}}// 第六步,將檔案存到指定位置try {if (createDir(filePath + "/problem")) {FileOutputStream fout = new FileOutputStream(filePath+ "/problem/problem.xls");wb.write(fout);fout.close();return true;} else {return false;}} catch (Exception e) {e.printStackTrace();return false;}}/** * @see 建立檔案夾 */public static boolean createDir(String destDirName) {File dir = new File(destDirName);if (dir.exists()) {System.out.println("建立目錄" + destDirName + "失敗,已經存在!!");}if (!destDirName.endsWith(File.separator)) {destDirName = destDirName + File.separator;}// 建立單個目錄if (dir.mkdirs()) {System.out.println("建立成功");return true;} else {System.out.println("建立失敗!!");return false;}}/** * 刪除某個檔案夾下的所有檔案夾和檔案 * * @param delpath * String * @throws FileNotFoundException * @throws IOException * @return boolean */public static boolean deletefile(String delpath) throws Exception {try {File file = new File(delpath);// 若且唯若此抽象路徑名表示的檔案存在且 是一個目錄時,返回 trueif (!file.isDirectory()) {file.delete();} else if (file.isDirectory()) {String[] filelist = file.list();for (int i = 0; i < filelist.length; i++) {File delfile = new File(delpath + "\\" + filelist[i]);if (!delfile.isDirectory()) {delfile.delete();System.out.println(delfile.getAbsolutePath() + "刪除檔案成功");} else if (delfile.isDirectory()) {deletefile(delpath + "\\" + filelist[i]);}}System.out.println(file.getAbsolutePath() + "刪除成功");file.delete();}} catch (FileNotFoundException e) {System.out.println("deletefile() Exception:" + e.getMessage());}return true;}/** * 建立ZIP檔案 * * @param sourcePath * 檔案或檔案夾路徑 * @param zipPath * 產生的zip檔案存在路徑(包括檔案名稱) */public static void createZip(String sourcePath, String zipPath) {FileOutputStream fos = null;ZipOutputStream zos = null;try {fos = new FileOutputStream(zipPath);zos = new ZipOutputStream(fos);writeZip(new File(sourcePath), "", zos);} catch (FileNotFoundException e) {System.out.println(("建立ZIP檔案失敗"));} finally {try {if (zos != null) {zos.close();}} catch (IOException e) {System.out.println(("建立ZIP檔案失敗"));}}}/** * 建立zip壓縮包 * * @param file * @param parentPath * @param zos */private static void writeZip(File file, String parentPath,ZipOutputStream zos) {zos.setEncoding("gbk");if (file.exists()) {if (file.isDirectory()) {// 處理檔案夾parentPath += file.getName() + File.separator;File[] files = file.listFiles();for (File f : files) {zos.setEncoding("gbk");writeZip(f, parentPath, zos);}} else {FileInputStream fis = null;DataInputStream dis = null;try {fis = new FileInputStream(file);dis = new DataInputStream(new BufferedInputStream(fis));ZipEntry ze = new ZipEntry(parentPath + file.getName());zos.putNextEntry(ze);zos.setEncoding("gbk");byte[] content = new byte[1024];int len;while ((len = fis.read(content)) != -1) {zos.write(content, 0, len);zos.flush();}} catch (FileNotFoundException e) {System.out.println(("建立ZIP檔案失敗"));} catch (IOException e) {System.out.println(("建立ZIP檔案失敗"));} finally {try {if (dis != null) {dis.close();}} catch (IOException e) {System.out.println(("建立ZIP檔案失敗"));}}}}}