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. }