免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 961 | 回复: 0
打印 上一主题 下一主题

POI生成MS Excel 文件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-05-09 16:48 |只看该作者 |倒序浏览
下面一个简单的用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
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP