e579754bcb0c077d42356cbf6729120590647ca5.svn-base 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. package cn.com.goldenwater.dcproj.utils.export.template;
  2. import cn.com.goldenwater.dcproj.dao.BisInspManageResponsibilityDao;
  3. import cn.com.goldenwater.dcproj.dao.BisInspVlgdrinkProjManageDao;
  4. import cn.com.goldenwater.dcproj.dao.BisInspWateruserInfoDao;
  5. import cn.com.goldenwater.dcproj.model.BisInspManageResponsibility;
  6. import cn.com.goldenwater.dcproj.model.BisInspVlgdrinkProjManage;
  7. import cn.com.goldenwater.dcproj.model.BisInspWateruserInfo;
  8. import cn.com.goldenwater.dcproj.param.BisInspManageResponsibilityParam;
  9. import cn.com.goldenwater.dcproj.param.BisInspVlgdrinkProjManageParam;
  10. import cn.com.goldenwater.dcproj.param.BisInspWateruserInfoParam;
  11. import cn.com.goldenwater.dcproj.utils.export.CreateRyTables;
  12. import cn.com.goldenwater.dcproj.utils.export.ExcelFieldUtil;
  13. import cn.com.goldenwater.dcproj.utils.export.TableEnum;
  14. import com.github.pagehelper.Page;
  15. import com.github.pagehelper.PageHelper;
  16. import org.apache.poi.hssf.usermodel.HSSFRow;
  17. import org.apache.poi.hssf.usermodel.HSSFSheet;
  18. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.beans.factory.annotation.Value;
  21. import org.springframework.stereotype.Component;
  22. import java.io.*;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.Map;
  26. /**
  27. * @ClassName CreateRyTablesByTemplate
  28. * @Author liyz
  29. * @Date 2019/3/29 16:31
  30. * @Version 1.0
  31. **/
  32. @Component
  33. public class CreateRyTablesByTemplate extends CreateRyTables {
  34. @Value("${export.templatePath}")
  35. private String templatePath;
  36. @Autowired
  37. private BisInspManageResponsibilityDao bisInspManageResponsibilityDao;
  38. @Autowired
  39. private BisInspVlgdrinkProjManageDao bisInspVlgdrinkProjManageDao;
  40. @Autowired
  41. private BisInspWateruserInfoDao bisInspWateruserInfoDao;
  42. @Autowired
  43. private ExcelFieldUtil excelFieldUtil;
  44. /**
  45. * 6.管理责任体系落实情况表(不合并)
  46. *
  47. * @param engId engId
  48. * @param dir 存放目录
  49. * @param fileName 文件名称
  50. * @return 数据状态
  51. */
  52. @Override
  53. public int createBisInspManageResponsibilityExcel(String engId, String dir, String fileName) {
  54. OutputStream os = null;
  55. //导出
  56. try {
  57. BisInspManageResponsibilityParam bisInspManageResponsibilityParam = new BisInspManageResponsibilityParam();
  58. bisInspManageResponsibilityParam.setEngId(engId);
  59. // 一页查200条数据
  60. bisInspManageResponsibilityParam.setPageSize(200);
  61. Page<Object> page = PageHelper.startPage(0, bisInspManageResponsibilityParam.getPageSize());
  62. List<BisInspManageResponsibility> data = bisInspManageResponsibilityDao.findList(bisInspManageResponsibilityParam);
  63. // 如果没有数据,则直接返回
  64. if (data.size() == 0) {
  65. return 0;
  66. }
  67. // 加载模板
  68. FileInputStream fis = new FileInputStream(templatePath + File.separator + "t6.xls");
  69. HSSFWorkbook fisWorkBook = new HSSFWorkbook(fis);
  70. HSSFWorkbook workBook = new HSSFWorkbook();
  71. workBook = fisWorkBook;
  72. workBook.setSheetName(0, "管理责任体系落实情况表");
  73. HSSFSheet sheet = workBook.getSheetAt(0);
  74. // 如果有数据,则导出到excel
  75. File outputFile = new File(dir + fileName + (System.currentTimeMillis()) + ".xls");
  76. os = new FileOutputStream(outputFile);
  77. // 导出到excel
  78. List<Map<String, Object>> list = excelFieldUtil.getBisInspManageResponsibilityDataList(data);
  79. int n = 0;
  80. for (int i = 0; i < list.size(); i++) {
  81. HSSFRow creRow = sheet.createRow(3 + i);
  82. List<Object> coVal = new ArrayList<>(list.get(i).values());
  83. for (int j = 0; j < coVal.size(); j++) {
  84. creRow.createCell(j).setCellValue(coVal.get(j).toString());
  85. }
  86. n = 3 + i;
  87. }
  88. int pages = page.getPages();
  89. // 如果大于1页,继续请求剩余页的数据
  90. if (pages > 1) {
  91. for (int p = 2; p < pages; p++) {
  92. // 请求第二页
  93. bisInspManageResponsibilityParam.setPageNum(p);
  94. // 每页200条
  95. PageHelper.startPage(bisInspManageResponsibilityParam.getPageNum(), bisInspManageResponsibilityParam.getPageSize());
  96. list = excelFieldUtil.getBisInspManageResponsibilityDataList(bisInspManageResponsibilityDao.findList(bisInspManageResponsibilityParam));
  97. for (int i = 0; i < list.size(); i++) {
  98. // 创建行,从第 n 行开始打印
  99. HSSFRow creRow = sheet.createRow(n);
  100. List<Object> coVal = new ArrayList<>(list.get(i).values());
  101. // 把每一行的每一列放进去
  102. for (int j = 0; j < coVal.size(); j++) {
  103. creRow.createCell(j).setCellValue(coVal.get(j).toString());
  104. }
  105. // 行数增加1
  106. n = n + 1;
  107. }
  108. }
  109. }
  110. workBook.write(os);
  111. fis.close();
  112. os.flush();
  113. os.close();
  114. } catch (Exception e) {
  115. try {
  116. if (os != null) {
  117. os.close();
  118. }
  119. } catch (IOException e1) {
  120. e1.printStackTrace();
  121. }
  122. e.printStackTrace();
  123. }
  124. return 1;
  125. }
  126. /**
  127. * 6.管理责任体系落实情况表(合并,合并是不分区域全放在一个Excel里)
  128. *
  129. * @param endIdsList 获取数据必须的参数列表
  130. * @param dir 生成Excel的所在目录
  131. * @param fileName 生成的Excel的名称
  132. * @return 数据状态
  133. */
  134. @Override
  135. public int createBisInspManageResponsibilitySumExcel(List<String> endIdsList, String dir, String fileName) {
  136. OutputStream os = null;
  137. int zipStatusT6 = 0;
  138. //导出
  139. try {
  140. BisInspManageResponsibilityParam bisInspManageResponsibilityParam = new BisInspManageResponsibilityParam();
  141. bisInspManageResponsibilityParam.setEngId(endIdsList.get(0));
  142. // 一页查200条数据
  143. bisInspManageResponsibilityParam.setPageSize(200);
  144. Page<Object> page = PageHelper.startPage(0, bisInspManageResponsibilityParam.getPageSize());
  145. List<BisInspManageResponsibility> data = bisInspManageResponsibilityDao.findList(bisInspManageResponsibilityParam);
  146. // 如果没有数据,则直接返回
  147. if (data.size() == 0) {
  148. zipStatusT6 = 0;
  149. } else {
  150. zipStatusT6++;
  151. }
  152. // 加载模板
  153. FileInputStream fis = new FileInputStream(templatePath + File.separator + "t6.xls");
  154. HSSFWorkbook fisWorkBook = new HSSFWorkbook(fis);
  155. HSSFWorkbook workBook = new HSSFWorkbook();
  156. workBook = fisWorkBook;
  157. workBook.setSheetName(0, TableEnum.T6.getName());
  158. HSSFSheet sheet = workBook.getSheetAt(0);
  159. File outputFile = new File(dir + fileName + (System.currentTimeMillis()) + ".xls");
  160. os = new FileOutputStream(outputFile);
  161. // 导出到excel
  162. List<Map<String, Object>> list = excelFieldUtil.getBisInspManageResponsibilityDataList(data);
  163. int n = 0;
  164. for (int i = 0; i < list.size(); i++) {
  165. HSSFRow creRow = sheet.createRow(3 + i);
  166. List<Object> coVal = new ArrayList<>(list.get(i).values());
  167. for (int j = 0; j < coVal.size(); j++) {
  168. creRow.createCell(j).setCellValue(coVal.get(j).toString());
  169. }
  170. n = 3 + i;
  171. }
  172. // 下一页
  173. int pages = page.getPages();
  174. // 如果大于1页,继续请求剩余页的数据
  175. if (pages > 1) {
  176. for (int p = 2; p < pages; p++) {
  177. // 请求第二页
  178. bisInspManageResponsibilityParam.setPageNum(p);
  179. // 每页200条
  180. PageHelper.startPage(bisInspManageResponsibilityParam.getPageNum(), bisInspManageResponsibilityParam.getPageSize());
  181. list = excelFieldUtil.getBisInspManageResponsibilityDataList(bisInspManageResponsibilityDao.findList(bisInspManageResponsibilityParam));
  182. for (int i = 0; i < list.size(); i++) {
  183. // 行数增加1
  184. n = n + 1;
  185. // 创建行,从第 n 行开始打印
  186. HSSFRow creRow = sheet.createRow(n);
  187. List<Object> coVal = new ArrayList<>(list.get(i).values());
  188. // 把每一行的每一列放进去
  189. for (int j = 0; j < coVal.size(); j++) {
  190. creRow.createCell(j).setCellValue(coVal.get(j).toString());
  191. }
  192. }
  193. // workBook.write(os);
  194. }
  195. }
  196. // 如果有多个engId
  197. for (int i = 1; i < endIdsList.size(); i++) {
  198. // 如果第一个engId没有数据,那么n为0,从第一行开始读,就把表头读没了
  199. if (n == 0) {
  200. // 应该是从第3行向下塞数据,因为后面会加1,所以此处取2
  201. n = 2;
  202. }
  203. bisInspManageResponsibilityParam.setEngId(endIdsList.get(i));
  204. page = PageHelper.startPage(0, bisInspManageResponsibilityParam.getPageSize());
  205. data = bisInspManageResponsibilityDao.findList(bisInspManageResponsibilityParam);
  206. list = excelFieldUtil.getBisInspManageResponsibilityDataList(data);
  207. // 如果没有数据,则跳过
  208. if (list.size() == 0) {
  209. continue;
  210. }
  211. // 如果有数据
  212. zipStatusT6++;
  213. // 如果有数据
  214. for (int i2 = 0; i2 < list.size(); i2++) {
  215. // 行数增加1
  216. n = n + 1;
  217. // 创建行,从第 n 行开始打印
  218. HSSFRow creRow = sheet.createRow(n);
  219. List<Object> coVal = new ArrayList<>(list.get(i2).values());
  220. // 把每一行的每一列放进去
  221. for (int j = 0; j < coVal.size(); j++) {
  222. creRow.createCell(j).setCellValue(coVal.get(j).toString());
  223. }
  224. }
  225. pages = page.getPages();
  226. // 如果超过一页
  227. if (pages > 1) {
  228. for (int p = 2; p <= pages; p++) {
  229. // 请求第二页
  230. bisInspManageResponsibilityParam.setPageNum(p);
  231. // 每页200条
  232. PageHelper.startPage(bisInspManageResponsibilityParam.getPageNum(), bisInspManageResponsibilityParam.getPageSize());
  233. list = excelFieldUtil.getBisInspManageResponsibilityDataList(bisInspManageResponsibilityDao.findList(bisInspManageResponsibilityParam));
  234. for (int i2 = 0; i2 < list.size(); i2++) {
  235. // 行数增加1
  236. n = n + 1;
  237. // 创建行,从第 n 行开始打印
  238. HSSFRow creRow = sheet.createRow(n);
  239. List<Object> coVal = new ArrayList<>(list.get(i2).values());
  240. // 把每一行的每一列放进去
  241. for (int j = 0; j < coVal.size(); j++) {
  242. creRow.createCell(j).setCellValue(coVal.get(j).toString());
  243. }
  244. }
  245. // workBook.write(os);
  246. }
  247. }
  248. }
  249. // 最后写入到文件里
  250. workBook.write(os);
  251. fis.close();
  252. os.flush();
  253. os.close();
  254. } catch (Exception e) {
  255. try {
  256. if (os != null) {
  257. os.close();
  258. }
  259. } catch (IOException e1) {
  260. e1.printStackTrace();
  261. }
  262. e.printStackTrace();
  263. }
  264. return zipStatusT6;
  265. }
  266. /**
  267. * 4.农村饮水工程运行管理情况表(不合并)
  268. *
  269. * @param engId engId
  270. * @param dir 存放目录
  271. * @param fileName 文件名称
  272. * @return 数据状态
  273. */
  274. @Override
  275. public int createBisInspVlgdrinkProjManageExcel(String engId, String dir, String fileName) {
  276. OutputStream os = null;
  277. //导出
  278. try {
  279. BisInspVlgdrinkProjManageParam bisInspVlgdrinkProjManageParam = new BisInspVlgdrinkProjManageParam();
  280. bisInspVlgdrinkProjManageParam.setEngId(engId);
  281. // 一页请求200条
  282. bisInspVlgdrinkProjManageParam.setPageSize(200);
  283. Page<Object> page = PageHelper.startPage(0, bisInspVlgdrinkProjManageParam.getPageSize());
  284. List<BisInspVlgdrinkProjManage> data = bisInspVlgdrinkProjManageDao.findList(bisInspVlgdrinkProjManageParam);
  285. // 如果没有数据,则直接返回
  286. if (data.size() == 0) {
  287. return 0;
  288. }
  289. // 如果有数据,则导出到excel
  290. File outputFile = new File(dir + fileName + (System.currentTimeMillis()) + ".xls");
  291. os = new FileOutputStream(outputFile);
  292. // 加载模板
  293. FileInputStream fis = new FileInputStream(templatePath + File.separator + "t4.xls");
  294. HSSFWorkbook fisWorkBook = new HSSFWorkbook(fis);
  295. HSSFWorkbook workBook = new HSSFWorkbook();
  296. workBook = fisWorkBook;
  297. workBook.setSheetName(0, TableEnum.T4.getName());
  298. HSSFSheet sheet = workBook.getSheetAt(0);
  299. // 导出到excel
  300. List<Map<String, Object>> list = excelFieldUtil.getBisInspVlgdrinkProjManageDataList(data);
  301. int n = 0;
  302. for (int i = 0; i < list.size(); i++) {
  303. HSSFRow creRow = sheet.createRow(4 + i);
  304. List<Object> coVal = new ArrayList<>(list.get(i).values());
  305. for (int j = 0; j < coVal.size(); j++) {
  306. creRow.createCell(j).setCellValue(coVal.get(j).toString());
  307. }
  308. n = 4 + i;
  309. }
  310. int pages = page.getPages();
  311. // 如果超过一页,继续导出
  312. if (pages > 1) {
  313. for (int p = 2; p < pages; p++) {
  314. bisInspVlgdrinkProjManageParam.setPageNum(p);
  315. PageHelper.startPage(bisInspVlgdrinkProjManageParam.getPageNum(), bisInspVlgdrinkProjManageParam.getPageSize());
  316. list = excelFieldUtil.getBisInspVlgdrinkProjManageDataList(bisInspVlgdrinkProjManageDao.findList(bisInspVlgdrinkProjManageParam));
  317. for (int i = 0; i < list.size(); i++) {
  318. // 创建行,从第 n 行开始打印
  319. HSSFRow creRow = sheet.createRow(n);
  320. List<Object> coVal = new ArrayList<>(list.get(i).values());
  321. // 把每一行的每一列放进去
  322. for (int j = 0; j < coVal.size(); j++) {
  323. creRow.createCell(j).setCellValue(coVal.get(j).toString());
  324. }
  325. // 行数增加1
  326. n = n + 1;
  327. }
  328. }
  329. }
  330. workBook.write(os);
  331. fis.close();
  332. os.flush();
  333. os.close();
  334. } catch (Exception e) {
  335. try {
  336. os.close();
  337. } catch (IOException e1) {
  338. e1.printStackTrace();
  339. }
  340. e.printStackTrace();
  341. }
  342. return 1;
  343. }
  344. /**
  345. * 4.农村饮水工程运行管理情况表(合并,合并是不分区域全放在一个Excel里)
  346. *
  347. * @param endIdsList endIdsList
  348. * @param dir 存放目录
  349. * @param fileName 文件名称
  350. * @return 数据状态
  351. */
  352. @Override
  353. public int createBisInspVlgdrinkProjManageSumExcel(List<String> endIdsList, String dir, String fileName) {
  354. OutputStream os = null;
  355. int zipStatusT4 = 0;
  356. //导出
  357. try {
  358. BisInspVlgdrinkProjManageParam bisInspVlgdrinkProjManageParam = new BisInspVlgdrinkProjManageParam();
  359. bisInspVlgdrinkProjManageParam.setEngId(endIdsList.get(0));
  360. // 一页查200条数据
  361. bisInspVlgdrinkProjManageParam.setPageSize(200);
  362. Page<Object> page = PageHelper.startPage(0, bisInspVlgdrinkProjManageParam.getPageSize());
  363. List<BisInspVlgdrinkProjManage> data = bisInspVlgdrinkProjManageDao.findList(bisInspVlgdrinkProjManageParam);
  364. // 如果没有数据,则直接返回(合并时不能直接返回,否则下面的数据就获取不到了)
  365. if (data.size() != 0) {
  366. zipStatusT4++;
  367. }
  368. // 加载模板
  369. FileInputStream fis = new FileInputStream(templatePath + File.separator + "t4.xls");
  370. HSSFWorkbook fisWorkBook = new HSSFWorkbook(fis);
  371. HSSFWorkbook workBook = new HSSFWorkbook();
  372. workBook = fisWorkBook;
  373. workBook.setSheetName(0, TableEnum.T4.getName());
  374. HSSFSheet sheet = workBook.getSheetAt(0);
  375. File outputFile = new File(dir + fileName + (System.currentTimeMillis()) + ".xls");
  376. os = new FileOutputStream(outputFile);
  377. // 导出到excel
  378. List<Map<String, Object>> list = excelFieldUtil.getBisInspVlgdrinkProjManageDataList(data);
  379. int n = 0;
  380. for (int i = 0; i < list.size(); i++) {
  381. HSSFRow creRow = sheet.createRow(4 + i);
  382. List<Object> coVal = new ArrayList<>(list.get(i).values());
  383. for (int j = 0; j < coVal.size(); j++) {
  384. creRow.createCell(j).setCellValue(coVal.get(j).toString());
  385. }
  386. n = 4 + i;
  387. }
  388. // 下一页
  389. int pages = page.getPages();
  390. // 如果大于1页,继续请求剩余页的数据
  391. if (pages > 1) {
  392. for (int p = 2; p < pages; p++) {
  393. // 请求第二页
  394. bisInspVlgdrinkProjManageParam.setPageNum(p);
  395. // 每页200条
  396. PageHelper.startPage(bisInspVlgdrinkProjManageParam.getPageNum(), bisInspVlgdrinkProjManageParam.getPageSize());
  397. list = excelFieldUtil.getBisInspVlgdrinkProjManageDataList(bisInspVlgdrinkProjManageDao.findList(bisInspVlgdrinkProjManageParam));
  398. for (int i = 0; i < list.size(); i++) {
  399. // 行数增加1
  400. n = n + 1;
  401. // 创建行,从第 n 行开始打印
  402. HSSFRow creRow = sheet.createRow(n);
  403. List<Object> coVal = new ArrayList<>(list.get(i).values());
  404. // 把每一行的每一列放进去
  405. for (int j = 0; j < coVal.size(); j++) {
  406. creRow.createCell(j).setCellValue(coVal.get(j).toString());
  407. }
  408. }
  409. // workBook.write(os);
  410. }
  411. }
  412. // 如果有多个engId
  413. for (int i = 1; i < endIdsList.size(); i++) {
  414. // 如果第一个engId没有数据,那么n为0,从第一行开始读,就把表头读没了
  415. if (n == 0) {
  416. // 应该是从第4行向下塞数据,因为后面会加1,所以此处取3
  417. n = 3;
  418. }
  419. bisInspVlgdrinkProjManageParam.setEngId(endIdsList.get(i));
  420. page = PageHelper.startPage(0, bisInspVlgdrinkProjManageParam.getPageSize());
  421. data = bisInspVlgdrinkProjManageDao.findList(bisInspVlgdrinkProjManageParam);
  422. list = excelFieldUtil.getBisInspVlgdrinkProjManageDataList(data);
  423. // 如果没有数据,则跳过
  424. if (list.size() == 0) {
  425. continue;
  426. }
  427. // 如果有数据,数据状态加1
  428. zipStatusT4++;
  429. // 如果有数据
  430. for (int i2 = 0; i2 < list.size(); i2++) {
  431. // 行数增加1
  432. n = n + 1;
  433. // 创建行,从第 n 行开始打印
  434. HSSFRow creRow = sheet.createRow(n);
  435. List<Object> coVal = new ArrayList<>(list.get(i2).values());
  436. // 把每一行的每一列放进去
  437. for (int j = 0; j < coVal.size(); j++) {
  438. creRow.createCell(j).setCellValue(coVal.get(j).toString());
  439. }
  440. }
  441. pages = page.getPages();
  442. // 如果超过一页
  443. if (pages > 1) {
  444. for (int p = 2; p <= pages; p++) {
  445. // 请求第二页
  446. bisInspVlgdrinkProjManageParam.setPageNum(p);
  447. // 每页200条
  448. PageHelper.startPage(bisInspVlgdrinkProjManageParam.getPageNum(), bisInspVlgdrinkProjManageParam.getPageSize());
  449. list = excelFieldUtil.getBisInspVlgdrinkProjManageDataList(bisInspVlgdrinkProjManageDao.findList(bisInspVlgdrinkProjManageParam));
  450. for (int i2 = 0; i2 < list.size(); i2++) {
  451. // 行数增加1
  452. n = n + 1;
  453. // 创建行,从第 n 行开始打印
  454. HSSFRow creRow = sheet.createRow(n);
  455. List<Object> coVal = new ArrayList<>(list.get(i2).values());
  456. // 把每一行的每一列放进去
  457. for (int j = 0; j < coVal.size(); j++) {
  458. creRow.createCell(j).setCellValue(coVal.get(j).toString());
  459. }
  460. }
  461. // workBook.write(os);
  462. }
  463. }
  464. }
  465. // 最后写入到文件里
  466. workBook.write(os);
  467. fis.close();
  468. os.flush();
  469. os.close();
  470. } catch (Exception e) {
  471. try {
  472. if (os != null) {
  473. os.close();
  474. }
  475. } catch (IOException e1) {
  476. e1.printStackTrace();
  477. }
  478. e.printStackTrace();
  479. }
  480. return zipStatusT4;
  481. }
  482. /**
  483. * 3.用水户情况表(不合并)
  484. *
  485. * @param engId engId
  486. * @param dir 存放目录
  487. * @param fileName 文件名称
  488. * @return 数据状态
  489. */
  490. @Override
  491. public int createBisInspWateruserInfoExcel(String engId, String dir, String fileName) {
  492. OutputStream os = null;
  493. //导出
  494. try {
  495. BisInspWateruserInfoParam bisInspWateruserInfoParam = new BisInspWateruserInfoParam();
  496. bisInspWateruserInfoParam.setEngId(engId);
  497. // 一页请求200条
  498. bisInspWateruserInfoParam.setPageSize(200);
  499. Page<Object> page = PageHelper.startPage(0, bisInspWateruserInfoParam.getPageSize());
  500. List<BisInspWateruserInfo> data = bisInspWateruserInfoDao.findList(bisInspWateruserInfoParam);
  501. // 如果没有数据,则直接返回
  502. if (data.size() == 0) {
  503. return 0;
  504. }
  505. // 如果有数据,则导出到excel
  506. File outputFile = new File(dir + fileName + (System.currentTimeMillis()) + ".xls");
  507. os = new FileOutputStream(outputFile);
  508. // 加载模板
  509. FileInputStream fis = new FileInputStream(templatePath + File.separator + "t3.xls");
  510. HSSFWorkbook fisWorkBook = new HSSFWorkbook(fis);
  511. HSSFWorkbook workBook = new HSSFWorkbook();
  512. workBook = fisWorkBook;
  513. workBook.setSheetName(0, TableEnum.T3.getName());
  514. HSSFSheet sheet = workBook.getSheetAt(0);
  515. // 导出到excel
  516. List<Map<String, Object>> list = excelFieldUtil.getBisInspWateruserInfoDataList(data);
  517. int n = 0;
  518. for (int i = 0; i < list.size(); i++) {
  519. HSSFRow creRow = sheet.createRow(3 + i);
  520. List<Object> coVal = new ArrayList<>(list.get(i).values());
  521. for (int j = 0; j < coVal.size(); j++) {
  522. creRow.createCell(j).setCellValue(coVal.get(j).toString());
  523. }
  524. n = 3 + i;
  525. }
  526. int pages = page.getPages();
  527. // 如果超过一页,继续导出
  528. if (pages > 1) {
  529. for (int p = 2; p < pages; p++) {
  530. bisInspWateruserInfoParam.setPageNum(p);
  531. PageHelper.startPage(bisInspWateruserInfoParam.getPageNum(), bisInspWateruserInfoParam.getPageSize());
  532. list = excelFieldUtil.getBisInspWateruserInfoDataList(bisInspWateruserInfoDao.findList(bisInspWateruserInfoParam));
  533. for (int i = 0; i < list.size(); i++) {
  534. // 创建行,从第 n 行开始打印
  535. HSSFRow creRow = sheet.createRow(n);
  536. List<Object> coVal = new ArrayList<>(list.get(i).values());
  537. // 把每一行的每一列放进去
  538. for (int j = 0; j < coVal.size(); j++) {
  539. creRow.createCell(j).setCellValue(coVal.get(j).toString());
  540. }
  541. // 行数增加1
  542. n = n + 1;
  543. }
  544. }
  545. }
  546. workBook.write(os);
  547. fis.close();
  548. os.flush();
  549. os.close();
  550. } catch (Exception e) {
  551. try {
  552. os.close();
  553. } catch (IOException e1) {
  554. e1.printStackTrace();
  555. }
  556. e.printStackTrace();
  557. }
  558. return 1;
  559. }
  560. /**
  561. * 3.用水户情况表(合并,合并是不分区域全放在一个Excel里)
  562. *
  563. * @param endIdsList endIdsList
  564. * @param dir 存放目录
  565. * @param fileName 文件名称
  566. * @return 数据状态
  567. */
  568. @Override
  569. public int createBisInspWateruserInfoSumExcel(List<String> endIdsList, String dir, String fileName) {
  570. OutputStream os = null;
  571. int zipStatusT3 = 0;
  572. //导出
  573. try {
  574. BisInspWateruserInfoParam bisInspWateruserInfoParam = new BisInspWateruserInfoParam();
  575. bisInspWateruserInfoParam.setEngId(endIdsList.get(0));
  576. // 一页查200条数据
  577. bisInspWateruserInfoParam.setPageSize(200);
  578. Page<Object> page = PageHelper.startPage(0, bisInspWateruserInfoParam.getPageSize());
  579. List<BisInspWateruserInfo> data = bisInspWateruserInfoDao.findList(bisInspWateruserInfoParam);
  580. // 如果没有数据,则直接返回(合并时不能直接返回,否则下面的数据就获取不到了)
  581. if (data.size() == 0) {
  582. zipStatusT3 = 0;
  583. } else {
  584. zipStatusT3++;
  585. }
  586. // 加载模板
  587. FileInputStream fis = new FileInputStream(templatePath + File.separator + "t3.xls");
  588. HSSFWorkbook fisWorkBook = new HSSFWorkbook(fis);
  589. HSSFWorkbook workBook = new HSSFWorkbook();
  590. workBook = fisWorkBook;
  591. workBook.setSheetName(0, TableEnum.T3.getName());
  592. HSSFSheet sheet = workBook.getSheetAt(0);
  593. File outputFile = new File(dir + fileName + (System.currentTimeMillis()) + ".xls");
  594. os = new FileOutputStream(outputFile);
  595. // 导出到excel
  596. List<Map<String, Object>> list = excelFieldUtil.getBisInspWateruserInfoDataList(data);
  597. int n = 0;
  598. for (int i = 0; i < list.size(); i++) {
  599. HSSFRow creRow = sheet.createRow(3 + i);
  600. List<Object> coVal = new ArrayList<>(list.get(i).values());
  601. for (int j = 0; j < coVal.size(); j++) {
  602. creRow.createCell(j).setCellValue(coVal.get(j).toString());
  603. }
  604. n = 3 + i;
  605. }
  606. // 下一页
  607. int pages = page.getPages();
  608. // 如果大于1页,继续请求剩余页的数据
  609. if (pages > 1) {
  610. for (int p = 2; p < pages; p++) {
  611. // 请求第二页
  612. bisInspWateruserInfoParam.setPageNum(p);
  613. // 每页200条
  614. PageHelper.startPage(bisInspWateruserInfoParam.getPageNum(), bisInspWateruserInfoParam.getPageSize());
  615. list = excelFieldUtil.getBisInspWateruserInfoDataList(bisInspWateruserInfoDao.findList(bisInspWateruserInfoParam));
  616. for (int i = 0; i < list.size(); i++) {
  617. // 行数增加1
  618. n = n + 1;
  619. // 创建行,从第 n 行开始打印
  620. HSSFRow creRow = sheet.createRow(n);
  621. List<Object> coVal = new ArrayList<>(list.get(i).values());
  622. // 把每一行的每一列放进去
  623. for (int j = 0; j < coVal.size(); j++) {
  624. creRow.createCell(j).setCellValue(coVal.get(j).toString());
  625. }
  626. }
  627. // workBook.write(os);
  628. }
  629. }
  630. // 如果有多个engId
  631. for (int i = 1; i < endIdsList.size(); i++) {
  632. // 如果第一个engId没有数据,那么n为0,从第一行开始读,就把表头读没了
  633. if (n == 0) {
  634. // 应该是从第3行向下塞数据,因为后面会加1,所以此处取2
  635. n = 2;
  636. }
  637. bisInspWateruserInfoParam.setEngId(endIdsList.get(i));
  638. page = PageHelper.startPage(0, bisInspWateruserInfoParam.getPageSize());
  639. data = bisInspWateruserInfoDao.findList(bisInspWateruserInfoParam);
  640. list = excelFieldUtil.getBisInspWateruserInfoDataList(data);
  641. // 如果没有数据,则跳过
  642. if (list.size() == 0) {
  643. continue;
  644. }
  645. // 如果有数据,数据状态加1
  646. zipStatusT3++;
  647. // 如果有数据
  648. for (int i2 = 0; i2 < list.size(); i2++) {
  649. // 行数增加1
  650. n = n + 1;
  651. // 创建行,从第 n 行开始打印
  652. HSSFRow creRow = sheet.createRow(n);
  653. List<Object> coVal = new ArrayList<>(list.get(i2).values());
  654. // 把每一行的每一列放进去
  655. for (int j = 0; j < coVal.size(); j++) {
  656. creRow.createCell(j).setCellValue(coVal.get(j).toString());
  657. }
  658. }
  659. pages = page.getPages();
  660. // 如果超过一页
  661. if (pages > 1) {
  662. for (int p = 2; p <= pages; p++) {
  663. // 请求第二页
  664. bisInspWateruserInfoParam.setPageNum(p);
  665. // 每页200条
  666. PageHelper.startPage(bisInspWateruserInfoParam.getPageNum(), bisInspWateruserInfoParam.getPageSize());
  667. list = excelFieldUtil.getBisInspWateruserInfoDataList(bisInspWateruserInfoDao.findList(bisInspWateruserInfoParam));
  668. for (int i2 = 0; i2 < list.size(); i2++) {
  669. // 行数增加1
  670. n = n + 1;
  671. // 创建行,从第 n 行开始打印
  672. HSSFRow creRow = sheet.createRow(n);
  673. List<Object> coVal = new ArrayList<>(list.get(i2).values());
  674. // 把每一行的每一列放进去
  675. for (int j = 0; j < coVal.size(); j++) {
  676. creRow.createCell(j).setCellValue(coVal.get(j).toString());
  677. }
  678. }
  679. // workBook.write(os);
  680. }
  681. }
  682. }
  683. // 最后写入到文件里
  684. workBook.write(os);
  685. fis.close();
  686. os.flush();
  687. os.close();
  688. } catch (Exception e) {
  689. try {
  690. if (os != null) {
  691. os.close();
  692. }
  693. } catch (IOException e1) {
  694. e1.printStackTrace();
  695. }
  696. e.printStackTrace();
  697. }
  698. return zipStatusT3;
  699. }
  700. }