当前位置: 移动技术网 > IT编程>开发语言>Java > java web开发中大量数据导出Excel超时(504)问题解决

java web开发中大量数据导出Excel超时(504)问题解决

2019年07月22日  | 移动技术网IT编程  | 我要评论

超图,中法混血儿alonzo,本票样本

import java.io.ioexception;
import java.io.outputstream;
import java.lang.reflect.field;
import java.text.simpledateformat;
import java.util.date;
import java.util.hashmap;
import java.util.list;
import java.util.map;
import java.util.map.entry;
import javax.servlet.http.httpservletresponse;
import org.apache.commons.lang3.stringutils;
import org.joda.time.datetime;
import com.travelzen.framework.net.http.tzhttpclient;
import com.travelzen.tops.front.ota.member.item.customeritem;
public class csv {
  /**
   * 目标输出流
   */
  private outputstream stream;
  /**
   * 表头
   */
  private map<string,string> fields;

  /**
   * 数据源model所有字段map
   */
  private static map<string, field> fieldmap = new hashmap<>();
  public csv(httpservletresponse response,map<string,string> fields,string filename,class<?> clz) throws ioexception{
    if(response == null || fields == null || filename == null || clz == null)
      throw new illegalargumentexception();
    getfieldmap(clz,fieldmap);
    this.stream = response.getoutputstream();
    this.fields = fields;
    response.setcontenttype("application/octet-stream;charset=gbk");
    response.setheader("content-disposition", "attachment;filename="+ filename);
    //写表头,生成指定名字的文件,返回客户端
    stringbuilder hb = new stringbuilder();
    for(entry<string, string> e : fields.entryset())
      hb.append(e.getvalue()+",");
    stream.write(hb.substring(0, hb.length() - 1).getbytes("gbk"));
    stream.flush();
  }
  /**
   * 往表格中插入记录
   */
  public void write(list<object> data) throws illegalargumentexception, illegalaccessexception, ioexception{
    for(object o : data){
      stringbuilder sb = new stringbuilder();
      sb.append("\n");
      for(string field : fields.keyset()){
        field f = fieldmap.get(field);
        f.setaccessible(true);
        object value = f.get(o);
        if(value == null || stringutils.isblank(value.tostring())){
          sb.append(" ,");
        } else if (f.gettype() == date.class) {
          sb.append(new simpledateformat("yyyy-mm-dd hh:mm:ss").format(value) + ",");
        } else if (f.gettype() == datetime.class) {
          sb.append(((datetime)value).tostring("yyyy-mm-dd hh:mm:ss") + ",");
        } else {
          string tmp = value.tostring();
          if(tmp.contains(","))
            tmp = tmp.replace(",", "\",\"");
          sb.append(value.tostring() + ",");
        }
      }
      stream.write(sb.substring(0, sb.length() - 1).getbytes("gbk"));
      stream.flush();
    }
  }
  public void close() throws ioexception{
    stream.close();
  }
  private static <t extends object> void getfieldmap(class<t> clz, map<string, field> result) {
    for (field field : clz.getdeclaredfields()) {
      result.put(field.getname(), field);
    }
    if (clz.getsuperclass() != null) {
      getfieldmap(clz.getsuperclass(), result);
    }
  }
}

web开发中常见的准备excel数据需要从数据库查询数据,或者跨系统调用接口查询数据,耗费大量时间,因此未及时向浏览器返回数据,导致504超时。

本工具使用servletoutputstream分段的往浏览器flush数据。调用方式:先new csv(),传入指定参数,不断的调用wirte()方法往浏览器写入数据,最后调用close方法关闭流。

本工具导出的文件格式为.csv文件,windows office工具默认编码为asci,wps会匹配各种编码,libreoffice calc可以指定编码,故此设置编码为gbk,兼容三种excel软件,也可根据自身需求设置编码。

本工具只处理了csv中”,”的转码,对于双引号并未处理。

希望本文能够对遇到此问题的朋友能有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网