当前位置 : 主页 > 编程语言 > java >

用java流快速生成xml文件,效率比poi高几十倍

来源:互联网 收集:自由互联 发布时间:2021-06-28
java代码,调用下面.stg文件 package cbh.tool.xls;import cbh.tool.exception.NumberOutOfRangeException;import com.fasterxml.jackson.databind.ObjectMapper;import org.stringtemplate.v4.ST;import org.stringtemplate.v4.STGroup;import org.st
java代码,调用下面.stg文件
package cbh.tool.xls;

import cbh.tool.exception.NumberOutOfRangeException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.STGroupFile;

import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * Created by huxin on 2016/1/18.
 */
public class FileCompleteXLS {

    private STGroup stGroup;
    private File file;
    private FileOutputStream fos = null;
    private OutputStreamWriter osw = null;
    private Integer count = 0;
    public FileCompleteXLS() {
        stGroup = new STGroupFile("xls.stg");
    }

    public void buildFile(File file) throws IOException {
        this.file =file;
        buildOWSStream();
    }

    public void writeTail() throws IOException {
        ST st = stGroup.getInstanceOf("tail");
        osw.write(st.render());
        osw.flush();
    }

    public void writeHead() throws IOException {
        ST st = stGroup.getInstanceOf("header");
        osw.write(st.render());
    }

    public void writeSheetHead(String sheetName) throws IOException {
        count = 0;
        ST st1 = stGroup.getInstanceOf("worksheetHead");
        st1.add("sheetName",sheetName);
        osw.write(st1.render());
    }

    public void worksheetTail() throws IOException {
        ST st1 = stGroup.getInstanceOf("worksheetTail");
        osw.write(st1.render());
    }

    public void writeRow(List cuterData) throws IOException, NumberOutOfRangeException {
        if(count > 1000000) {
            throw new NumberOutOfRangeException();
        }
        cuterData.forEach(data -> {
            count ++;
            ST st = stGroup.getInstanceOf("row");
            st.add("mapValus",new ObjectMapper().convertValue(data,Map.class).values());
            try {
                osw.write(st.render());
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        osw.flush();
    }

    private void buildOWSStream() throws FileNotFoundException {
        if(file != null){
            fos = new FileOutputStream(file,true);
            osw = new OutputStreamWriter(fos);
        }else {
            throw new FileNotFoundException();
        }
    }

    public void close() throws IOException {
        this.fos.flush();
        this.osw.flush();
        this.fos.close();
        this.osw.close();
    }
}
ST4.0 .stg文件
//header
header(lists) ::= <<
\
 
\
 
\
 
  
\
  
    \
   
    \
    \
   
    2016-01-22T02:25:00Z\
    \
   
    2016-01-21T10:27:26Z\
    \
  
\
  
    \
   
    2052-10.1.0.5444\
    \
  
\
  
    \
   
    13830\
    \
   
    13920\
    \
   
    False\
    \
   
    False\
    \
  
\
  
    \
    \
  
\
  
    \
   
    
    
     
      \ }>\};separator="\n"> \
     
    
   
   
     \ 
    \
   
     \
    
      \
     
\
\
\ \ 0\ \ 0\ \ 100\ \ \ \ 3\ \ 8\ \ 4\ \ R9C4:R10C5\ \ \ \ False\ \ False\ \
\
\
>>
网友评论