XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poiartifactId>
<version>3.9version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poi-ooxmlartifactId>
<version>3.9version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poi-ooxml-schemasartifactId>
<version>3.9version>
dependency>
<dependency>
<groupId>org.apache.xmlbeansgroupId>
<artifactId>xmlbeansartifactId>
<version>2.6.0version>
dependency>
<dependency>
<groupId>staxgroupId>
<artifactId>stax-apiartifactId>
<version>1.0.1version>
dependency>
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelUtil
{
public static Workbook fillExcelData(List
import java.util.ArrayList;
import java.util.List;
/**
* 字符串工具类
* @author
*
*/
public class StringUtil {
/**
* 判断是否为空
* @param str
* @return
*/
public static boolean isEmpty(String str){
if(str==null||"".equals(str.trim())){
return true;
}else{
return false;
}
}
/**
* 判断是否不为空
* @param str
* @return
*/
public static boolean isNotEmpty(String str){
if((str!=null)&&!"".equals(str.trim())){
return true;
}else{
return false;
}
}
/**
* 格式化模糊查询
* @param str
* @return
*/
public static String formatLike(String str){
if(isNotEmpty(str)){
return "%"+str+"%";
}else{
return null;
}
}
/**
* 排除list空值
* @param list
* @return
*/
public static List filterWhite(List list){
List resultList=new ArrayList();
for(String l:list){
if(isNotEmpty(l)){
resultList.add(l);
}
}
return resultList;
}
}
数据格式转换类
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.PropertyUtilsBean;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class DataConverterUtil {
/**
* map转对象
*
* @param map
* @param beanClass
* @return
*/
public static T map2Object(Map map, Class beanClass) {
if (map == null)
return null;
T obj = null;
try {
obj = beanClass.newInstance();
org.apache.commons.beanutils.BeanUtils.populate(obj, map);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return obj;
}
public static Map beanToMap(Object obj) {
Map params = new HashMap(0);
try {
PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
PropertyDescriptor[] descriptors = propertyUtilsBean
.getPropertyDescriptors(obj);
for (int i = 0; i < descriptors.length; i++) {
String name = descriptors[i].getName();
if (!"class".equals(name)) {
params.put(name,
propertyUtilsBean.getNestedProperty(obj, name));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return params;
}
/**
* 对象转Json
*
* @param obj
* @return
*/
public static String object2Json(Object obj) {
ObjectMapper mapper = new ObjectMapper();
// 忽略空属性
mapper.setSerializationInclusion(Include.NON_NULL);
try {
return mapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
/**
* 对象转Json
*
* @param obj
* @return
*/
public static String object2JsonWithNULL(Object obj) {
ObjectMapper mapper = new ObjectMapper();
// 忽略空属性
try {
return mapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
/**
* json转对象
*
* @param json
* @param objectClass
* @return
*/
public static T json2Object(String json, Class objectClass) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(json, objectClass);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* list 转 字符转,
*
* @param list
* @param 分隔符
* @return
*/
public static String list2String(List list, String spit) {
if (list == null || list.isEmpty()) {
return "";
}
String result = "";
for (int i = 0; i < list.size(); i++) {
String tmp = list.get(i);
if ("".equals(tmp)) {
continue;
}
if (result.equals("")) {
result = result + tmp;
} else {
result = result + spit + tmp;
}
}
return result;
}
/**
* list<对象>转list
public static List
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Workbook;
public class ResponseUtil {
public static void write(HttpServletResponse response,Object o)throws Exception{
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
out.print(o.toString());
out.flush();
out.close();
}
//导出Excel
public static void export(HttpServletResponse response,Workbook wb,String fileName)throws Exception{
response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("utf-8"),"iso8859-1"));
response.setContentType("application/ynd.ms-excel;charset=UTF-8");
OutputStream out=response.getOutputStream();
wb.write(out);
out.flush();
out.close();
}
}
/**
* @Description: (导出用户列表)
*/
@RequestMapping(value = "/exportTask", method = RequestMethod.POST)
@ResponseBody
public String exportTask(
@RequestBody ExportRequestDto exportRequest,
HttpServletResponse response) {
Workbook wb = userService.exportTask(exportRequest.getUserDto(), exportRequest.getHeaders(),
exportRequest.getKeys());
try {
ResponseUtil.export(response, wb, "用户列表.xlsx");
} catch (Exception e) {
log.info("导出excel出现异常!");
e.printStackTrace();
}
return null;
}
{
"userDto":{
"user":"xxx"
},
"headers":["id","标题"],
"keys":["id","title"]
}
喜欢请点赞,谢谢^^