244fa570d2fc2603a1b68f94fa6ecf0adc0619b7.svn-base 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package xin.glue.ui.common;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Date;
  7. import java.util.List;
  8. import xin.glue.ui.common.entity.MergeCell;
  9. import jxl.Workbook;
  10. import jxl.format.UnderlineStyle;
  11. import jxl.write.Label;
  12. import jxl.write.WritableFont;
  13. import jxl.write.WritableSheet;
  14. import jxl.write.WritableWorkbook;
  15. import jxl.write.WriteException;
  16. import com.alibaba.fastjson.JSON;
  17. import com.alibaba.fastjson.JSONArray;
  18. import com.alibaba.fastjson.JSONObject;
  19. public class OutExcelCommon {
  20. /**
  21. * 导出Excel(简单表格,不含合并行列)
  22. * @param jsonStr 结果集json格式字符串
  23. * @param fileName 文件路径
  24. * @param sheetName 工作表名称
  25. * @param title 首行列名
  26. * @param colRef 列对应字段
  27. * @throws FileNotFoundException,WriteException, IOException,Exception
  28. */
  29. public static void outExcel(String jsonStr,String fileName,String sheetName,String[] title,String[] colRef) throws FileNotFoundException,WriteException, IOException,Exception {
  30. int startIndex =0;
  31. int endIndex =0;
  32. int index =0;
  33. Label label = null;
  34. WritableWorkbook book = null;
  35. WritableSheet sheet = null;
  36. try {
  37. deleteFiles(fileName);
  38. File excelFile = new File(fileName+sheetName+".xls");
  39. // 如果文件存在就删除它
  40. if (excelFile.exists())
  41. excelFile.delete();
  42. // 打开文件
  43. book = Workbook.createWorkbook(excelFile);
  44. JSONArray array = JSONArray.parseArray(jsonStr);
  45. int maxCnt = 65530;//每个工作表格显示65530条数据(注:jxl导出excel表格一个工作表最多可以存储65536条)
  46. int avg = array.size() / maxCnt ;
  47. int avgCnt = array.size() % maxCnt == 0 ? avg : avg+1 ;
  48. //avgCnt = array.size() == 0 ? 1 : avgCnt;
  49. for(int e = 0; e < avgCnt; e++){
  50. // 生成工作表,参数0表示这是第一页
  51. sheet = book.createSheet(sheetName+e, e);
  52. // 合并单元格
  53. //sheet.mergeCells(5, 5, 6, 6);
  54. // 文字样式
  55. jxl.write.WritableFont wfc = new jxl.write.WritableFont(
  56. WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false,
  57. UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
  58. jxl.write.WritableFont wfc2 = new jxl.write.WritableFont(
  59. WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false,
  60. UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
  61. jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);
  62. jxl.write.WritableCellFormat wcfFC2 = new jxl.write.WritableCellFormat(wfc2);
  63. // 设置单元格样式
  64. wcfFC.setBackground(jxl.format.Colour.GRAY_25);// 单元格颜色
  65. wcfFC.setAlignment(jxl.format.Alignment.CENTRE);// 单元格居中
  66. wcfFC.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);//设置边框
  67. wcfFC2.setAlignment(jxl.format.Alignment.CENTRE);// 单元格居中
  68. wcfFC2.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);//设置边框
  69. index = 0;
  70. // 构造表格首行
  71. String[] titles = title[0].split("\\^");
  72. for (int i =0;i<titles.length;i++) {
  73. // 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
  74. label = new Label(i, 0, titles[i], wcfFC);
  75. // 将定义好的单元格添加到工作表中
  76. sheet.addCell(label);
  77. sheet.setColumnView(i, 20); // 设置列的宽度
  78. }
  79. index++;
  80. // 构造表格数据行
  81. String[] colRefs = colRef[0].split("\\^");
  82. startIndex = e * maxCnt;
  83. endIndex = (startIndex + maxCnt) > array.size() ? array.size() : (startIndex + maxCnt) ;
  84. for(int i = startIndex; i < endIndex;i++){
  85. JSONObject jobj = (JSONObject)array.get(i);
  86. for (int j =0;j<colRefs.length;j++) {
  87. if(jobj.getString(colRefs[j]) == null){
  88. //若无数据,返回空
  89. label = new Label(j, index, "",wcfFC2);
  90. }else{
  91. label = new Label(j, index, jobj.getString(colRefs[j]),wcfFC2);
  92. }
  93. sheet.addCell(label);
  94. }
  95. index++;
  96. }
  97. }
  98. // 写入数据并关闭文件
  99. book.write();
  100. book.close();
  101. } catch (FileNotFoundException e) {
  102. throw e;
  103. } catch (WriteException e) {
  104. book.write();
  105. book.close();
  106. throw e;
  107. } catch (IOException e) {
  108. book.write();
  109. book.close();
  110. throw e;
  111. } catch (Exception e) {
  112. book.write();
  113. book.close();
  114. throw e;
  115. }
  116. }
  117. /**
  118. * 导出Excel(复杂表格,含合并行列)
  119. * @param jsonStr 结果集json格式字符串
  120. * @param fileName 文件路径
  121. * @param sheetName 工作表名称
  122. * @param title 首行列名
  123. * @param colRef 列对应字段
  124. * @param jsonMerges 合并行列json格式字符串(每个合并的行列必须对应一个MergeCell对象)
  125. * @throws Exception
  126. */
  127. public static void outExcel(String jsonStr,String fileName,String sheetName,String[] title,String[] colRef,String jsonMerges) throws Exception{
  128. int index =0;
  129. int startIndex =0;
  130. int endIndex =0;
  131. Label label = null;
  132. WritableWorkbook book = null;
  133. WritableSheet sheet = null;
  134. try {
  135. deleteFiles(fileName);
  136. File excelFile = new File(fileName+sheetName+".xls");
  137. // 如果文件存在就删除它
  138. if (excelFile.exists())
  139. excelFile.delete();
  140. // 打开文件
  141. book = Workbook.createWorkbook(excelFile);
  142. JSONArray array = JSONArray.parseArray(jsonStr);
  143. int maxCnt = 65530;//每个工作表格显示65530条数据(注:jxl导出excel表格一个工作表最多可以存储65536条)
  144. int avg = array.size() / maxCnt ;
  145. int avgCnt = array.size() % maxCnt == 0 ? avg : avg+1 ;
  146. //avgCnt = array.size() == 0 ? 1 : avgCnt;
  147. for(int e = 0; e < avgCnt; e++){
  148. // 生成工作表,参数0表示这是第一页
  149. sheet = book.createSheet(sheetName+e, e);
  150. // 合并单元格
  151. //sheet.mergeCells(5, 5, 6, 6);
  152. // 文字样式
  153. jxl.write.WritableFont wfc = new jxl.write.WritableFont(
  154. WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false,
  155. UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
  156. jxl.write.WritableFont wfc2 = new jxl.write.WritableFont(
  157. WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false,
  158. UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
  159. jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);
  160. jxl.write.WritableCellFormat wcfFC2 = new jxl.write.WritableCellFormat(wfc2);
  161. // 设置单元格样式
  162. wcfFC.setBackground(jxl.format.Colour.GRAY_25);// 单元格颜色
  163. wcfFC.setAlignment(jxl.format.Alignment.CENTRE);// 单元格居中
  164. wcfFC.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);//设置边框
  165. wcfFC2.setAlignment(jxl.format.Alignment.CENTRE);// 单元格居中
  166. wcfFC2.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);//设置边框
  167. index =0;
  168. // 构造表格首行
  169. String[] ts = title[0].split("\\|");
  170. String[] titles;
  171. for (int i =0;i<ts.length;i++) {
  172. titles= ts[i].split("\\^");
  173. for (int j =0;j<titles.length;j++) {
  174. // 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
  175. label = new Label(j, i, titles[j], wcfFC);
  176. // 将定义好的单元格添加到工作表中
  177. sheet.addCell(label);
  178. sheet.setColumnView(j, 20); // 设置列的宽度
  179. }
  180. index++;
  181. }
  182. //合并单元格
  183. //合: 第1列第1行 到 第4列第1行
  184. //sheet.mergeCells(0, 0, 3, 0);
  185. List<MergeCell> cellsList=JSON.parseArray(jsonMerges,MergeCell.class);
  186. for (MergeCell mergeCell : cellsList) {
  187. if("x".equals(mergeCell.getName())){
  188. sheet.mergeCells(mergeCell.getX(), mergeCell.getY(), mergeCell.getX()+mergeCell.getCnt()-1, mergeCell.getY());
  189. }else{
  190. sheet.mergeCells(mergeCell.getX(), mergeCell.getY(), mergeCell.getX(), mergeCell.getY()+mergeCell.getCnt()-1);
  191. }
  192. }
  193. // 构造表格数据行
  194. String[] colRefs = colRef[0].split("\\^");
  195. startIndex = e * maxCnt;
  196. endIndex = (startIndex + maxCnt) > array.size() ? array.size() : (startIndex + maxCnt) ;
  197. for(int i = startIndex; i <endIndex;i++){
  198. JSONObject jobj = (JSONObject)array.get(i);
  199. for (int j =0;j<colRefs.length;j++) {
  200. if(jobj.getString(colRefs[j]) == null){
  201. //若无数据,返回空
  202. label = new Label(j, index, "",wcfFC2);
  203. }else{
  204. label = new Label(j, index, jobj.getString(colRefs[j]),wcfFC2);
  205. }
  206. sheet.addCell(label);
  207. }
  208. index++;
  209. }
  210. }
  211. book.write();
  212. book.close();
  213. } catch (FileNotFoundException e) {
  214. throw e;
  215. } catch (WriteException e) {
  216. book.write();
  217. book.close();
  218. throw e;
  219. } catch (IOException e) {
  220. book.write();
  221. book.close();
  222. throw e;
  223. } catch (Exception e) {
  224. book.write();
  225. book.close();
  226. throw e;
  227. }
  228. }
  229. //删除文件夹下不是当天的文件
  230. public static void deleteFiles(String path){
  231. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
  232. java.util.Date date=new java.util.Date();
  233. String str=sdf.format(date);
  234. File file = new File(path);
  235. if(!file.exists()){
  236. file.mkdirs();
  237. }
  238. File[] list = file.listFiles();
  239. for (int i = 0; i < list.length; i++) {
  240. date = new Date(list[i].lastModified());
  241. if(!str.equals(sdf.format(date))){
  242. list[i].delete();
  243. }
  244. }
  245. }
  246. }