POI读写海量Excel(详细解读)(4)

2019-04-22 19:41

/**

* 在写入器中写入电子表格 * */ public static class SpreadsheetWriter { private final Writer _out; private int _rownum; private static String LINE_SEPARATOR = System.getProperty(\ public SpreadsheetWriter(Writer out) { _out = out; } public void beginSheet() throws IOException { _out.write(\ + xmlns=\\\ _out.write(\ } public void endSheet() throws IOException { _out.write(\ _out.write(\ } /** * 插入新行 * * @param rownum 以0开始 */ public void insertRow(int rownum) throws IOException { _out.write(\ this._rownum = rownum; } /** * 插入行结束标志 */ public void endRow() throws IOException { _out.write(\ } /** * 插入新列

\

* @param columnIndex * @param value * @param styleIndex * @throws IOException */

public void createCell(int columnIndex, String value, int styleIndex) throws IOException { String ref = new CellReference(_rownum, columnIndex) .formatAsString(); _out.write(\ if (styleIndex != -1) _out.write(\ _out.write(\ _out.write(\ _out.write(\}

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(\ if (styleIndex != -1) _out.write(\ _out.write(\ _out.write(\ _out.write(\}

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, DateUtil.getExcelDate(value, false), styleIndex); }

} }

public class Excel2007WriterImpl extends AbstractExcel2007Writer{ /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub System.out.println(\ long start = System.currentTimeMillis();

} /*

//构建excel2007写入器

AbstractExcel2007Writer excel07Writer = new Excel2007WriterImpl(); //调用处理方法

excel07Writer.process(\long end = System.currentTimeMillis();

System.out.println(\

* 可根据需求重写此方法,对于单元格的小数或者日期格式,会出现精度问题或者日期格式转化问题,建议使用字符串插入方法 * @see com.excel.ver2.AbstractExcel2007Writer#generate() */ @Override public void generate()throws Exception { //电子表格开始 beginSheet();

for (int rownum = 0; rownum < 100; rownum++) { //插入新行

insertRow(rownum);

//建立新单元格,索引值从0开始,表示第一列 createCell(0, \中国<\ createCell(1, 34343.123456789); createCell(2, \ createCell(3, \

createCell(4, \ createCell(5, \ createCell(6, \

//结束行 endRow(); }

//电子表格结束 endSheet(); } }

public class XMLEncoder {

private static final String[] xmlCode = new String[256];

static {

// Special characters xmlCode['\\''] = \

xmlCode['\\\ xmlCode['&'] = \ xmlCode['<'] = \ xmlCode['>'] = \ }

/** *

* Encode the given text into xml. *

*

* @param string the text to encode * @return the encoded string */

public static String encode(String string) { if (string == null) return \ int n = string.length(); char character; String xmlchar;

StringBuffer buffer = new StringBuffer(); // loop over all the characters of the String. for (int i = 0; i < n; i++) {

character = string.charAt(i);

// the xmlcode of these characters are added to a StringBuffer one by one try {

xmlchar = xmlCode[character]; if (xmlchar == null) {

buffer.append(character);

} else {

buffer.append(xmlCode[character]); }

} catch (ArrayIndexOutOfBoundsException aioobe) { buffer.append(character); } }

return buffer.toString(); } }

可以参见转载文章:http://blog.csdn.net/goodkuang2012/article/details/7350985


POI读写海量Excel(详细解读)(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:自考《国际法》学习笔记

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: