XLSView 추가
parent
3294d4db62
commit
0f467b4158
@ -1,14 +1,51 @@
|
||||
package cokr.xit.base.file.web;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.URLEncoder;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.jxls.common.Context;
|
||||
import org.jxls.util.JxlsHelper;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.web.servlet.view.AbstractView;
|
||||
|
||||
import cokr.xit.foundation.Assert;
|
||||
|
||||
public class XLSView extends AbstractView {
|
||||
@Override
|
||||
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest hreq, HttpServletResponse hresp) throws Exception {
|
||||
try (InputStream input = getInputStream(model)) {
|
||||
String filename = getFilename(model);
|
||||
|
||||
Context ctx = new Context();
|
||||
model.forEach(ctx::putVar);
|
||||
|
||||
String charset = "UTF-8";
|
||||
hresp.setCharacterEncoding(charset);
|
||||
hresp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||
hresp.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(filename, charset) +"\"");
|
||||
|
||||
JxlsHelper.getInstance().processTemplate(input, hresp.getOutputStream(), ctx);
|
||||
}
|
||||
}
|
||||
|
||||
private InputStream getInputStream(Map<String, Object> model) throws Exception {
|
||||
String path = (String)model.remove("template");
|
||||
if (Assert.isEmpty(path))
|
||||
throw new RuntimeException("'template' not found");
|
||||
|
||||
return new ClassPathResource(path).getInputStream();
|
||||
}
|
||||
|
||||
private String getFilename(Map<String, Object> model) {
|
||||
return Assert.ifEmpty(
|
||||
(String)model.remove("filename"),
|
||||
() -> new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date()) + ".xlsx"
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue