| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773 |
- package cn.com.goldenwater.dcproj.utils.export.template;
- import cn.com.goldenwater.dcproj.dao.BisInspManageResponsibilityDao;
- import cn.com.goldenwater.dcproj.dao.BisInspVlgdrinkProjManageDao;
- import cn.com.goldenwater.dcproj.dao.BisInspWateruserInfoDao;
- import cn.com.goldenwater.dcproj.model.BisInspManageResponsibility;
- import cn.com.goldenwater.dcproj.model.BisInspVlgdrinkProjManage;
- import cn.com.goldenwater.dcproj.model.BisInspWateruserInfo;
- import cn.com.goldenwater.dcproj.param.BisInspManageResponsibilityParam;
- import cn.com.goldenwater.dcproj.param.BisInspVlgdrinkProjManageParam;
- import cn.com.goldenwater.dcproj.param.BisInspWateruserInfoParam;
- import cn.com.goldenwater.dcproj.utils.export.CreateRyTables;
- import cn.com.goldenwater.dcproj.utils.export.ExcelFieldUtil;
- import cn.com.goldenwater.dcproj.utils.export.TableEnum;
- import com.github.pagehelper.Page;
- import com.github.pagehelper.PageHelper;
- import org.apache.poi.hssf.usermodel.HSSFRow;
- import org.apache.poi.hssf.usermodel.HSSFSheet;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- /**
- * @ClassName CreateRyTablesByTemplate
- * @Author liyz
- * @Date 2019/3/29 16:31
- * @Version 1.0
- **/
- @Component
- public class CreateRyTablesByTemplate extends CreateRyTables {
- @Value("${export.templatePath}")
- private String templatePath;
- @Autowired
- private BisInspManageResponsibilityDao bisInspManageResponsibilityDao;
- @Autowired
- private BisInspVlgdrinkProjManageDao bisInspVlgdrinkProjManageDao;
- @Autowired
- private BisInspWateruserInfoDao bisInspWateruserInfoDao;
- @Autowired
- private ExcelFieldUtil excelFieldUtil;
- /**
- * 6.管理责任体系落实情况表(不合并)
- *
- * @param engId engId
- * @param dir 存放目录
- * @param fileName 文件名称
- * @return 数据状态
- */
- @Override
- public int createBisInspManageResponsibilityExcel(String engId, String dir, String fileName) {
- OutputStream os = null;
- //导出
- try {
- BisInspManageResponsibilityParam bisInspManageResponsibilityParam = new BisInspManageResponsibilityParam();
- bisInspManageResponsibilityParam.setEngId(engId);
- // 一页查200条数据
- bisInspManageResponsibilityParam.setPageSize(200);
- Page<Object> page = PageHelper.startPage(0, bisInspManageResponsibilityParam.getPageSize());
- List<BisInspManageResponsibility> data = bisInspManageResponsibilityDao.findList(bisInspManageResponsibilityParam);
- // 如果没有数据,则直接返回
- if (data.size() == 0) {
- return 0;
- }
- // 加载模板
- FileInputStream fis = new FileInputStream(templatePath + File.separator + "t6.xls");
- HSSFWorkbook fisWorkBook = new HSSFWorkbook(fis);
- HSSFWorkbook workBook = new HSSFWorkbook();
- workBook = fisWorkBook;
- workBook.setSheetName(0, "管理责任体系落实情况表");
- HSSFSheet sheet = workBook.getSheetAt(0);
- // 如果有数据,则导出到excel
- File outputFile = new File(dir + fileName + (System.currentTimeMillis()) + ".xls");
- os = new FileOutputStream(outputFile);
- // 导出到excel
- List<Map<String, Object>> list = excelFieldUtil.getBisInspManageResponsibilityDataList(data);
- int n = 0;
- for (int i = 0; i < list.size(); i++) {
- HSSFRow creRow = sheet.createRow(3 + i);
- List<Object> coVal = new ArrayList<>(list.get(i).values());
- for (int j = 0; j < coVal.size(); j++) {
- creRow.createCell(j).setCellValue(coVal.get(j).toString());
- }
- n = 3 + i;
- }
- int pages = page.getPages();
- // 如果大于1页,继续请求剩余页的数据
- if (pages > 1) {
- for (int p = 2; p < pages; p++) {
- // 请求第二页
- bisInspManageResponsibilityParam.setPageNum(p);
- // 每页200条
- PageHelper.startPage(bisInspManageResponsibilityParam.getPageNum(), bisInspManageResponsibilityParam.getPageSize());
- list = excelFieldUtil.getBisInspManageResponsibilityDataList(bisInspManageResponsibilityDao.findList(bisInspManageResponsibilityParam));
- for (int i = 0; i < list.size(); i++) {
- // 创建行,从第 n 行开始打印
- HSSFRow creRow = sheet.createRow(n);
- List<Object> coVal = new ArrayList<>(list.get(i).values());
- // 把每一行的每一列放进去
- for (int j = 0; j < coVal.size(); j++) {
- creRow.createCell(j).setCellValue(coVal.get(j).toString());
- }
- // 行数增加1
- n = n + 1;
- }
- }
- }
- workBook.write(os);
- fis.close();
- os.flush();
- os.close();
- } catch (Exception e) {
- try {
- if (os != null) {
- os.close();
- }
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- e.printStackTrace();
- }
- return 1;
- }
- /**
- * 6.管理责任体系落实情况表(合并,合并是不分区域全放在一个Excel里)
- *
- * @param endIdsList 获取数据必须的参数列表
- * @param dir 生成Excel的所在目录
- * @param fileName 生成的Excel的名称
- * @return 数据状态
- */
- @Override
- public int createBisInspManageResponsibilitySumExcel(List<String> endIdsList, String dir, String fileName) {
- OutputStream os = null;
- int zipStatusT6 = 0;
- //导出
- try {
- BisInspManageResponsibilityParam bisInspManageResponsibilityParam = new BisInspManageResponsibilityParam();
- bisInspManageResponsibilityParam.setEngId(endIdsList.get(0));
- // 一页查200条数据
- bisInspManageResponsibilityParam.setPageSize(200);
- Page<Object> page = PageHelper.startPage(0, bisInspManageResponsibilityParam.getPageSize());
- List<BisInspManageResponsibility> data = bisInspManageResponsibilityDao.findList(bisInspManageResponsibilityParam);
- // 如果没有数据,则直接返回
- if (data.size() == 0) {
- zipStatusT6 = 0;
- } else {
- zipStatusT6++;
- }
- // 加载模板
- FileInputStream fis = new FileInputStream(templatePath + File.separator + "t6.xls");
- HSSFWorkbook fisWorkBook = new HSSFWorkbook(fis);
- HSSFWorkbook workBook = new HSSFWorkbook();
- workBook = fisWorkBook;
- workBook.setSheetName(0, TableEnum.T6.getName());
- HSSFSheet sheet = workBook.getSheetAt(0);
- File outputFile = new File(dir + fileName + (System.currentTimeMillis()) + ".xls");
- os = new FileOutputStream(outputFile);
- // 导出到excel
- List<Map<String, Object>> list = excelFieldUtil.getBisInspManageResponsibilityDataList(data);
- int n = 0;
- for (int i = 0; i < list.size(); i++) {
- HSSFRow creRow = sheet.createRow(3 + i);
- List<Object> coVal = new ArrayList<>(list.get(i).values());
- for (int j = 0; j < coVal.size(); j++) {
- creRow.createCell(j).setCellValue(coVal.get(j).toString());
- }
- n = 3 + i;
- }
- // 下一页
- int pages = page.getPages();
- // 如果大于1页,继续请求剩余页的数据
- if (pages > 1) {
- for (int p = 2; p < pages; p++) {
- // 请求第二页
- bisInspManageResponsibilityParam.setPageNum(p);
- // 每页200条
- PageHelper.startPage(bisInspManageResponsibilityParam.getPageNum(), bisInspManageResponsibilityParam.getPageSize());
- list = excelFieldUtil.getBisInspManageResponsibilityDataList(bisInspManageResponsibilityDao.findList(bisInspManageResponsibilityParam));
- for (int i = 0; i < list.size(); i++) {
- // 行数增加1
- n = n + 1;
- // 创建行,从第 n 行开始打印
- HSSFRow creRow = sheet.createRow(n);
- List<Object> coVal = new ArrayList<>(list.get(i).values());
- // 把每一行的每一列放进去
- for (int j = 0; j < coVal.size(); j++) {
- creRow.createCell(j).setCellValue(coVal.get(j).toString());
- }
- }
- // workBook.write(os);
- }
- }
- // 如果有多个engId
- for (int i = 1; i < endIdsList.size(); i++) {
- // 如果第一个engId没有数据,那么n为0,从第一行开始读,就把表头读没了
- if (n == 0) {
- // 应该是从第3行向下塞数据,因为后面会加1,所以此处取2
- n = 2;
- }
- bisInspManageResponsibilityParam.setEngId(endIdsList.get(i));
- page = PageHelper.startPage(0, bisInspManageResponsibilityParam.getPageSize());
- data = bisInspManageResponsibilityDao.findList(bisInspManageResponsibilityParam);
- list = excelFieldUtil.getBisInspManageResponsibilityDataList(data);
- // 如果没有数据,则跳过
- if (list.size() == 0) {
- continue;
- }
- // 如果有数据
- zipStatusT6++;
- // 如果有数据
- for (int i2 = 0; i2 < list.size(); i2++) {
- // 行数增加1
- n = n + 1;
- // 创建行,从第 n 行开始打印
- HSSFRow creRow = sheet.createRow(n);
- List<Object> coVal = new ArrayList<>(list.get(i2).values());
- // 把每一行的每一列放进去
- for (int j = 0; j < coVal.size(); j++) {
- creRow.createCell(j).setCellValue(coVal.get(j).toString());
- }
- }
- pages = page.getPages();
- // 如果超过一页
- if (pages > 1) {
- for (int p = 2; p <= pages; p++) {
- // 请求第二页
- bisInspManageResponsibilityParam.setPageNum(p);
- // 每页200条
- PageHelper.startPage(bisInspManageResponsibilityParam.getPageNum(), bisInspManageResponsibilityParam.getPageSize());
- list = excelFieldUtil.getBisInspManageResponsibilityDataList(bisInspManageResponsibilityDao.findList(bisInspManageResponsibilityParam));
- for (int i2 = 0; i2 < list.size(); i2++) {
- // 行数增加1
- n = n + 1;
- // 创建行,从第 n 行开始打印
- HSSFRow creRow = sheet.createRow(n);
- List<Object> coVal = new ArrayList<>(list.get(i2).values());
- // 把每一行的每一列放进去
- for (int j = 0; j < coVal.size(); j++) {
- creRow.createCell(j).setCellValue(coVal.get(j).toString());
- }
- }
- // workBook.write(os);
- }
- }
- }
- // 最后写入到文件里
- workBook.write(os);
- fis.close();
- os.flush();
- os.close();
- } catch (Exception e) {
- try {
- if (os != null) {
- os.close();
- }
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- e.printStackTrace();
- }
- return zipStatusT6;
- }
- /**
- * 4.农村饮水工程运行管理情况表(不合并)
- *
- * @param engId engId
- * @param dir 存放目录
- * @param fileName 文件名称
- * @return 数据状态
- */
- @Override
- public int createBisInspVlgdrinkProjManageExcel(String engId, String dir, String fileName) {
- OutputStream os = null;
- //导出
- try {
- BisInspVlgdrinkProjManageParam bisInspVlgdrinkProjManageParam = new BisInspVlgdrinkProjManageParam();
- bisInspVlgdrinkProjManageParam.setEngId(engId);
- // 一页请求200条
- bisInspVlgdrinkProjManageParam.setPageSize(200);
- Page<Object> page = PageHelper.startPage(0, bisInspVlgdrinkProjManageParam.getPageSize());
- List<BisInspVlgdrinkProjManage> data = bisInspVlgdrinkProjManageDao.findList(bisInspVlgdrinkProjManageParam);
- // 如果没有数据,则直接返回
- if (data.size() == 0) {
- return 0;
- }
- // 如果有数据,则导出到excel
- File outputFile = new File(dir + fileName + (System.currentTimeMillis()) + ".xls");
- os = new FileOutputStream(outputFile);
- // 加载模板
- FileInputStream fis = new FileInputStream(templatePath + File.separator + "t4.xls");
- HSSFWorkbook fisWorkBook = new HSSFWorkbook(fis);
- HSSFWorkbook workBook = new HSSFWorkbook();
- workBook = fisWorkBook;
- workBook.setSheetName(0, TableEnum.T4.getName());
- HSSFSheet sheet = workBook.getSheetAt(0);
- // 导出到excel
- List<Map<String, Object>> list = excelFieldUtil.getBisInspVlgdrinkProjManageDataList(data);
- int n = 0;
- for (int i = 0; i < list.size(); i++) {
- HSSFRow creRow = sheet.createRow(4 + i);
- List<Object> coVal = new ArrayList<>(list.get(i).values());
- for (int j = 0; j < coVal.size(); j++) {
- creRow.createCell(j).setCellValue(coVal.get(j).toString());
- }
- n = 4 + i;
- }
- int pages = page.getPages();
- // 如果超过一页,继续导出
- if (pages > 1) {
- for (int p = 2; p < pages; p++) {
- bisInspVlgdrinkProjManageParam.setPageNum(p);
- PageHelper.startPage(bisInspVlgdrinkProjManageParam.getPageNum(), bisInspVlgdrinkProjManageParam.getPageSize());
- list = excelFieldUtil.getBisInspVlgdrinkProjManageDataList(bisInspVlgdrinkProjManageDao.findList(bisInspVlgdrinkProjManageParam));
- for (int i = 0; i < list.size(); i++) {
- // 创建行,从第 n 行开始打印
- HSSFRow creRow = sheet.createRow(n);
- List<Object> coVal = new ArrayList<>(list.get(i).values());
- // 把每一行的每一列放进去
- for (int j = 0; j < coVal.size(); j++) {
- creRow.createCell(j).setCellValue(coVal.get(j).toString());
- }
- // 行数增加1
- n = n + 1;
- }
- }
- }
- workBook.write(os);
- fis.close();
- os.flush();
- os.close();
- } catch (Exception e) {
- try {
- os.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- e.printStackTrace();
- }
- return 1;
- }
- /**
- * 4.农村饮水工程运行管理情况表(合并,合并是不分区域全放在一个Excel里)
- *
- * @param endIdsList endIdsList
- * @param dir 存放目录
- * @param fileName 文件名称
- * @return 数据状态
- */
- @Override
- public int createBisInspVlgdrinkProjManageSumExcel(List<String> endIdsList, String dir, String fileName) {
- OutputStream os = null;
- int zipStatusT4 = 0;
- //导出
- try {
- BisInspVlgdrinkProjManageParam bisInspVlgdrinkProjManageParam = new BisInspVlgdrinkProjManageParam();
- bisInspVlgdrinkProjManageParam.setEngId(endIdsList.get(0));
- // 一页查200条数据
- bisInspVlgdrinkProjManageParam.setPageSize(200);
- Page<Object> page = PageHelper.startPage(0, bisInspVlgdrinkProjManageParam.getPageSize());
- List<BisInspVlgdrinkProjManage> data = bisInspVlgdrinkProjManageDao.findList(bisInspVlgdrinkProjManageParam);
- // 如果没有数据,则直接返回(合并时不能直接返回,否则下面的数据就获取不到了)
- if (data.size() != 0) {
- zipStatusT4++;
- }
- // 加载模板
- FileInputStream fis = new FileInputStream(templatePath + File.separator + "t4.xls");
- HSSFWorkbook fisWorkBook = new HSSFWorkbook(fis);
- HSSFWorkbook workBook = new HSSFWorkbook();
- workBook = fisWorkBook;
- workBook.setSheetName(0, TableEnum.T4.getName());
- HSSFSheet sheet = workBook.getSheetAt(0);
- File outputFile = new File(dir + fileName + (System.currentTimeMillis()) + ".xls");
- os = new FileOutputStream(outputFile);
- // 导出到excel
- List<Map<String, Object>> list = excelFieldUtil.getBisInspVlgdrinkProjManageDataList(data);
- int n = 0;
- for (int i = 0; i < list.size(); i++) {
- HSSFRow creRow = sheet.createRow(4 + i);
- List<Object> coVal = new ArrayList<>(list.get(i).values());
- for (int j = 0; j < coVal.size(); j++) {
- creRow.createCell(j).setCellValue(coVal.get(j).toString());
- }
- n = 4 + i;
- }
- // 下一页
- int pages = page.getPages();
- // 如果大于1页,继续请求剩余页的数据
- if (pages > 1) {
- for (int p = 2; p < pages; p++) {
- // 请求第二页
- bisInspVlgdrinkProjManageParam.setPageNum(p);
- // 每页200条
- PageHelper.startPage(bisInspVlgdrinkProjManageParam.getPageNum(), bisInspVlgdrinkProjManageParam.getPageSize());
- list = excelFieldUtil.getBisInspVlgdrinkProjManageDataList(bisInspVlgdrinkProjManageDao.findList(bisInspVlgdrinkProjManageParam));
- for (int i = 0; i < list.size(); i++) {
- // 行数增加1
- n = n + 1;
- // 创建行,从第 n 行开始打印
- HSSFRow creRow = sheet.createRow(n);
- List<Object> coVal = new ArrayList<>(list.get(i).values());
- // 把每一行的每一列放进去
- for (int j = 0; j < coVal.size(); j++) {
- creRow.createCell(j).setCellValue(coVal.get(j).toString());
- }
- }
- // workBook.write(os);
- }
- }
- // 如果有多个engId
- for (int i = 1; i < endIdsList.size(); i++) {
- // 如果第一个engId没有数据,那么n为0,从第一行开始读,就把表头读没了
- if (n == 0) {
- // 应该是从第4行向下塞数据,因为后面会加1,所以此处取3
- n = 3;
- }
- bisInspVlgdrinkProjManageParam.setEngId(endIdsList.get(i));
- page = PageHelper.startPage(0, bisInspVlgdrinkProjManageParam.getPageSize());
- data = bisInspVlgdrinkProjManageDao.findList(bisInspVlgdrinkProjManageParam);
- list = excelFieldUtil.getBisInspVlgdrinkProjManageDataList(data);
- // 如果没有数据,则跳过
- if (list.size() == 0) {
- continue;
- }
- // 如果有数据,数据状态加1
- zipStatusT4++;
- // 如果有数据
- for (int i2 = 0; i2 < list.size(); i2++) {
- // 行数增加1
- n = n + 1;
- // 创建行,从第 n 行开始打印
- HSSFRow creRow = sheet.createRow(n);
- List<Object> coVal = new ArrayList<>(list.get(i2).values());
- // 把每一行的每一列放进去
- for (int j = 0; j < coVal.size(); j++) {
- creRow.createCell(j).setCellValue(coVal.get(j).toString());
- }
- }
- pages = page.getPages();
- // 如果超过一页
- if (pages > 1) {
- for (int p = 2; p <= pages; p++) {
- // 请求第二页
- bisInspVlgdrinkProjManageParam.setPageNum(p);
- // 每页200条
- PageHelper.startPage(bisInspVlgdrinkProjManageParam.getPageNum(), bisInspVlgdrinkProjManageParam.getPageSize());
- list = excelFieldUtil.getBisInspVlgdrinkProjManageDataList(bisInspVlgdrinkProjManageDao.findList(bisInspVlgdrinkProjManageParam));
- for (int i2 = 0; i2 < list.size(); i2++) {
- // 行数增加1
- n = n + 1;
- // 创建行,从第 n 行开始打印
- HSSFRow creRow = sheet.createRow(n);
- List<Object> coVal = new ArrayList<>(list.get(i2).values());
- // 把每一行的每一列放进去
- for (int j = 0; j < coVal.size(); j++) {
- creRow.createCell(j).setCellValue(coVal.get(j).toString());
- }
- }
- // workBook.write(os);
- }
- }
- }
- // 最后写入到文件里
- workBook.write(os);
- fis.close();
- os.flush();
- os.close();
- } catch (Exception e) {
- try {
- if (os != null) {
- os.close();
- }
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- e.printStackTrace();
- }
- return zipStatusT4;
- }
- /**
- * 3.用水户情况表(不合并)
- *
- * @param engId engId
- * @param dir 存放目录
- * @param fileName 文件名称
- * @return 数据状态
- */
- @Override
- public int createBisInspWateruserInfoExcel(String engId, String dir, String fileName) {
- OutputStream os = null;
- //导出
- try {
- BisInspWateruserInfoParam bisInspWateruserInfoParam = new BisInspWateruserInfoParam();
- bisInspWateruserInfoParam.setEngId(engId);
- // 一页请求200条
- bisInspWateruserInfoParam.setPageSize(200);
- Page<Object> page = PageHelper.startPage(0, bisInspWateruserInfoParam.getPageSize());
- List<BisInspWateruserInfo> data = bisInspWateruserInfoDao.findList(bisInspWateruserInfoParam);
- // 如果没有数据,则直接返回
- if (data.size() == 0) {
- return 0;
- }
- // 如果有数据,则导出到excel
- File outputFile = new File(dir + fileName + (System.currentTimeMillis()) + ".xls");
- os = new FileOutputStream(outputFile);
- // 加载模板
- FileInputStream fis = new FileInputStream(templatePath + File.separator + "t3.xls");
- HSSFWorkbook fisWorkBook = new HSSFWorkbook(fis);
- HSSFWorkbook workBook = new HSSFWorkbook();
- workBook = fisWorkBook;
- workBook.setSheetName(0, TableEnum.T3.getName());
- HSSFSheet sheet = workBook.getSheetAt(0);
- // 导出到excel
- List<Map<String, Object>> list = excelFieldUtil.getBisInspWateruserInfoDataList(data);
- int n = 0;
- for (int i = 0; i < list.size(); i++) {
- HSSFRow creRow = sheet.createRow(3 + i);
- List<Object> coVal = new ArrayList<>(list.get(i).values());
- for (int j = 0; j < coVal.size(); j++) {
- creRow.createCell(j).setCellValue(coVal.get(j).toString());
- }
- n = 3 + i;
- }
- int pages = page.getPages();
- // 如果超过一页,继续导出
- if (pages > 1) {
- for (int p = 2; p < pages; p++) {
- bisInspWateruserInfoParam.setPageNum(p);
- PageHelper.startPage(bisInspWateruserInfoParam.getPageNum(), bisInspWateruserInfoParam.getPageSize());
- list = excelFieldUtil.getBisInspWateruserInfoDataList(bisInspWateruserInfoDao.findList(bisInspWateruserInfoParam));
- for (int i = 0; i < list.size(); i++) {
- // 创建行,从第 n 行开始打印
- HSSFRow creRow = sheet.createRow(n);
- List<Object> coVal = new ArrayList<>(list.get(i).values());
- // 把每一行的每一列放进去
- for (int j = 0; j < coVal.size(); j++) {
- creRow.createCell(j).setCellValue(coVal.get(j).toString());
- }
- // 行数增加1
- n = n + 1;
- }
- }
- }
- workBook.write(os);
- fis.close();
- os.flush();
- os.close();
- } catch (Exception e) {
- try {
- os.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- e.printStackTrace();
- }
- return 1;
- }
- /**
- * 3.用水户情况表(合并,合并是不分区域全放在一个Excel里)
- *
- * @param endIdsList endIdsList
- * @param dir 存放目录
- * @param fileName 文件名称
- * @return 数据状态
- */
- @Override
- public int createBisInspWateruserInfoSumExcel(List<String> endIdsList, String dir, String fileName) {
- OutputStream os = null;
- int zipStatusT3 = 0;
- //导出
- try {
- BisInspWateruserInfoParam bisInspWateruserInfoParam = new BisInspWateruserInfoParam();
- bisInspWateruserInfoParam.setEngId(endIdsList.get(0));
- // 一页查200条数据
- bisInspWateruserInfoParam.setPageSize(200);
- Page<Object> page = PageHelper.startPage(0, bisInspWateruserInfoParam.getPageSize());
- List<BisInspWateruserInfo> data = bisInspWateruserInfoDao.findList(bisInspWateruserInfoParam);
- // 如果没有数据,则直接返回(合并时不能直接返回,否则下面的数据就获取不到了)
- if (data.size() == 0) {
- zipStatusT3 = 0;
- } else {
- zipStatusT3++;
- }
- // 加载模板
- FileInputStream fis = new FileInputStream(templatePath + File.separator + "t3.xls");
- HSSFWorkbook fisWorkBook = new HSSFWorkbook(fis);
- HSSFWorkbook workBook = new HSSFWorkbook();
- workBook = fisWorkBook;
- workBook.setSheetName(0, TableEnum.T3.getName());
- HSSFSheet sheet = workBook.getSheetAt(0);
- File outputFile = new File(dir + fileName + (System.currentTimeMillis()) + ".xls");
- os = new FileOutputStream(outputFile);
- // 导出到excel
- List<Map<String, Object>> list = excelFieldUtil.getBisInspWateruserInfoDataList(data);
- int n = 0;
- for (int i = 0; i < list.size(); i++) {
- HSSFRow creRow = sheet.createRow(3 + i);
- List<Object> coVal = new ArrayList<>(list.get(i).values());
- for (int j = 0; j < coVal.size(); j++) {
- creRow.createCell(j).setCellValue(coVal.get(j).toString());
- }
- n = 3 + i;
- }
- // 下一页
- int pages = page.getPages();
- // 如果大于1页,继续请求剩余页的数据
- if (pages > 1) {
- for (int p = 2; p < pages; p++) {
- // 请求第二页
- bisInspWateruserInfoParam.setPageNum(p);
- // 每页200条
- PageHelper.startPage(bisInspWateruserInfoParam.getPageNum(), bisInspWateruserInfoParam.getPageSize());
- list = excelFieldUtil.getBisInspWateruserInfoDataList(bisInspWateruserInfoDao.findList(bisInspWateruserInfoParam));
- for (int i = 0; i < list.size(); i++) {
- // 行数增加1
- n = n + 1;
- // 创建行,从第 n 行开始打印
- HSSFRow creRow = sheet.createRow(n);
- List<Object> coVal = new ArrayList<>(list.get(i).values());
- // 把每一行的每一列放进去
- for (int j = 0; j < coVal.size(); j++) {
- creRow.createCell(j).setCellValue(coVal.get(j).toString());
- }
- }
- // workBook.write(os);
- }
- }
- // 如果有多个engId
- for (int i = 1; i < endIdsList.size(); i++) {
- // 如果第一个engId没有数据,那么n为0,从第一行开始读,就把表头读没了
- if (n == 0) {
- // 应该是从第3行向下塞数据,因为后面会加1,所以此处取2
- n = 2;
- }
- bisInspWateruserInfoParam.setEngId(endIdsList.get(i));
- page = PageHelper.startPage(0, bisInspWateruserInfoParam.getPageSize());
- data = bisInspWateruserInfoDao.findList(bisInspWateruserInfoParam);
- list = excelFieldUtil.getBisInspWateruserInfoDataList(data);
- // 如果没有数据,则跳过
- if (list.size() == 0) {
- continue;
- }
- // 如果有数据,数据状态加1
- zipStatusT3++;
- // 如果有数据
- for (int i2 = 0; i2 < list.size(); i2++) {
- // 行数增加1
- n = n + 1;
- // 创建行,从第 n 行开始打印
- HSSFRow creRow = sheet.createRow(n);
- List<Object> coVal = new ArrayList<>(list.get(i2).values());
- // 把每一行的每一列放进去
- for (int j = 0; j < coVal.size(); j++) {
- creRow.createCell(j).setCellValue(coVal.get(j).toString());
- }
- }
- pages = page.getPages();
- // 如果超过一页
- if (pages > 1) {
- for (int p = 2; p <= pages; p++) {
- // 请求第二页
- bisInspWateruserInfoParam.setPageNum(p);
- // 每页200条
- PageHelper.startPage(bisInspWateruserInfoParam.getPageNum(), bisInspWateruserInfoParam.getPageSize());
- list = excelFieldUtil.getBisInspWateruserInfoDataList(bisInspWateruserInfoDao.findList(bisInspWateruserInfoParam));
- for (int i2 = 0; i2 < list.size(); i2++) {
- // 行数增加1
- n = n + 1;
- // 创建行,从第 n 行开始打印
- HSSFRow creRow = sheet.createRow(n);
- List<Object> coVal = new ArrayList<>(list.get(i2).values());
- // 把每一行的每一列放进去
- for (int j = 0; j < coVal.size(); j++) {
- creRow.createCell(j).setCellValue(coVal.get(j).toString());
- }
- }
- // workBook.write(os);
- }
- }
- }
- // 最后写入到文件里
- workBook.write(os);
- fis.close();
- os.flush();
- os.close();
- } catch (Exception e) {
- try {
- if (os != null) {
- os.close();
- }
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- e.printStackTrace();
- }
- return zipStatusT3;
- }
- }
|