java导出excel java操作文件、文件夹 java制作zip,excelzip
java导出excel java操作文件、文件夹 java制作zip,excelzip
java导出excel java操作文件、文件夹 java制作zip .
分类: java 江湖Study 2013-04-18 16:46 714人阅读 评论(1) 收藏 举报
[java] view plaincopyprint?
01./**
02. * 导出老师信息
03. */
04. public static boolean exportTeach(String filePath, String teachName,
05. String grade, String classes, String subject) {
06.
07. // 第一步,创建一个webbook,对应一个Excel文件
08. HSSFWorkbook wb = new HSSFWorkbook();
09. // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
10. HSSFSheet sheet = wb.createSheet("老师信息");
11. // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
12. HSSFRow row = sheet.createRow((int) 0);
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((int) 0);
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. }
相关文章
- 暂无相关文章
用户点评