Преглед на файлове

优化工具类

shuiqilin
zhangxin преди 1 година
родител
ревизия
2de7b9b9d2
променени са 2 файла, в които са добавени 241 реда и са изтрити 36 реда
  1. 71
    36
      src/main/java/com/qtzl/alterSales/manager/tools/ExcelUtils.java
  2. 170
    0
      src/main/java/com/qtzl/alterSales/manager/tools/JsonToXmlUtil.java

+ 71
- 36
src/main/java/com/qtzl/alterSales/manager/tools/ExcelUtils.java Целия файл

@@ -6,6 +6,7 @@ import com.alibaba.fastjson.JSONObject;
import com.qtzl.alterSales.manager.model.protocol.center.excel.ExcelClassField;
import com.qtzl.alterSales.manager.model.protocol.center.excel.ExcelExport;
import com.qtzl.alterSales.manager.model.protocol.center.excel.ExcelImport;
import com.qtzl.alterSales.manager.service.third.FileConfig;
import com.qtzl.alterSales.manager.service.third.FmsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
@@ -399,22 +400,22 @@ public class ExcelUtils {
return cell.getCellFormula();
}

public static <T> String exportTemplate(HttpServletResponse response, String fileName,String filePath, Class<T> clazz) {
return exportTemplate(response, fileName,filePath, fileName, clazz, false);
public static <T> String exportTemplate(HttpServletResponse response, String fileName, String filePath, Class<T> clazz) {
return exportTemplate(response, fileName, filePath, fileName, clazz, false);
}

public static <T> void exportTemplate(HttpServletResponse response, String fileName,String filePath, String sheetName,
public static <T> void exportTemplate(HttpServletResponse response, String fileName, String filePath, String sheetName,
Class<T> clazz) {
exportTemplate(response, fileName,filePath, sheetName, clazz, false);
exportTemplate(response, fileName, filePath, sheetName, clazz, false);
}

public static <T> void exportTemplate(HttpServletResponse response, String fileName,String filePath, Class<T> clazz,
public static <T> void exportTemplate(HttpServletResponse response, String fileName, String filePath, Class<T> clazz,
boolean isContainExample) {
exportTemplate(response, fileName,filePath, fileName, clazz, isContainExample);
exportTemplate(response, fileName, filePath, fileName, clazz, isContainExample);
}

public static <T> String exportTemplate(HttpServletResponse response, String fileName,String filePath, String sheetName,
Class<T> clazz, boolean isContainExample) {
public static <T> String exportTemplate(HttpServletResponse response, String fileName, String filePath, String sheetName,
Class<T> clazz, boolean isContainExample) {
// 获取表头字段
List<ExcelClassField> headFieldList = getExcelClassFieldList(clazz);
// 获取表头数据和示例数据
@@ -436,7 +437,7 @@ public class ExcelUtils {
sheetDataList.add(exampleList);
}
// 导出数据
return export(response, fileName,filePath, sheetName, sheetDataList, selectMap);
return export(response, fileName, filePath, sheetName, sheetDataList, selectMap);
}

private static <T> List<ExcelClassField> getExcelClassFieldList(Class<T> clazz) {
@@ -549,7 +550,7 @@ public class ExcelUtils {
}
Map<String, List<List<Object>>> map = new HashMap<>();
map.put(file.getName(), sheetData);
export(null, file, file.getName(),null, map, null);
export(null, file, file.getName(), null, map, null);
}

/**
@@ -671,61 +672,95 @@ public class ExcelUtils {
return map;
}

public static String exportEmpty(HttpServletResponse response, String fileName,String filePath) {
public static String exportEmpty(HttpServletResponse response, String fileName, String filePath) {
List<List<Object>> sheetDataList = new ArrayList<>();
List<Object> headList = new ArrayList<>();
headList.add("导出无数据");
sheetDataList.add(headList);
return export(response, fileName,filePath, sheetDataList);
return export(response, fileName, filePath, sheetDataList);
}

public static String export(HttpServletResponse response, String fileName,String filePath, List<List<Object>> sheetDataList) {
return export(response, fileName,filePath, fileName, sheetDataList, null);
public static String export(HttpServletResponse response, String fileName, String filePath, List<List<Object>> sheetDataList) {
return export(response, fileName, filePath, fileName, sheetDataList, null);
}

public static void exportManySheet(HttpServletResponse response, String fileName,String filePath, Map<String, List<List<Object>>> sheetMap) {
export(response, null, fileName,filePath, sheetMap, null);
public static void exportManySheet(HttpServletResponse response, String fileName, String filePath, Map<String, List<List<Object>>> sheetMap) {
export(response, null, fileName, filePath, sheetMap, null);
}


public static void export(HttpServletResponse response, String fileName,String filePath, String sheetName,
public static void export(HttpServletResponse response, String fileName, String filePath, String sheetName,
List<List<Object>> sheetDataList) {
export(response, fileName,filePath, sheetName, sheetDataList, null);
export(response, fileName, filePath, sheetName, sheetDataList, null);
}

public static String export(HttpServletResponse response, String fileName,String filePath, String sheetName,
List<List<Object>> sheetDataList, Map<Integer, List<String>> selectMap) {
public static String export(HttpServletResponse response, String fileName, String filePath, String sheetName,
List<List<Object>> sheetDataList, Map<Integer, List<String>> selectMap) {

Map<String, List<List<Object>>> map = new HashMap<>();

map.put(sheetName, sheetDataList);
return export(response, null, fileName,filePath, map, selectMap);
return export(response, null, fileName, filePath, map, selectMap);
}

public static <T, K> String export(HttpServletResponse response, String fileName,String filePath, List<T> list, Class<K> template) {

public static <T, K> String export(HttpServletResponse response, String fileName, String filePath, List<T> list, Class<K> template) {
// list 是否为空
boolean lisIsEmpty = list == null || list.isEmpty();
// 如果模板数据为空,且导入的数据为空,则导出空文件
if (template == null && lisIsEmpty) {
return exportEmpty(response, fileName,filePath);
return exportEmpty(response, fileName, filePath);

}
// 如果 list 数据,则导出模板数据
if (lisIsEmpty) {
return exportTemplate(response, fileName,filePath, template);
return exportTemplate(response, fileName, filePath, template);
}
// 导出数据
List<List<Object>> sheetDataList = getSheetData(list);
return export(response, fileName,filePath, sheetDataList);
return export(response, fileName, filePath, sheetDataList);
}
public static <T, K> String export(HttpServletResponse response, String fileName, String filePath, Map<String,List<T>> mapList, Class<K> template,Boolean pageSheet) {
// list 是否为空
boolean lisIsEmpty = mapList == null || mapList.isEmpty();
// 如果模板数据为空,且导入的数据为空,则导出空文件
if (template == null && lisIsEmpty) {
return exportEmpty(response, fileName, filePath);

}
// 如果 list 数据,则导出模板数据
if (lisIsEmpty) {
return exportTemplate(response, fileName, filePath, template);
}
Map<String, List<List<Object>>> map = new HashMap<>();
Set<String> strings = mapList.keySet();
for (String key : strings) {
List<T> sheetList = mapList.get(key);
List<List<Object>> sheetDataList = getSheetData(sheetList);
map.put(key, sheetDataList);
}



// for (int i = 0; i < page; i++) {
// List<T> sheetList = new ArrayList<>();
// for (int j =i*FileConfig.FILEMAXNUM; j <(i+1)*FileConfig.FILEMAXNUM; j++) {
// sheetList.add(list.get(j));
// }
// List<List<Object>> sheetDataList = getSheetData(sheetList);
// map.put(fileName+i, sheetDataList);
// }
return export(response, null, fileName, filePath, map, null);
}

public static String export(HttpServletResponse response, String fileName,String filePath, List<List<Object>> sheetDataList, Map<Integer, List<String>> selectMap) {
return export(response, fileName,filePath, fileName, sheetDataList, selectMap);
public static String export(HttpServletResponse response, String fileName, String filePath, List<List<Object>> sheetDataList, Map<Integer, List<String>> selectMap) {
return export(response, fileName, filePath, fileName, sheetDataList, selectMap);
}

private static String export(HttpServletResponse response, File file, String fileName,String filePath,
Map<String, List<List<Object>>> sheetMap, Map<Integer, List<String>> selectMap) {
private static String export(HttpServletResponse response, File file, String fileName, String filePath,
Map<String, List<List<Object>>> sheetMap, Map<Integer, List<String>> selectMap) {
// 整个 Excel 表格 book 对象
SXSSFWorkbook book = new SXSSFWorkbook();
SXSSFWorkbook book = new SXSSFWorkbook(FileConfig.FILEMAXNUM);
// 每个 Sheet 页
Set<Entry<String, List<List<Object>>>> entries = sheetMap.entrySet();
for (Entry<String, List<List<Object>>> entry : entries) {
@@ -779,13 +814,13 @@ public class ExcelUtils {
}
// 本地导出
FileOutputStream fos;
String updateFilePath =null;
String updateFilePath = null;
try {

// String path = ResourceUtils.getURL("classpath:").getPath();
updateFilePath = filePath+ fileName + "_" + UUID.randomUUID().toString() + ".xlsx";
file= new File(updateFilePath);
if(!file.isFile()) { //D
// String path = ResourceUtils.getURL("classpath:").getPath();
updateFilePath = filePath + fileName + "_" + UUID.randomUUID().toString() + ".xlsx";
file = new File(updateFilePath);
if (!file.isFile()) { //D
file.createNewFile(); //E
}
fos = new FileOutputStream(file);
@@ -999,7 +1034,7 @@ public class ExcelUtils {
if (StringUtils.isBlank(str)) {
return false;
}
if (StringUtils.isEmpty(str)){
if (StringUtils.isEmpty(str)) {
return false;
}
for (int i = str.length(); --i >= 0; ) {

+ 170
- 0
src/main/java/com/qtzl/alterSales/manager/tools/JsonToXmlUtil.java Целия файл

@@ -0,0 +1,170 @@
package com.qtzl.alterSales.manager.tools;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.*;
import java.util.Map.Entry;

public class JsonToXmlUtil {

/**
* xml转json字符串 注意:路径和字符串二传一另外一个传null<br>
* 方 法 名:xmlToJson <br>
* @param xmlPath xml路径(和字符串二传一,两样都传优先使用路径)
* @param xmlStr xml字符串(和路径二传一,两样都传优先使用路径)
* @return String
* @throws IOException
* @throws JDOMException
*/
@SuppressWarnings("unchecked")
public static String xmlToJson(String xmlPath,String xmlStr){
SAXBuilder sbder = new SAXBuilder();
Map<String, Object> map = new HashMap<String, Object>();
Document document;
try {
if(xmlPath!=null){
//路径
document = sbder.build(new File(xmlPath));
}else if(xmlStr!=null){
//xml字符
StringReader reader = new StringReader(xmlStr);
InputSource ins = new InputSource(reader);
document = sbder.build(ins);
}else{
return "{}";
}
//获取根节点
Element el = document.getRootElement();
List<Element> eList = el.getChildren();
Map<String, Object> rootMap = new HashMap<String, Object>();
//得到递归组装的map
rootMap = xmlToMap(eList,rootMap);
map.put(el.getName(), rootMap);
//将map转换为json 返回
return JSON.toJSONString(map);
} catch (Exception e) {
return "{}";
}
}
/**
* json转xml<br>
* 方 法 名:jsonToXml <br>
* @param json
* @return String
*/
public static String jsonToXml(String json){
try {
StringBuffer buffer = new StringBuffer();
buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
buffer.append("<base>");
JSONObject jObj = JSON.parseObject(json);
jsonToXmlstr(jObj,buffer);
buffer.append("</base>");
return buffer.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
/**
* json转str<br>
* 方 法 名:jsonToXmlstr <br>
* @param jObj
* @param buffer
* @return String
*/
public static String jsonToXmlstr(JSONObject jObj,StringBuffer buffer ){
Set<Entry<String, Object>> se = jObj.entrySet();
for( Iterator<Entry<String, Object>> it = se.iterator(); it.hasNext(); )
{
Entry<String, Object> en = it.next();
if(en.getValue() != null && en.getValue().getClass().getName().equals("com.alibaba.fastjson.JSONObject")){
buffer.append("<"+en.getKey()+">");
JSONObject jo = jObj.getJSONObject(en.getKey());
jsonToXmlstr(jo,buffer);
buffer.append("</"+en.getKey()+">");
}else if(en.getValue() != null && en.getValue().getClass().getName().equals("com.alibaba.fastjson.JSONArray")){
if (en.getKey().equals("extproperties")) {
JSONArray ja = jObj.getJSONArray(en.getKey());
Iterator<Object> it1 = ja.iterator();
List<String> list=new ArrayList<String>();
while (it1.hasNext()) {
String ob = (String) it1.next();
System.out.println(ob);
}
}else {
JSONArray jarray = jObj.getJSONArray(en.getKey());
for (int i = 0; i < jarray.size(); i++) {
buffer.append("<"+en.getKey()+">");
JSONObject jsonobject = jarray.getJSONObject(i);
jsonToXmlstr(jsonobject,buffer);
buffer.append("</"+en.getKey()+">");
}
}

}else if(en.getValue() != null && en.getValue().getClass().getName().equals("java.lang.String")){
buffer.append("<"+en.getKey()+">"+en.getValue());
buffer.append("</"+en.getKey()+">");
} else if(en.getValue() != null && en.getValue().getClass().getName().equals("java.lang.Integer")){
buffer.append("<"+en.getKey()+">"+en.getValue());
buffer.append("</"+en.getKey()+">");
}else{
buffer.append("<"+en.getKey()+">"+"");
buffer.append("</"+en.getKey()+">");
}

}
return buffer.toString();
}


/**
* 节点转map<br>
* 方 法 名:xmlToMap <br>
* @param eList
* @param map
* @return Map<String,Object>
*/
@SuppressWarnings("unchecked")
public static Map<String, Object> xmlToMap(List<Element> eList,Map<String, Object> map){
for (Element e : eList) {
Map<String, Object> eMap = new HashMap<String, Object>();
List<Element> elementList = e.getChildren();
if(elementList!=null&&elementList.size()>0){
eMap = xmlToMap(elementList,eMap);
Object obj = map.get(e.getName());
if(obj!=null){
List<Object> olist = new ArrayList<Object>();
if(obj.getClass().getName().equals("java.util.HashMap")){
olist.add(obj);
olist.add(eMap);

}else if(obj.getClass().getName().equals("java.util.ArrayList")){
olist = (List<Object>)obj;
olist.add(eMap);
}
map.put(e.getName(), olist);
}else{
map.put(e.getName(), eMap);
}
}else{
map.put(e.getName(), e.getValue());
}
}
return map;
}




}

Loading…
Отказ
Запис