ExcelTool.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package com.xtframe.util;
  2. import java.io.BufferedInputStream;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.lang.reflect.Field;
  8. import java.lang.reflect.Modifier;
  9. import java.net.URLEncoder;
  10. import java.util.ArrayList;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. import java.util.Map;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16. import org.apache.poi.hssf.usermodel.HSSFCell;
  17. import org.apache.poi.hssf.usermodel.HSSFRow;
  18. import org.apache.poi.hssf.usermodel.HSSFSheet;
  19. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  20. import com.xt.jygl.gkjygl.cghzyxxgl.dcsb.ctl.DcsbybbCtl;
  21. import com.xt.jygl.gkyxtjyfx.hxhbtj.vo.HxhbtjVo;
  22. import sun.misc.Unsafe;
  23. public class ExcelTool {
  24. public static int START_ROW = 1;//被复制的开始行
  25. /**
  26. * 将传入数据映射到MAP中
  27. *
  28. * @param classType
  29. * 传入数据类型
  30. * @param dataObj
  31. * 传入数据
  32. * @return 布尔值 是否复制成功
  33. * @throws IOException
  34. */
  35. public static Map reflectForgetData(Object dataObj)
  36. throws IOException {
  37. // 输出结果
  38. Map<String, Object> map = new HashMap();
  39. try{
  40. if (dataObj == null) return map;
  41. // 取得实例
  42. Class<?> realDataObj = dataObj.getClass();
  43. // 将所有OBJ内的数据放入map
  44. //创建此类型的空对象
  45. Field fu = Unsafe.class.getDeclaredField("theUnsafe");
  46. fu.setAccessible(true);
  47. Unsafe us = (Unsafe) fu.get(null);
  48. Object newObj = us.allocateInstance(realDataObj);
  49. //获取所有成员(包括private)的值,并拷贝到新对象中
  50. Field[] fields = realDataObj.getDeclaredFields();
  51. for (Field f : fields) {
  52. //不拷贝static成员和final成员
  53. if (Modifier.isStatic(f.getModifiers()) || Modifier.isFinal(f.getModifiers())) {
  54. continue;
  55. }
  56. f.setAccessible(true);
  57. Object fieldValue = f.get(dataObj);
  58. f.set(newObj, fieldValue);
  59. map.put(f.getName(), fieldValue);
  60. }
  61. }catch(Exception e){
  62. e.printStackTrace();
  63. }
  64. return map;
  65. }
  66. /**
  67. * 导出EXCEL
  68. * @param request
  69. * @param response
  70. * @param list 待导出的数据
  71. * @param type 导出的类型 1、全省港口集装箱航线航班月度统计 2、xx 3、xx
  72. * @throws Exception
  73. */
  74. public static void exportExcel(HttpServletRequest request, HttpServletResponse response,
  75. List list,int type,String exportFileName)
  76. throws Exception {
  77. // 选择模板文件:
  78. String templetFile = "hxhbtj.xls";
  79. String fileName = "全省港口集装箱航线航班月度统计.xls";
  80. if(StringUtils.isNotEmpty(exportFileName)){
  81. fileName = exportFileName + ".xls";
  82. }
  83. if (type==2) {
  84. // templetFile="gbjlhzb.xls";
  85. // fileName = "过磅记录汇总表.xls";
  86. }else if (type==3) {
  87. // templetFile="cgbjjlb.xls";
  88. // fileName = "储罐报警记录表.xls";
  89. }else if (type==4){
  90. // templetFile="smsdetail.xls";
  91. // fileName = "信息发送详情.xls";
  92. }
  93. fileName = URLEncoder.encode(fileName, "UTF-8");
  94. response.setContentType("application/msexcel; charset=utf-8");
  95. response.setHeader("Content-disposition", "attachment; filename=" + fileName);// 设定输出文件头
  96. InputStream is = null;// 输入流对象
  97. try {
  98. // 文件路径
  99. //String realpath = File.separator + "template" + File.separator + templetFile;
  100. //is = request.getSession().getServletContext().getResourceAsStream(realpath);// 获取文件输入流
  101. is=new BufferedInputStream(ExcelTool.class.getResourceAsStream("/templete/" + templetFile));
  102. HSSFWorkbook workbook2003 = new HSSFWorkbook(is);// 创建Excel2007文件对象
  103. HSSFSheet sheet = workbook2003.getSheetAt(0);// 取出第一个工作表,索引是0
  104. //list数据
  105. if (type==1) {
  106. setListInfo(sheet, HxhbtjVo.class, list);
  107. }else if (type==2) {
  108. //setListInfo(sheet, GbhzVo.class, list);
  109. }else if (type==3){
  110. //setListInfo(sheet, SbbjjlVo.class, list);
  111. }else if (type==4){
  112. //setListInfo(sheet, SendHistory.class, list);
  113. }
  114. OutputStream bos = response.getOutputStream();
  115. workbook2003.write(bos);
  116. response.flushBuffer();
  117. bos.flush();
  118. bos.close();
  119. } catch (Exception e) {
  120. e.printStackTrace();
  121. } finally {
  122. // 关闭文件流
  123. if (is != null) {
  124. try {
  125. is.close();
  126. } catch (IOException e) {
  127. e.printStackTrace();
  128. }
  129. }
  130. }
  131. }
  132. public static void setListInfo(HSSFSheet sheet, Class classType, List dataList) {
  133. int END_ROW = START_ROW + dataList.size();
  134. List mapList;
  135. try {
  136. mapList = reflectForgetData(classType, dataList);
  137. } catch (IOException e) {
  138. mapList = new ArrayList();
  139. e.printStackTrace();
  140. }
  141. if (mapList == null || mapList.size() == 0) {
  142. HSSFRow modRow = sheet.getRow(START_ROW + 1);
  143. sheet.removeRow(modRow);
  144. }
  145. // 获取模板行对象
  146. HSSFRow modRow = sheet.getRow(START_ROW + 1);
  147. for(int j = 1; j < mapList.size(); j++) {
  148. // 为各数据对象单位添加行
  149. // sheet.shiftRows(startRow + j + 1, sheet.getLastRowNum(), 1, true, false);
  150. sheet.shiftRows(START_ROW + j + 1, END_ROW, 1, true, false);
  151. HSSFRow newXSSFRow = sheet.createRow(START_ROW + j + 1);
  152. // 把模板里各行的内容复制到新生成的行里
  153. for (int k = 0; k < modRow.getLastCellNum(); k++ ) {
  154. HSSFCell modcell = modRow.getCell(k);
  155. HSSFCell newcell = newXSSFRow.createCell(k);
  156. if(null == modcell) {
  157. continue;
  158. }
  159. // 把模板里的cell相关属性设入新增cell
  160. newcell.setCellStyle(modcell.getCellStyle());
  161. newcell.setCellType(modcell.getCellType());
  162. if (null != modcell.getStringCellValue() && !"".equals(modcell.getStringCellValue().trim())) {
  163. newcell.setCellValue(StringUtils.nvl(modcell.getStringCellValue()));
  164. }
  165. }
  166. }
  167. int cellIndex = START_ROW + 1;
  168. Map<String, Object> map = null;
  169. for(int mc = 0; mc < mapList.size(); mc++) {
  170. map = (HashMap)mapList.get(mc);
  171. // 输出行数
  172. int outputRowCnt = 0;
  173. // 开始循环遍历行,表头不处理,从数据对象单位开始行开始处理
  174. //for (int i = cellIndex; i <= sheet.getLastRowNum(); i++) {
  175. HSSFRow row = sheet.getRow(cellIndex);// 获取行对象
  176. if (row == null) {// 如果为空,不处理
  177. continue;
  178. }
  179. // 循环遍历单元格
  180. for (int j = 0; j < row.getLastCellNum(); j++) {
  181. HSSFCell cell = row.getCell(j);// 获取单元格对象
  182. if (cell == null) {// 单元格为空设置cellStr为空串
  183. continue;
  184. }
  185. if (map.containsKey(cell.getStringCellValue())) {
  186. if (map.get(cell.getStringCellValue()) != null) {
  187. cell.setCellValue(String.valueOf(map.get(cell.getStringCellValue())));
  188. } else {
  189. cell.setCellValue("");
  190. }
  191. }
  192. }
  193. cellIndex++;
  194. outputRowCnt++;
  195. }
  196. }
  197. /**
  198. * 将传入数据映射到MAP中
  199. *
  200. * @param classType
  201. * 传入数据类型
  202. * @param dataObj
  203. * 传入数据
  204. * @return 布尔值 是否复制成功
  205. * @throws IOException
  206. */
  207. public static List reflectForgetData(Class<?> classType, List<Object> dataObjs)
  208. throws IOException {
  209. // 输出结果
  210. List resultList = new ArrayList();
  211. try{
  212. // 将所有OBJ内的数据放入map
  213. //创建此类型的空对象
  214. Field fu = Unsafe.class.getDeclaredField("theUnsafe");
  215. fu.setAccessible(true);
  216. Unsafe us = (Unsafe) fu.get(null);
  217. Object newObj = us.allocateInstance(classType);
  218. //获取所有成员(包括private)的值,并拷贝到新对象中
  219. Field[] fields = classType.getDeclaredFields();
  220. for(int i = 0;i < dataObjs.size();i++)
  221. {
  222. // 取得单个元素
  223. Object dataObj = dataObjs.get(i);
  224. // 单个对象
  225. Map<String, Object> map = new HashMap();
  226. for (Field f : fields) {
  227. //不拷贝static成员和final成员
  228. if (Modifier.isStatic(f.getModifiers()) || Modifier.isFinal(f.getModifiers())) {
  229. continue;
  230. }
  231. f.setAccessible(true);
  232. //Object fieldValue = f.get(dataObj);
  233. //f.set(newObj, fieldValue);
  234. map.put(f.getName(), f.get(dataObj));
  235. }
  236. resultList.add(map);
  237. }
  238. }catch(Exception e){
  239. e.printStackTrace();
  240. }
  241. return resultList;
  242. }
  243. }