JxlsUtils.java 文件源码

java
阅读 28 收藏 0 点赞 0 评论 0

项目:simter-jxls-ext 作者:
/**
 * Generate a {@link Response.ResponseBuilder} instance
 * and render the excel template with the specified data to its output stream.
 *
 * @param template the excel template, can be xlsx or xls format
 * @param data     the data
 * @param filename the download filename of the response
 * @return the instance of {@link Response.ResponseBuilder} with the excel data
 * @throws RuntimeException if has IOException or UnsupportedEncodingException inner
 */
public static Response.ResponseBuilder renderTemplate2Response(InputStream template, Map<String, Object> data,
                                                               String filename) {
  StreamingOutput stream = (OutputStream output) -> {
    // Convert to jxls Context
    Context context = convert2Context(data);

    // Add default functions
    addDefault(context);

    // render
    renderByJxls(template, output, context);
  };

  // create response
  Response.ResponseBuilder builder = Response.ok(stream);
  if (filename != null) {
    try {
      builder.header("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(filename, "UTF-8") + "\"");
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException(e.getMessage(), e);
    }
  }
  return builder;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号