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

/**
	 * 导出老师信息
	 */
	public static boolean exportTeach(String filePath, String teachName,
			String grade, String classes, String subject) {

		// 第一步,创建一个webbook,对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet("老师信息");
		// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
		HSSFRow 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文件中的sheet
		HSSFSheet sheet = wb.createSheet("备课题目信息");
		// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
		HSSFRow 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);
			// 当且仅当此抽象路径名表示的文件存在且 是一个目录时,返回 true
			if (!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文件失败"));
					}
				}
			}
		}
	}

你可能感兴趣的:(java导出excel java操作文件。文件夹 java制作zip)