excel超大文件写入

更新时间:2023-07-20 20:35:06 阅读: 评论:0

excel超⼤⽂件写⼊
这种⽅式可以写超⼤的excel⽂件,如果直接采⽤poi写⼊⽅式,⽂件超过1M就需要配置tomcat内存,⽂件越⼤配置需求就越⼤,采⽤这种⽅式不需要对tomcat 做任何配置。
package bile.xls;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import org.apache.poi.ss.urmodel.DateUtil;
财务内控import org.apache.poi.ss.urmodel.IndexedColors;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.urmodel.XSSFCellStyle;
import org.apache.poi.xssf.urmodel.XSSFDataFormat;
import org.apache.poi.xssf.urmodel.XSSFFont;
import org.apache.poi.xssf.urmodel.XSSFSheet;
import org.apache.poi.xssf.urmodel.XSSFWorkbook;
public class BigGridDemo {
public static void main(String[] args) {
夸美女漂亮的幽默句子
try {
// 第⼀步.创建⼀个临时的 excel ⽂件,配置单元格属性,数值格式。
FileOutputStream os;
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("Big Grid");
os = new FileOutputStream("d:/template.xlsx");
wb.write(os);袜子英语
os.clo();
Map<String, XSSFCellStyle> styles = createStyles(wb);
String sheetRef = PackagePart().getPartName().getName();
// 第⼆步,⽣成xml数据临时⽂件
File tmp = ateTempFile("sheet", ".xml");
Writer fw = new OutputStreamWriter(new FileOutputStream(tmp), "UTF-8");
generate(fw, styles);
fw.clo();
/
/ 第三步,创建ZIP输出流,将xml数据临时⽂件数据写⼊到ZIP⽂件中。
FileOutputStream out = new FileOutputStream("d:/big-grid.xlsx");
substitute(new File("d:/template.xlsx"), tmp, sheetRef.substring(1), out);
out.clo();
} catch (Exception e) {
e.printStackTrace();
e.printStackTrace();
}
}
/**
* 创建样式表
*/
private static Map<String, XSSFCellStyle> createStyles(XSSFWorkbook wb){
Map<String, XSSFCellStyle> styles = new HashMap<String, XSSFCellStyle>();
经典小说排行XSSFDataFormat fmt = wb.createDataFormat();
XSSFCellStyle style1 = wb.createCellStyle();
style1.tAlignment(XSSFCellStyle.ALIGN_RIGHT);
style1.Format("0.0%"));
styles.put("percent", style1);
XSSFCellStyle style2 = wb.createCellStyle();
style2.tAlignment(XSSFCellStyle.ALIGN_CENTER);
style2.Format("0.0X"));
styles.put("coeff", style2);
XSSFCellStyle style3 = wb.createCellStyle();
style3.tAlignment(XSSFCellStyle.ALIGN_RIGHT);
style3.Format("$#,##0.00"));
styles.put("currency", style3);
XSSFCellStyle style4 = wb.createCellStyle();
style4.tAlignment(XSSFCellStyle.ALIGN_RIGHT);
style4.Format("mmm dd"));
styles.put("date", style4);
XSSFCellStyle style5 = wb.createCellStyle();
XSSFFont headerFont = wb.createFont();
headerFont.tBold(true);
style5.tFillForegroundColor(IndexedColors.GREY_Index());
style5.tFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
style5.tFont(headerFont);
styles.put("header", style5);
return styles;
}
private static void generate(Writer out, Map<String, XSSFCellStyle> styles) throws Exception {
Random rnd = new Random();
Calendar calendar = Instance();
SpreadsheetWriter sw = new SpreadsheetWriter(out);
sw.beginSheet();
//inrt header row
sw.inrtRow(0);
int styleIndex = ("header").getIndex();
//write data rows
for (int rownum = 1; rownum < 20; rownum++) {
sw.inrtRow(rownum);
小狗怎么画简笔画
}
}
/**
*
* @param zipfile the template file
* @param tmpfile the XML file with the sheet data
* @param entry the name of the sheet entry to substitute, e.g. xl/l
* @param out the stream to write the result to
*/
private static void substitute(File zipfile, File tmpfile, String entry, OutputStream out) throws IOException {          ZipFile zip = new ZipFile(zipfile);
意大利统一ZipOutputStream zos = new ZipOutputStream(out);
@SuppressWarnings("unchecked")
Enumeration<ZipEntry> en = (Enumeration<ZipEntry>) ies();
while (en.hasMoreElements()) {
ZipEntry ze = en.nextElement();
if(!ze.getName().equals(entry)){
zos.putNextEntry(new Name()));
InputStream is = InputStream(ze);
copyStream(is, zos);
is.clo();
}
}
zos.putNextEntry(new ZipEntry(entry));
InputStream is = new FileInputStream(tmpfile);
copyStream(is, zos);
is.clo();
zos.clo();
}
怎么算百公里油耗private static void copyStream(InputStream in, OutputStream out) throws IOException {
byte[] chunk = new byte[1024];
int count;
while ((count = in.read(chunk)) >=0 ) {
out.write(chunk,0,count);
}
}
/**
* Writes spreadsheet data in a Writer.
* (YK: in future it may evolve in a full-featured API for streaming data in Excel)
*/
public static class SpreadsheetWriter {
private final Writer _out;
private int _rownum;
public SpreadsheetWriter(Writer out){
_out = out;
}
public void beginSheet() throws IOException {
public void beginSheet() throws IOException {
_out.write("<?xml version=\"1.0\" encoding=\""+"UTF-8"+"\"?>" +
"<worksheet xmlns=\"schemas.openxmlformats/spreadsheetml/2006/main\">" );              _out.write("<sheetData>\n");
}
public void endSheet() throws IOException {
_out.write("</sheetData>");
_out.write("</worksheet>");
}
家庭电路图/**
* Inrt a new row
*
* @param rownum 0-bad row number
*/
public void inrtRow(int rownum) throws IOException {
_out.write("<row r=\""+(rownum+1)+"\">\n");
this._rownum = rownum;
}
/**
* Inrt row end marker
*/
public void endRow() throws IOException {
_out.write("</row>\n");
}
public void createCell(int columnIndex, String value, int styleIndex) throws IOException {
String ref = new CellReference(_rownum, columnIndex).formatAsString();
_out.write("<c r=\""+ref+"\" t=\"inlineStr\"");
if(styleIndex != -1) _out.write(" s=\""+styleIndex+"\"");
_out.write(">");
_out.write("<is><t>"+value+"</t></is>");
_out.write("</c>");
}
public void createCell(int columnIndex, String value) throws IOException {
createCell(columnIndex, value, -1);
}
public void createCell(int columnIndex, double value, int styleIndex) throws IOException {
String ref = new CellReference(_rownum, columnIndex).formatAsString();
_out.write("<c r=\""+ref+"\" t=\"n\"");
if(styleIndex != -1) _out.write(" s=\""+styleIndex+"\"");
_out.write(">");
_out.write("<v>"+value+"</v>");
_out.write("</c>");
}
public void createCell(int columnIndex, double value) throws IOException {
createCell(columnIndex, value, -1);
}
public void createCell(int columnIndex, Calendar value, int styleIndex) throws IOException {
createCell(columnIndex, ExcelDate(value, fal), styleIndex);
}
}
}

本文发布于:2023-07-20 20:35:06,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1089582.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:配置   创建   需要   数值   属性
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图