From fa3522b71d6aab4bfca8f598879e3e04abd6793d Mon Sep 17 00:00:00 2001 From: chenlw <874313221@qq.com> Date: Tue, 25 Oct 2016 09:09:30 +0800 Subject: [PATCH] =?UTF-8?q?excel=E8=81=94=E8=B0=83=E5=AE=8C=E6=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...r.xml => standard_data_details-mapper.xml} | 34 ++--- .../platform/controller/ExcelController.java | 5 +- src/com/platform/entities/PreDataInfo.java | 68 +++++++++ .../platform/service/IPreDataInfoService.java | 7 + .../service/impl/PreDataInfoServiceImpl.java | 42 +++++- .../utils/excelUtils/ExcelOperation.java | 130 ++++++++++++++---- 6 files changed, 241 insertions(+), 45 deletions(-) rename src/com/dao/mapper/{pre-data-details-mapper.xml => standard_data_details-mapper.xml} (89%) diff --git a/src/com/dao/mapper/pre-data-details-mapper.xml b/src/com/dao/mapper/standard_data_details-mapper.xml similarity index 89% rename from src/com/dao/mapper/pre-data-details-mapper.xml rename to src/com/dao/mapper/standard_data_details-mapper.xml index ed0c50fd..fcf64032 100644 --- a/src/com/dao/mapper/pre-data-details-mapper.xml +++ b/src/com/dao/mapper/standard_data_details-mapper.xml @@ -63,17 +63,17 @@ PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" - AND pre_data_details.city_name=#{cityName} + AND standard_data_details.city_name=#{cityName} AND - pre_data_details.district_name=#{districtName} + standard_data_details.district_name=#{districtName} - AND pre_data_details.data_version=#{dataVersion} + AND standard_data_details.data_version=#{dataVersion} - AND pre_data_details.system_name=#{systemName} + AND standard_data_details.system_name=#{systemName} @@ -84,7 +84,7 @@ PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" - area_code,city_name,district_name,system_name,area_level,data_version,begin_in_use,function_details, + id,area_code,city_name,district_name,system_name,area_level,data_version,begin_in_use,function_details, department_manager,manager_contacts,sys_developer_full_name,data_base_type,data_base_version, is_budget_exec,is_budget_quota,is_collection,is_department_budget,total_budget_account, final_account_manage,report_form_manage,non_tax_revenue_manage,wage_system, @@ -96,20 +96,20 @@ PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" resultMap="getEntityByText"> SELECT - FROM pre_data_details + FROM standard_data_details - and pre_data_details.id>= #{offset} + and standard_data_details.id>= #{offset} - and pre_data_details.id>=0 + and standard_data_details.id>=0 - ORDER BY pre_data_details.area_code,pre_data_details.sys_code + ORDER BY standard_data_details.area_code,standard_data_details.sys_code LIMIT #{limit} @@ -121,7 +121,7 @@ PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" - SELECT MAX(idx) FROM (SELECT id idx FROM pre_data_details + SELECT MAX(idx) FROM (SELECT id idx FROM standard_data_details ORDER BY id LIMIT 0,#{totalLimit}) AS TEMP SELECT - FROM pre_data_details - ORDER BY pre_data_details.area_code,pre_data_details.sys_code + FROM standard_data_details + ORDER BY standard_data_details.area_code,standard_data_details.sys_code SELECT CONCAT(area_code,',',system_name) - FROM pre_data_details + FROM standard_data_details where CONCAT(area_code,',',system_name) IN diff --git a/src/com/platform/controller/ExcelController.java b/src/com/platform/controller/ExcelController.java index 0c52ac15..c34d21c2 100644 --- a/src/com/platform/controller/ExcelController.java +++ b/src/com/platform/controller/ExcelController.java @@ -122,7 +122,10 @@ public class ExcelController extends BaseController{ // 文件下载处理函数 @RequestMapping(value = "/file/download") public ResponseEntity download(HttpServletRequest request, - HttpServletResponse response) throws IOException { + HttpServletResponse response) throws Exception { + //先导出 + preDataInfoService.exportExcel(Configs.FILE_DOWNLOAD_PATH); + File file = new File(Configs.FILE_DOWNLOAD_PATH); System.out.println(Configs.FILE_DOWNLOAD_PATH); if (file.exists()) { diff --git a/src/com/platform/entities/PreDataInfo.java b/src/com/platform/entities/PreDataInfo.java index e7594d20..5dd778e9 100644 --- a/src/com/platform/entities/PreDataInfo.java +++ b/src/com/platform/entities/PreDataInfo.java @@ -1,7 +1,12 @@ package com.platform.entities; +import java.util.ArrayList; +import java.util.List; + public class PreDataInfo { + private int id; + /** 是否更新 */ private String isupdate = "-"; @@ -76,6 +81,23 @@ public class PreDataInfo { /** 政府性债务管理 */ private String governmentDebtManage; + + /** 业务范围 */ + private List workRange; + + /** + * @return the id + */ + public int getId() { + return id; + } + + /** + * @param id the id to set + */ + public void setId(int id) { + this.id = id; + } /** * @return the isupdate @@ -428,4 +450,50 @@ public class PreDataInfo { this.governmentDebtManage = governmentDebtManage; } + /** + * @return the workRange + */ + public List getWorkRange() { + return workRange; + } + + /** + * @param workRange the workRange to set + */ + public void setWorkRange(List workRange) { + this.workRange = workRange; + if (null == this.workRange) { + this.workRange = new ArrayList(); + } + if ("是".equals(departmentBudgetManage)) { + this.workRange.add("部门预算管理"); + } + if ("是".equals(budgetQuotaManage)) { + this.workRange.add("预算指标管理"); + } + if ("是".equals(budgetExecManage)) { + this.workRange.add("预算执行管理"); + } + if ("是".equals(totalBudgetAccount)) { + this.workRange.add("总预算会计"); + } + if ("是".equals(finalAccountManage)) { + this.workRange.add("决算管理"); + } + if ("是".equals(reportFormManage)) { + this.workRange.add("报表管理"); + } + if ("是".equals(nonTaxManage)) { + this.workRange.add("非税收入管理"); + } + if ("是".equals(wageSystem)) { + this.workRange.add("工资统发系统"); + } + if ("是".equals(analysisOfFinancialEconomicProsperity)) { + this.workRange.add("财政经济景气预测与分析"); + } + if ("是".equals(governmentDebtManage)) { + this.workRange.add("政府性债务管理"); + } + } } diff --git a/src/com/platform/service/IPreDataInfoService.java b/src/com/platform/service/IPreDataInfoService.java index aa7cacef..98fa7408 100644 --- a/src/com/platform/service/IPreDataInfoService.java +++ b/src/com/platform/service/IPreDataInfoService.java @@ -21,6 +21,13 @@ public interface IPreDataInfoService { */ public Map importExcel(List paths)throws Exception; + /** 导出 + * @param paths 导出的excel路径 + * @return + * @throws Exception + */ + public Map exportExcel(String path)throws Exception; + /** 条件查询 * @param op * @return diff --git a/src/com/platform/service/impl/PreDataInfoServiceImpl.java b/src/com/platform/service/impl/PreDataInfoServiceImpl.java index 51805322..1b4f8737 100644 --- a/src/com/platform/service/impl/PreDataInfoServiceImpl.java +++ b/src/com/platform/service/impl/PreDataInfoServiceImpl.java @@ -1,6 +1,7 @@ package com.platform.service.impl; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; @@ -14,6 +15,7 @@ import javax.annotation.Resource; import org.apache.log4j.Logger; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Service; import com.platform.utils.BeanCopy; @@ -50,6 +52,9 @@ public class PreDataInfoServiceImpl implements IPreDataInfoService { @Override public List findAll() throws Exception { List result = preDataInfoDao.findAll(); + for (PreDataInfo preDataInfo : result) { + preDataInfo.setWorkRange(new ArrayList()); + } return result; } @@ -94,7 +99,7 @@ public class PreDataInfoServiceImpl implements IPreDataInfoService { //Excel导入 try { //excel数据 --> java bean - List subPreData = ExcelOperation.readExcel4Update(workbook, Constant.EXCEL_TEMPLATE_INIT_ROW, PreDataInfo.class); + List subPreData = ExcelOperation.readExcel4Update(workbook, Constant.EXCEL_TEMPLATE_INIT_ROW, 0, PreDataInfo.class, "id", "workRange"); if (null != subPreData && subPreData.size() > 0) { //是否是 同一个县区的 系统--是则 加入 boolean isSameArea = true; @@ -307,4 +312,39 @@ public class PreDataInfoServiceImpl implements IPreDataInfoService { return result; } + @Override + public Map exportExcel(String path) throws Exception { + Map map = new HashMap(); + List errList = new ArrayList(); + List list = findAll(); +// InputStreamReader in= new InputStreamReader(Resource.class.getResourceAsStream(templateFilePath), "UTF-8"); + ClassPathResource res = new ClassPathResource(Constant.SYSTEM_INFO_EXCEL_TEMPLATE_FIEL_PATH); + //此处 耗时太久:(原因:excel 2007 以后采用的方式不同造成的建议升级 到 poi 3.15以上) + XSSFWorkbook workbook = null; + XSSFWorkbook result = null; + try { + workbook = new XSSFWorkbook(res.getInputStream()); + result = ExcelOperation.writeExcelTemplate(workbook, Constant.EXCEL_TEMPLATE_INIT_ROW, 0, 25, list, "id", "workRange"); + } catch (IllegalArgumentException + | IllegalAccessException | IOException e) { + errList.add(e.getMessage()); + } + try { + File f = new File(path); + FileOutputStream fileOut = null; + if (f.exists()) { + fileOut = new FileOutputStream(f); + result.write(fileOut); + fileOut.close(); + } + else { + errList.add("without this file "+path); + } + } catch (IOException e) { + errList.add(e.getMessage()); + } + map.put("err", errList); + return map; + } + } diff --git a/src/com/platform/utils/excelUtils/ExcelOperation.java b/src/com/platform/utils/excelUtils/ExcelOperation.java index c7cb9563..34280b9b 100644 --- a/src/com/platform/utils/excelUtils/ExcelOperation.java +++ b/src/com/platform/utils/excelUtils/ExcelOperation.java @@ -4,6 +4,7 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.lang.reflect.Field; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; @@ -23,17 +24,21 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelOperation { /** 写入excel - * @param templateFilePath + * @param workbook 模板 + * @param excelInitRow 从第excelInitRow行 开始填充数据,( Constant.EXCEL_TEMPLATE_INIT_ROW) + * @param excelInitCell 从第excelInitCell列 开始填充数据, + * @param excelEndCell 到第excelEndCell列 结束, * @param list list中对象的属性顺序和模板的列顺序一一对应 - * @param excelInitRow //从第excelInitRow行 开始填充数据,( Constant.EXCEL_TEMPLATE_INIT_ROW) - * @return HSSFWorkbook + * @param filter list中对象的属性过滤(比如"id") + * @return 带有数据的XSSFWorkbook * @throws FileNotFoundException * @throws IOException * @throws IllegalArgumentException * @throws IllegalAccessException - *///从第几行 填充数据 - public static XSSFWorkbook writeExcelTemplate(XSSFWorkbook workbook, int excelInitRow, List list) throws FileNotFoundException, IOException, IllegalArgumentException, IllegalAccessException { - + */ + public static XSSFWorkbook writeExcelTemplate(XSSFWorkbook workbook, int excelInitRow, int excelInitCell, int excelEndCell,List list, String... filter) throws FileNotFoundException, IOException, IllegalArgumentException, IllegalAccessException { + List filters = new ArrayList(); + filters.addAll(Arrays.asList(filter)); //读取工作表 XSSFSheet sheet = workbook.getSheetAt(0);//getSheet("Sheet1"); XSSFRow row; @@ -46,15 +51,30 @@ public class ExcelOperation { Object obj = list.get(i); Field[] fields = obj.getClass().getDeclaredFields(); lengths = fields.length; + int num = 0; for (int j = 0; j < lengths; j++) { - fields[j].setAccessible(true); + if (j - num >= lengths) { + break; + } + if (j >= excelEndCell) { + break; + } + num = getWriteIndex(filters, fields, j - num, num); + if (num == 1) { + break; + } + Field tmpField = fields[j - num]; + tmpField.setAccessible(true); cell = row.createCell((short) j,HSSFCellStyle.ALIGN_CENTER); cell.setCellType(HSSFCell.CELL_TYPE_STRING); - Object value = fields[j].get(obj); + Object value = tmpField.get(obj); if (null != value) { //int型 - if (fields[j].getType().toString().contains("Integer")) { - cell.setCellValue(String.valueOf(value)); + if (tmpField.getType().toString().contains("Integer") || tmpField.getType().toString().contains("int")) { + if (filters.contains(tmpField.getName())) + cell.setCellValue("-"); + else + cell.setCellValue(String.valueOf(value)); } else { cell.setCellValue(value.toString()); @@ -123,14 +143,18 @@ public class ExcelOperation { } /** 读取excel模板 - * @param workbook excel的XSSFWorkbook + * @param workbook excel的带数据的XSSFWorkbook * @param excelInitRow 读取数据的起始行 + * @param excelInitCell 读取数据的起始列 * @param classz excel的每行数据导出 到的 类 - * @return - * @throws IllegalAccessException - * @throws InstantiationException + * @param filter excel的每行数据导出 到的 类 的属性的过滤 + * @return 类 的集合 + * @throws InstantiationException + * @throws IllegalAccessException */ - public static List readExcel4Update(XSSFWorkbook workbook, int excelInitRow, Class classz) throws InstantiationException, IllegalAccessException { + public static List readExcel4Update(XSSFWorkbook workbook, int excelInitRow, int excelInitCell, Class classz, String... filter) throws InstantiationException, IllegalAccessException { + List filters = new ArrayList(); + filters.addAll(Arrays.asList(filter)); List list = null; //读取工作表 XSSFSheet sheet = workbook.getSheetAt(0);//getSheet("Sheet1"); @@ -150,38 +174,48 @@ public class ExcelOperation { for (int i = excelInitRow; i < rowNum; i++) { //获得当前行 row = sheet.getRow(i); - cell = row.getCell(0); - if ("-".equals(getValue(cell)) || "".equals(getValue(cell))) { + cell = row.getCell(excelInitCell); + if ("-".equals(getValue(cell))) { continue; } //实例化 Object newObj = classz.newInstance(); Field[] fields = newObj.getClass().getDeclaredFields(); + int fieldSize = fields.length; boolean isAddObj = false; + int num = 0; //遍历每列的数据 - for (int j = 0; j < objColl; j++) { + for (int j = excelInitCell; j < objColl; j++) { cell = row.getCell(j); + if (j + num >= fieldSize) { + break; + } + num = getIndex(filters, fields, j + num, num); + if (num == -1) { + break; + } + Field tmpField = fields[j + num]; if (null != cell) { - fields[j].setAccessible(true); + tmpField.setAccessible(true); String cellString = getValue(cell); if (!"".equals(cellString)) { isAddObj = true; //int型 - if (fields[j].getType().toString().contains("Integer") ||fields[j].getType().toString().contains("int")) { + if (tmpField.getType().toString().contains("Integer") ||tmpField.getType().toString().contains("int")) { double dnum = Double.valueOf(cellString); int inum = (int) dnum; - fields[j].set(newObj, inum); + tmpField.set(newObj, inum); } //double型 - else if (fields[j].getType().toString().contains("Double") ||fields[j].getType().toString().contains("double")) { - fields[j].set(newObj, Double.valueOf(cellString)); + else if (tmpField.getType().toString().contains("Double") ||tmpField.getType().toString().contains("double")) { + tmpField.set(newObj, Double.valueOf(cellString)); } //boolean型 - else if (fields[j].getType().toString().contains("boolean") ||fields[j].getType().toString().contains("Boolean")) { - fields[j].set(newObj, Boolean.valueOf(cellString)); + else if (tmpField.getType().toString().contains("boolean") ||tmpField.getType().toString().contains("Boolean")) { + tmpField.set(newObj, Boolean.valueOf(cellString)); } else { - fields[j].set(newObj, cellString); + tmpField.set(newObj, cellString); } } @@ -201,6 +235,9 @@ public class ExcelOperation { */ private static String getValue(XSSFCell cell) { String value =""; + if (null == cell) { + return value; + } switch (cell.getCellType()) { case XSSFCell.CELL_TYPE_NUMERIC: value = String.valueOf(cell.getNumericCellValue()); @@ -216,4 +253,45 @@ public class ExcelOperation { } return value.trim(); } + + /** 过滤掉 filters中含有的 属性 + * @param filters + * @param fields + * @param cur + * @param num + * @return + */ + private static int getIndex(List filters, Field[] fields,int cur, int num) { + if (null == filters || filters.size() == 0) { + return num; + } + if (cur >= fields.length) { + return -1; + } + if (filters.contains(fields[cur].getName())) { + num++; + num = getIndex(filters, fields, cur+1, num); + } + return num; + } + /** 过滤掉 filters中含有的 属性 + * @param filters + * @param fields + * @param cur + * @param num + * @return + */ + private static int getWriteIndex(List filters, Field[] fields,int cur, int num) { + if (null == filters || filters.size() == 0) { + return num; + } + if (cur >= fields.length) { + return 1; + } + if (filters.contains(fields[cur].getName())) { + num--; + num = getWriteIndex(filters, fields, cur+1, num); + } + return num; + } }