diff --git a/src/main/java/cokr/xit/base/file/web/DownloadView.java b/src/main/java/cokr/xit/base/file/web/DownloadView.java index fde05e5..c5a0027 100644 --- a/src/main/java/cokr/xit/base/file/web/DownloadView.java +++ b/src/main/java/cokr/xit/base/file/web/DownloadView.java @@ -47,18 +47,15 @@ public class DownloadView extends AbstractView { contentType = Assert.ifEmpty(URLConnection.guessContentTypeFromName(filename), "application/octet-stream"); } 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.setContentType(contentType); 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()); - } catch (Exception e) { - throw Assert.runtimeException(e); } finally { clear(model); } diff --git a/src/main/java/cokr/xit/base/file/web/XLSView.java b/src/main/java/cokr/xit/base/file/web/XLSView.java index bbdd0ff..3ef856c 100644 --- a/src/main/java/cokr/xit/base/file/web/XLSView.java +++ b/src/main/java/cokr/xit/base/file/web/XLSView.java @@ -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 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 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 model) { + return Assert.ifEmpty( + (String)model.remove("filename"), + () -> new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date()) + ".xlsx" + ); } } \ No newline at end of file