'新增导出功能'

This commit is contained in:
2019-04-24 17:36:55 +08:00
parent e13588f3ab
commit 936d4dc992
9 changed files with 162 additions and 173 deletions

View File

@@ -0,0 +1,58 @@
package net.tccn.base;
import com.lxyer.excel.poi.ExcelKit;
import org.apache.poi.ss.usermodel.Workbook;
import org.redkale.convert.Convert;
import org.redkale.net.http.*;
import org.redkale.util.AnyValue;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;
/**
* @author: liangxianyou
*/
public class MetaRender implements HttpRender<HttpScope> {
@Override
public void init(HttpContext context, AnyValue config) {
}
@Override
public <V extends HttpScope> void renderTo(HttpRequest request, HttpResponse response, Convert convert, V scope) {
Map<String, Object> attr = scope.getAttributes();
if ("excel".equals(scope.getReferid())) {
List list = (List) attr.get("data");
Kv heads = (Kv) attr.get("heads");
String fileName = (String) attr.get("fileName");
try {
Workbook workbook = ExcelKit.exportExcel(list, heads);
File file = new File(String.format("tmp/%s.xls", fileName));
file.getParentFile().mkdirs();
if (file.exists()) file.delete();
FileOutputStream fos = new FileOutputStream(file);
workbook.write(fos);
try {
fos.close();
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
response.finish(file);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public Class<HttpScope> getType() {
return HttpScope.class;
}
}