XLSView 추가

master
mjkhan21 12 months ago
parent 3294d4db62
commit 0f467b4158

@ -47,18 +47,15 @@ public class DownloadView extends AbstractView {
contentType = Assert.ifEmpty(URLConnection.guessContentTypeFromName(filename), "application/octet-stream"); contentType = Assert.ifEmpty(URLConnection.guessContentTypeFromName(filename), "application/octet-stream");
} }
String disposition = Assert.ifEmpty((String)model.get("disposition"), "attachment"); String disposition = Assert.ifEmpty((String)model.get("disposition"), "attachment");
Number length = getLength(model);
if (length == null)
throw new IllegalArgumentException("Unable to determine the length");
hresp.setCharacterEncoding("UTF-8"); hresp.setCharacterEncoding("UTF-8");
hresp.setContentType(contentType); hresp.setContentType(contentType);
hresp.setHeader("Content-Disposition", disposition + "; filename=\"" + URLEncoder.encode(filename, "UTF-8") +"\""); hresp.setHeader("Content-Disposition", disposition + "; filename=\"" + URLEncoder.encode(filename, "UTF-8") +"\"");
hresp.setContentLengthLong(length.longValue()); Number length = getLength(model);
if (length != null)
hresp.setContentLengthLong(length.longValue());
FileCopyUtils.copy(stream, hresp.getOutputStream()); FileCopyUtils.copy(stream, hresp.getOutputStream());
} catch (Exception e) {
throw Assert.runtimeException(e);
} finally { } finally {
clear(model); clear(model);
} }

@ -1,14 +1,51 @@
package cokr.xit.base.file.web; 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 java.util.Map;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 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 org.springframework.web.servlet.view.AbstractView;
import cokr.xit.foundation.Assert;
public class XLSView extends AbstractView { public class XLSView extends AbstractView {
@Override @Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest hreq, HttpServletResponse hresp) throws Exception { 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…
Cancel
Save