5c9f41abb87b2026edda42eabd8228cd512db52a.svn-base 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package com.yihu.utils;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.OutputStream;
  5. import java.io.PrintWriter;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.regex.Matcher;
  11. import java.util.regex.Pattern;
  12. import javax.servlet.http.HttpServletResponse;
  13. import com.common.json.JSONArray;
  14. import com.common.json.JSONObject;
  15. import jxl.Workbook;
  16. import jxl.write.WritableCellFormat;
  17. import jxl.write.WritableFont;
  18. import jxl.write.WritableSheet;
  19. import jxl.write.WritableWorkbook;
  20. public class ExportExcel {
  21. public static void initExpExl(HttpServletResponse response,String exlName,JSONObject json,List<String> title,List<String> keyList) throws Exception{
  22. Date date = new Date();
  23. SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");
  24. String time = f.format(date);
  25. String fileName = time +".xls";
  26. String destination = FileUtil.getTomcatLogDir() +fileName;
  27. File file = null;
  28. System.out.println("destination="+destination);
  29. try{
  30. String sb = json.isNull("rows") ? "" : json.getString("rows");
  31. if (!"".equals(sb) ) {
  32. file = new File(destination);
  33. if (!file.exists()) {
  34. file.createNewFile();
  35. }
  36. WritableWorkbook wwb = Workbook.createWorkbook(file);
  37. WritableFont font1= new WritableFont(WritableFont.TIMES,12,WritableFont.NO_BOLD);
  38. WritableCellFormat format1=new WritableCellFormat(font1);
  39. format1.setAlignment(jxl.format.Alignment.CENTRE);
  40. format1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
  41. WritableSheet wsheet = wwb.createSheet(exlName, 0);
  42. exportExpExl(wsheet, json,title,keyList);
  43. // 设置输出的格式
  44. response.reset();
  45. response.setContentType("application/x-download;charset=UTF-8");
  46. response.addHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("utf-8"), "ISO8859-1") + "\"");
  47. wwb.write();
  48. wwb.close();
  49. FileInputStream in = new FileInputStream(file);
  50. OutputStream out = response.getOutputStream();
  51. byte[] size = new byte[1024 * 10];
  52. int i = 0;
  53. while ((i = in.read(size, 0, size.length)) != -1) {
  54. out.write(size, 0, i);
  55. }
  56. in.close();
  57. out.flush();
  58. out.close();
  59. } else {
  60. if (sb.equals("")) {
  61. PrintWriter out = response.getWriter();
  62. out.print("暂无相关数据");
  63. out.flush();
  64. out.close();
  65. } else {
  66. PrintWriter out = response.getWriter();
  67. out.print(sb);
  68. out.flush();
  69. out.close();
  70. }
  71. }
  72. }catch(Exception e){
  73. throw(e);
  74. }finally{
  75. try {
  76. file.delete();
  77. } catch (Exception e) {
  78. e.printStackTrace();
  79. }
  80. }
  81. }
  82. public static void initExpExlMandD(HttpServletResponse response,String exlName,String exlName1,JSONObject json,JSONObject json1,List<String> title,List<String> keyList) throws Exception{
  83. Date date = new Date();
  84. SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");
  85. String time = f.format(date);
  86. String fileName = time +".xls";
  87. String destination = FileUtil.getTomcatLogDir() +fileName;
  88. File file = null;
  89. System.out.println("destination="+destination);
  90. try{
  91. String sb = json.isNull("rows") ? "" : json.getString("rows");
  92. if (!"".equals(sb) ) {
  93. file = new File(destination);
  94. if (!file.exists()) {
  95. file.createNewFile();
  96. }
  97. WritableWorkbook wwb = Workbook.createWorkbook(file);
  98. WritableFont font1= new WritableFont(WritableFont.TIMES,12,WritableFont.NO_BOLD);
  99. WritableCellFormat format1=new WritableCellFormat(font1);
  100. format1.setAlignment(jxl.format.Alignment.CENTRE);
  101. format1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
  102. WritableSheet wsheet = wwb.createSheet(exlName, 0);
  103. WritableSheet wsheet1 = wwb.createSheet(exlName1, 0);
  104. exportExpExl(wsheet, json,title,keyList);
  105. exportExpExl(wsheet1, json1,title,keyList);
  106. // 设置输出的格式
  107. response.reset();
  108. response.setContentType("application/x-download;charset=UTF-8");
  109. response.addHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("utf-8"), "ISO8859-1") + "\"");
  110. wwb.write();
  111. wwb.close();
  112. FileInputStream in = new FileInputStream(file);
  113. OutputStream out = response.getOutputStream();
  114. byte[] size = new byte[1024 * 10];
  115. int i = 0;
  116. while ((i = in.read(size, 0, size.length)) != -1) {
  117. out.write(size, 0, i);
  118. }
  119. in.close();
  120. out.flush();
  121. out.close();
  122. } else {
  123. if (sb.equals("")) {
  124. PrintWriter out = response.getWriter();
  125. out.print("暂无相关数据");
  126. out.flush();
  127. out.close();
  128. } else {
  129. PrintWriter out = response.getWriter();
  130. out.print(sb);
  131. out.flush();
  132. out.close();
  133. }
  134. }
  135. }catch(Exception e){
  136. throw(e);
  137. }finally{
  138. try {
  139. file.delete();
  140. } catch (Exception e) {
  141. e.printStackTrace();
  142. }
  143. }
  144. }
  145. public static void exportExpExl(WritableSheet wsheet,JSONObject json,List<String> titleList,List<String> keyList) throws Exception{
  146. JSONArray arry=json.getJSONArray("rows");
  147. int i=0;
  148. for (String string : titleList) {
  149. wsheet.addCell(new jxl.write.Label(i, 0, string));
  150. wsheet.setColumnView(0, 15);
  151. i++;
  152. }
  153. int nI = 1;
  154. if (null != arry && arry.length() > 0) {
  155. for (int j = 0; j < arry.length(); j++) {
  156. JSONObject jsonObject = arry.getJSONObject(j);
  157. for (int k = 0; k < keyList.size(); k++) {
  158. String vlaue = jsonObject.optString(keyList.get(k));
  159. if(isNumber(vlaue)){
  160. wsheet.addCell(new jxl.write.Number(k, nI,Double.valueOf(vlaue)));
  161. }else{
  162. wsheet.addCell(new jxl.write.Label(k, nI,jsonObject.optString(keyList.get(k))));
  163. }
  164. }
  165. nI++;
  166. }
  167. }
  168. }
  169. public static boolean isEmpty(Object obj) {
  170. if (obj == null)
  171. return true;
  172. if (obj instanceof String) {
  173. return (obj == null || obj.toString().trim().length() == 0);
  174. }
  175. if (obj instanceof Long) {
  176. return (obj == null || (Long) obj == 0);
  177. }
  178. if (obj instanceof Integer) {
  179. return (obj == null || (Integer) obj == 0);
  180. }
  181. return obj == null;
  182. }
  183. /**
  184. * 判断是否为数字
  185. * @param str
  186. * @return
  187. */
  188. private static boolean isNumeric(String str){
  189. for (int i = 0; i < str.length(); i++){
  190. System.out.println(str.charAt(i));
  191. if (!Character.isDigit(str.charAt(i))){
  192. return false;
  193. }
  194. }
  195. return true;
  196. }
  197. /**
  198. * 判断字符串是否数值
  199. * @param str
  200. * @return true:是数值 ;false:不是数值
  201. * @author:WD_SUHUAFU
  202. */
  203. private static boolean isNumber(String str) {
  204. Pattern pattern = Pattern.compile("^[0-9]+(.[0-9]*)?$");
  205. Matcher match = pattern.matcher(str);
  206. return match.matches();
  207. }
  208. /*static String getValue(JSONObject obj, String key){
  209. try{
  210. return obj.getString(key);
  211. }catch(JSONException e) {
  212. return "";
  213. }
  214. }*/
  215. static String getMapValue(Map<String, Object> obj, String key){
  216. if(obj.get(key)!=null){
  217. return obj.get(key).toString();
  218. }else{
  219. return "";
  220. }
  221. }
  222. }