/**
* 在写入器中写入电子表格 * */ 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