- 论坛徽章:
- 0
|
下面一个简单的用POI一生成一个Excel文档的程式:
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelBaseReport{
private int curRow = 0;
private short curCol = 0;
private HSSFRow hssfRow;
private HSSFCell hssfCell;
private HSSFWorkbook workbook;
private HSSFSheet currentSheet;
private List columnList;
private String templatePath;
/**
* Creates a new instance of ExcelBaseReport
*/
public ExcelBaseReport() {
templatePath = this.getClass().getPackage().getName().replace('.', '/')
+ "/template.xls";
init();
}
private ExcelBaseReport(String path) {
templatePath = path + "/template.xls";
init();
}
public ExcelBaseReport(InputStream is) {
try {
workbook = new HSSFWorkbook(is);
} catch (IOException ex) {
ex.printStackTrace();
}
}
private void init() {
try {
InputStream is = this.getClass().getClassLoader()
.getResourceAsStream(templatePath);
workbook = new HSSFWorkbook(is);
} catch (IOException ex) {
ex.printStackTrace();
}
}
public int setReportData(Collection c) throws Exception {
if (c == null || c.size() 1)
return -1;
if (getColumnList() == null || getColumnList().size() 1)
return -1;
currentSheet = workbook.getSheetAt(0);
Iterator oit = c.iterator();
while (oit.hasNext()) {
Object bean = oit.next();
hssfRow = this.currentSheet.createRow(curRow);
for (int i = 0; i getColumnList().size(); i++) {
hssfCell = hssfRow.createCell(curCol);
curCol++;
String colName = getColumnList().get(i);
Object value = BeanUtils.getProperty(bean, colName);
if (value != null) {
this.setValue(value);
}
}
curCol = 0;
this.curRow++;
}
return 0;
}
private void setValue(Object value) {
if (value instanceof Number) {
if (value instanceof Integer) {
this.hssfCell.setCellValue(((Integer) value).intValue());
} else {
this.hssfCell.setCellValue(((Double) value).doubleValue());
}
} else {
this.hssfCell.setCellValue(value.toString());
}
}
public List getColumnList() {
return columnList;
}
public byte[] getReportAsByte() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
this.workbook.write(baos);
byte[] data = baos.toByteArray();
baos.close();
return data;
}
public void setColumnList(List columnList) {
this.columnList = columnList;
}
public void setTemplatePath(String templatePath) {
this.templatePath = templatePath;
}
public static void main(String[] args) throws Exception {
ExcelBaseReport report = new ExcelBaseReport();
List list = new ArrayList();
Entry entry = new Entry();
entry.setId("1");
entry.setName("2");
list.add(entry);
entry = new Entry();
entry.setId("2");
entry.setName("hello");
list.add(entry);
List clist = new ArrayList();
clist.add("id");
clist.add("name");
report.setColumnList(clist);
report.setReportData(list);
FileOutputStream os = new FileOutputStream("c:/2.xls");
System.out.println("size : " + report.getReportAsByte().length);
os.write(report.getReportAsByte());
os.flush();
os.close();
}
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/37089/showart_295949.html |
|