package cn.com.goldenwater.dcproj.utils.export; import cn.com.goldenwater.dcproj.dao.AttAdBaseDao; import cn.com.goldenwater.dcproj.dto.AdUpDto; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.io.OutputStream; import java.util.*; import static cn.com.goldenwater.dcproj.utils.export.FieldFormat.formatCheckNull; /** * @ClassName CreateExcelHelper * @Author liyz * @Date 2019/3/15 16:27 * @Version 1.0 **/ @Component public class CreateExcelHelper { /** * 用于获取行政村名称 */ @Autowired private AttAdBaseDao attAdBaseDao; /** * 创建Excel公共方法 * * @param tab * @param fileName * @param os * @param dataList * @return * @throws Exception */ public ExportAbstract export(String tab, String fileName, OutputStream os, List> dataList) throws Exception { ExportAbstract exportZ = new ExcelExport(); // 容器1 Hashtable index_h = new Hashtable(); // 容器2 ArrayList rSearchfields = new ArrayList(); // 参数设置,根据tab setDataInfo(index_h, rSearchfields, tab); exportZ.fileName = fileName; exportZ.bdata = true; exportZ.index_h = index_h; exportZ.rSearchfields = rSearchfields; // 生成Excel数据 exportZ.ExportHead(os, dataList); exportZ.ContinueExport(dataList); // XLSTransformer former = new XLSTransformer(); return exportZ; } /** * 根据tab设置数据 * * @param index_h * @param rSearchfields * @param tab */ public static void setDataInfo(Hashtable index_h, ArrayList rSearchfields, String tab) { String[] index_h_s = TableFieldUtil.map.get(tab); if (index_h_s != null) { for (String str : index_h_s) { Column col = new Column(); int i = 0; col.setCode(str.split(",")[i++]); col.setName(str.split(",")[i++]); String width = str.split(",")[i++]; col.setGridwidth(Integer.parseInt("".equals(width) ? "80" : width)); col.setType(str.split(",")[i++]); index_h.put(col.getCode(), col); rSearchfields.add(col.getCode()); } } } //------------------------------- 其他业务方法 ------------------------------------------- /** * 通过行政编码获取行政村名称 * * @param adCode 行政区划(全称) * @return 行政村名称 */ public String getAdName(String adCode) { try { AdUpDto adUpDto = attAdBaseDao.fingAdUp(adCode); if (adUpDto != null) { return adUpDto.getAdFullName(); } } catch (Exception e) { } return ""; } /** * 通过行政编码获取 省 市 县 * * @param adCode 行政区划(全称) * @return 行政村名称 */ public HashMap getAdNames(String adCode) { HashMap map = new HashMap<>(3); String province = ""; String city = ""; String county = ""; try { if (adCode != null) { AdUpDto obj = attAdBaseDao.fingAdUp(adCode.substring(0, 2) + "0000000000"); if (obj != null) { province = obj.getAdName(); } obj = attAdBaseDao.fingAdUp(adCode.substring(0, 4) + "00000000"); if (obj != null) { city = obj.getAdName(); } obj = attAdBaseDao.fingAdUp(adCode.substring(0, 6) + "000000"); if (obj != null) { county = obj.getAdName(); } } } catch (Exception e) { e.printStackTrace(); } map.put("province", formatCheckNull(province).toString()); map.put("city", formatCheckNull(city).toString()); map.put("county", formatCheckNull(county).toString()); return map; } }