DBtoExcel.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package com.happy.Until;
  2. import jxl.Workbook;
  3. import jxl.write.Label;
  4. import jxl.write.WritableSheet;
  5. import jxl.write.WritableWorkbook;
  6. import jxl.write.WriteException;
  7. import jxl.write.biff.RowsExceededException;
  8. import java.io.File;
  9. import java.sql.ResultSet;
  10. import java.util.ArrayList;
  11. import java.util.Vector;
  12. public class DBtoExcel {
  13. /**
  14. * 导出Excel表
  15. *
  16. * @param rs
  17. * 数据库结果集
  18. * @param filePath
  19. * 要保存的路径,文件名为 fileName.xls
  20. * @param sheetName
  21. * 工作簿名称 工作簿名称,本方法目前只支持导出一个Excel工作簿
  22. * @param columnName
  23. * 列名,类型为Vector
  24. */
  25. public void WriteExcel(ResultSet rs, String filePath, String sheetName,
  26. Vector columnName) {
  27. WritableWorkbook workbook = null;
  28. WritableSheet sheet = null;
  29. int rowNum = 1; // 从第2行开始写入
  30. try {
  31. workbook = Workbook.createWorkbook(new File(filePath)); // 创建Excel文件
  32. sheet = workbook.createSheet(sheetName, 0); // 创建名为 sheetName 的工作簿
  33. this.writeCol(sheet, columnName, 0); // 首先将列名写入第一行
  34. // 将结果集写入
  35. while (rs.next()) {
  36. Vector col = new Vector(); // 用以保存一行数据
  37. for (int i = 1; i <= columnName.size(); i++) { // 将一行内容保存在col中
  38. col.add(rs.getString(i));
  39. }
  40. // 写入Excel
  41. this.writeCol(sheet, col, rowNum++);
  42. }
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. } finally {
  46. try {
  47. // 关闭
  48. workbook.write();
  49. workbook.close();
  50. rs.close();
  51. } catch (Exception e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. }
  56. public void WriteExcel2(ResultSet rs, String filePath, String sheetName,
  57. Vector columnName, String time1, String time2) {
  58. WritableWorkbook workbook = null;
  59. WritableSheet sheet = null;
  60. int rowNum = 2; // 从第一行开始写入
  61. try {
  62. workbook = Workbook.createWorkbook(new File(filePath)); // 创建Excel文件
  63. sheet = workbook.createSheet(sheetName, 0); // 创建名为 sheetName 的工作簿
  64. this.writeCol(sheet, columnName, 1); // 首先将列名写入
  65. // 写入时间
  66. Vector col2 = new Vector(); // 用以保存一行数据
  67. col2.add("统计时间-:"+time1+"到"+time2);
  68. this.writeCol(sheet, col2, 0);
  69. // 将结果集写入
  70. while (rs.next()) {
  71. Vector col = new Vector(); // 用以保存一行数据
  72. for (int i = 1; i <= columnName.size(); i++) { // 将一行内容保存在col中
  73. col.add(rs.getString(i));
  74. }
  75. // 写入Excel
  76. this.writeCol2(sheet, col, rowNum++);
  77. }
  78. } catch (Exception e) {
  79. e.printStackTrace();
  80. } finally {
  81. try {
  82. // 关闭
  83. workbook.write();
  84. workbook.close();
  85. rs.close();
  86. } catch (Exception e) {
  87. e.printStackTrace();
  88. }
  89. }
  90. }
  91. public void WriteExcel4(ArrayList<ArrayList> aa, String filePath, String sheetName,
  92. Vector columnName, String time1, String time2) {
  93. WritableWorkbook workbook = null;
  94. WritableSheet sheet = null;
  95. int rowNum = 2; // 从第一行开始写入
  96. try {
  97. workbook = Workbook.createWorkbook(new File(filePath)); // 创建Excel文件
  98. sheet = workbook.createSheet(sheetName, 0); // 创建名为 sheetName 的工作簿
  99. this.writeCol(sheet, columnName, 1); // 首先将列名写入
  100. // 写入时间
  101. Vector col2 = new Vector(); // 用以保存一行数据
  102. col2.add("统计时间-:"+time1+"到"+time2);
  103. this.writeCol(sheet, col2, 0);
  104. // 将结果集写入
  105. for (int i=0; i<aa.size(); i++){
  106. Vector col = new Vector(); // 用以保存一行数据
  107. for (int j = 0; j < aa.get(i).size(); j++) { // 将一行内容保存在col中
  108. col.add(aa.get(i).get(j));
  109. }
  110. // 写入Excel
  111. this.writeCol2(sheet, col, rowNum++);
  112. }
  113. } catch (Exception e) {
  114. e.printStackTrace();
  115. } finally {
  116. try {
  117. // 关闭
  118. workbook.write();
  119. workbook.close();
  120. } catch (Exception e) {
  121. e.printStackTrace();
  122. }
  123. }
  124. }
  125. public void WriteExcel5(ArrayList<ArrayList<String>> aa, ArrayList<String> al, ArrayList<String> al2, ArrayList<String> al_1, String filePath, String sheetName,
  126. Vector columnName, String time1, String time2, String place) {
  127. WritableWorkbook workbook = null;
  128. WritableSheet sheet = null;
  129. int rowNum = 3; // 从第四行开始写入具体数据
  130. try {
  131. workbook = Workbook.createWorkbook(new File(filePath)); // 创建Excel文件
  132. sheet = workbook.createSheet(sheetName, 0); // 创建名为 sheetName 的工作簿
  133. this.writeCol(sheet, columnName, 1); // 首先第二行将列名写入
  134. // 写入时间
  135. Vector col2 = new Vector(); // 用以保存一行数据
  136. col2.add("统计时间-:"+time1+"到"+time2);
  137. col2.add(" ");
  138. col2.add(" ");
  139. col2.add(" ");
  140. col2.add(" ");
  141. col2.add("订单号:"+place);
  142. this.writeCol(sheet, col2, 0); // 第一行写入时间、订单号
  143. // 将结果集写入
  144. for (int i=0; i<aa.size(); i++){
  145. Vector col = new Vector(); // 用以保存一行数据
  146. for (int j = 0; j < aa.get(i).size(); j++) { // 将一行内容保存在col中
  147. col.add(aa.get(i).get(j));
  148. }
  149. // 写入Excel
  150. this.writeCol2(sheet, col, rowNum++);
  151. }
  152. Vector col3 = new Vector();
  153. col3.add("-价格");
  154. for (int j=0; j<al.size(); j++){
  155. col3.add(al.get(j).toString());
  156. }
  157. this.writeCol2(sheet, col3, 2);
  158. Vector col4 = new Vector();
  159. col4.add("剩余数-");
  160. for (int j=0; j<al2.size(); j++){
  161. col4.add(al2.get(j).toString());
  162. }
  163. this.writeCol2(sheet, col4, rowNum+1);
  164. Vector col5 = new Vector();
  165. col5.add("走货数-");
  166. for (int j=0; j<al_1.size(); j++){
  167. col5.add(al_1.get(j).toString());
  168. }
  169. this.writeCol2(sheet, col5, rowNum);
  170. } catch (Exception e) {
  171. e.printStackTrace();
  172. } finally {
  173. try {
  174. // 关闭
  175. workbook.write();
  176. workbook.close();
  177. } catch (Exception e) {
  178. e.printStackTrace();
  179. }
  180. }
  181. }
  182. /***
  183. * 将数组写入工作簿
  184. *
  185. * @param sheet
  186. * 要写入的工作簿
  187. * @param col
  188. * 要写入的数据数组
  189. * @param rowNum
  190. * 要写入哪一行
  191. * @throws WriteException
  192. * @throws RowsExceededException
  193. */
  194. private void writeCol(WritableSheet sheet, Vector col, int rowNum)
  195. throws RowsExceededException, WriteException {
  196. int size = col.size(); // 获取集合大小
  197. for (int i = 0; i < size; i++) { // 写入每一列
  198. Label label = new Label(i, rowNum, (String) col.get(i));
  199. sheet.addCell(label);
  200. }
  201. }
  202. // 处理工号和姓名的算法
  203. private void writeCol2(WritableSheet sheet, Vector col, int rowNum)
  204. throws RowsExceededException, WriteException {
  205. int size = col.size(); // 获取集合大小
  206. for (int i = 0; i < size; i++) { // 写入每一列
  207. if(i == 0) {
  208. String name1 = (String) col.get(i);
  209. int n = name1.indexOf("-");
  210. String name2 = name1.substring(0, n);
  211. Label label = new Label(i, rowNum, name2);
  212. sheet.addCell(label);
  213. }else{
  214. Label label = new Label(i, rowNum, (String) col.get(i));
  215. sheet.addCell(label);
  216. }
  217. }
  218. }
  219. }