easyexcel在使用`Safari`浏览器导出的问题

最近在使用easyexcel的时候,碰到一个小坑:

在使用Safari浏览器进行导出的时候,导出的文件会自动增加一个.xlw后缀。

解决办法

将原来的代码:

response.setContentType("application/vnd.ms-excel");

改成:

response.setContentType(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

然后所有浏览器都没有问题了。

附上easyexcel导出的工具类

public class ExcelUtil {

private static Logger logger = LoggerFactory.getLogger(ExcelUtil.class);

/**
* 导出
*
* @param list 导出数据
* @param excelName excel名称
* @param response response
* @param clazz clazz
*/
public static void export(List<? extends BaseRowModel> list,
String excelName, HttpServletResponse response,
Class<? extends BaseRowModel> clazz) {

ServletOutputStream out = null;
ExcelWriter writer = null;
try {
out = response.getOutputStream();
writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
String fileName = excelName +
new SimpleDateFormat("yyyy-MM-dd").format(new Date());

Sheet sheet2 = new Sheet(2, 3, clazz, "sheet", null);
writer.write(list, sheet2);
response.setCharacterEncoding("utf-8");
// response.setContentType("application/vnd.ms-excel");
response.setContentType(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

setFileDownloadHeader(response, fileName);

out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != writer) {
writer.finish();
}
if (null != out) {
try {
out.flush();
} catch (IOException e) {
logger.error("excel导出出错", e);
}
}
}
}

private static void setFileDownloadHeader(
HttpServletResponse response, String filename) {
String headerValue = "attachment;";
headerValue += " filename=\""
+ encodeURIComponent(filename) + "\";";
headerValue += " filename*=utf-8''"
+ encodeURIComponent(filename + ".xlsx");
response.setHeader("Content-Disposition", headerValue);
}

private static String encodeURIComponent(String value) {
try {
return URLEncoder.encode(value, "UTF-8")
.replaceAll("\\+", "%20");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
}
接口签名参数Map字典排序
/**
* 方法用途: 对所有传入参数按照字段名的 ASCII 码从小到大排序(字典序),并且生成url参数串
*
* @param paramsMap 要排序的Map对象
* @param urlEncode 是否需要URLENCODE
* @param keyToLower 是否需要将Key转换为全小写 true:key转化成小写,false:不转化
* @return
*/
public static String formatUrlMap(Map<String, Object> paramsMap,
boolean urlEncode, boolean keyToLower) {

String buff = "";
Map<String, Object> tmpMap = paramsMap;

try {
List<Map.Entry<String, Object>> infoIds =
new ArrayList<Map.Entry<String, Object>>(tmpMap.entrySet());

//对所有传入参数按照字段名的ASCII码从小到大排序(字典序)
Collections.sort(infoIds, new Comparator<Map.Entry<String, Object>>() {
public int compare(Map.Entry<String, Object> o1,
Map.Entry<String, Object> o2) {
return (o1.getKey()).toString().compareTo(o2.getKey());
}
});

//构造URL 键值对的格式
StringBuffer buf = new StringBuffer();
for (Map.Entry<String, Object> item : infoIds) {
if (StringUtils.isNotBlank(item.getKey())) {
String key = item.getKey();
String value = item.getValue().toString();
if (urlEncode) {
value = URLEncoder.encode(value, "utf-8");
}
if (keyToLower) {
buf.append(key.toLowerCase() + "=" + value);
} else {
buf.append(key + "=" + value);
}
buf.append("&");
}
}
buff = buf.toString();

if (StringUtils.isNotEmpty(buff)) {
buff = buff.substring(0, buff.length() - 1);
}
} catch (Exception e) {
e.printStackTrace();
return null;
}

return buff;
}

排序后可以进行其他处理,比如拼上nonce、时间戳等;

Java生成随机字符串
/**
* 描述: Java 随机生成字符串
*/
public class CreateRandomStr {

public static String createRandomStr1(int length){

String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

Random random = new Random();

StringBuffer stringBuffer = new StringBuffer();

for (int i = 0; i < length; i++) {

int number = random.nextInt(62);

stringBuffer.append(str.charAt(number));

}

return stringBuffer.toString();

}
}
IDEA运行项目出现Command line is too long.

在项目的.idea文件夹中的workspace.xml文件中找到<component name="PropertiesComponent">节点,添加以下属性:

<property name="dynamic.classpath" value="true" />

重启idea.

未完待续..

未完待续..