'新增导出功能'
This commit is contained in:
58
src/main/java/net/tccn/base/MetaRender.java
Normal file
58
src/main/java/net/tccn/base/MetaRender.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user