优化代码实现

This commit is contained in:
2018-07-07 13:36:10 +08:00
parent fb9e72365f
commit e719a6e055
6 changed files with 212 additions and 161 deletions

View File

@@ -1,41 +1,36 @@
package excel;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* Created by liangxianyou at 2018/6/27 16:15.
*/
public class ExcelExportKit {
public static void main(String[] args) throws IOException {
}
public static final ThreadLocal<SimpleDateFormat> SDF_THREADLOCAL = new ThreadLocal<>();
//-------------非poi导出---------------
private static <T> String exportExcel(Map<String, String> head, List<T> rows, BiFunction fun) {
public static <T,U,R> StringBuilder exportExcel(Map<String, String> head, List<T> rows, BiFunction<T,U,R> fun) {
long start = System.currentTimeMillis();
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
buf.append(EXCEL_HEAD);
buf.append(createBody(head, rows, fun));//body
buf.append(EXCEL_END);
System.out.println(String.format("数据:%s条耗时%s ms", rows.size(), System.currentTimeMillis() - start));
return buf.toString();
return buf;
}
private static <T> StringBuffer createBody(Map<String, String> head, List<T> rows, BiFunction fun) {
StringBuffer buf = new StringBuffer();
private static <T,U,R> StringBuilder createBody(Map<String, String> head, List<T> rows, BiFunction<T,U,R> fun) {
StringBuilder buf = new StringBuilder();
//table-head
buf.append("<Row ss:StyleID=\"s50\" ss:Height=\"16\">");
head.forEach((k,v)->{
@@ -47,13 +42,18 @@ public class ExcelExportKit {
rows.forEach(x->{
buf.append("<Row>");
head.forEach((k,v)->{
Object value = fun.apply(x, v);
Object value = fun.apply(x, (U)v);
if (value == null) value = "";
if (value instanceof Number){
buf.append("<Cell><Data ss:Type=\"Number\">"+value+"</Data> </Cell>");
}else if (value instanceof Date){
buf.append("<Cell><Data ss:Type=\"Date\">"+
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value) +
"</Data> </Cell>"
SimpleDateFormat dateFormat = SDF_THREADLOCAL.get();
if (dateFormat == null) {//ThreadLocal 存贮 simpleDateFormat 对象
SDF_THREADLOCAL.set(dateFormat = new SimpleDateFormat());
}
buf.append(
"<Cell><Data ss:Type=\"String\">"+ dateFormat.format(value) +"</Data> </Cell>"
);
}else {
buf.append("<Cell><Data ss:Type=\"String\">"+value+"</Data> </Cell>");
@@ -65,69 +65,10 @@ public class ExcelExportKit {
return buf;
}
public static <T> void exportExcel(Map<String,String> head, List<T> rows, File file, BiFunction fun) throws IOException {
strToFile(exportExcel(head, rows, fun), file);
}
public static <T,U,R> void exportExceltoFile(Map<String,String> head, List<T> rows, File file, BiFunction<T,U,R> fun) throws IOException {
StringBuilder buf = exportExcel(head, rows, fun);
/**
* excel 压缩导出
* @param head
* @param rows
* @param file
* @param fun
* @param <T>
* @throws IOException
*/
public static <T> void exportExcelZip(Map<String,String> head, List<T> rows, File file, BiFunction fun) throws IOException {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(zos);
ZipEntry zipEntry = new ZipEntry(file.getName().split("[.]")[0]+".xls");
zos.putNextEntry(zipEntry);
bos.write(exportExcel(head, rows, fun).getBytes());
bos.flush();
bos.close();
zos.close();
}
/*
//使用redkale框架情况下的 导出
public static <T> boolean exportExcel(Map head, List<T> rows, HttpResponse response, String xlsName, BiFunction fun) throws IOException {
xlsName = new String(xlsName.getBytes("utf-8"),"iso-8859-1")+ ".xls";
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename="+xlsName);
response.finish(exportExcel(head, rows, fun).getBytes());
return true;
}*/
/**
* 非poi导出excel--【javax.servlet】
* @param head 表头
* @param rows 数据
* @param request
* @param response
* @param xlsName 导出的文件名称
* @return
* @throws IOException
*/
public static <T> boolean exportExcel(Map head, List<T> rows, HttpServletRequest request, HttpServletResponse response, String xlsName, BiFunction fun) throws IOException {
if(request.getHeader("user-agent") != null && request.getHeader("user-agent").indexOf("MSIE") != -1) {
xlsName = java.net.URLEncoder.encode(xlsName,"utf-8") + ".xls";
} else {
xlsName = new String(xlsName.getBytes("utf-8"),"iso-8859-1")+ ".xls";
}
OutputStream os = response.getOutputStream();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename="+xlsName);
os.write(exportExcel(head, rows, fun).getBytes());
return true;
strToFile(buf.toString(), file);
}
public static void strToFile(String entityBody, File file) throws IOException {
@@ -141,7 +82,6 @@ public class ExcelExportKit {
out.close();
}
public static class TC {
public static final BiFunction BEAN = (t,u)->{
Field field = null;