JAVA使用POI创建EXCEL文件

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;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

// 注:此方法创建的为excel 2007版本

private static void CreateExcel(List<EslExcel> eslExcel, String filePath) throws Exception
    {
        // 创建Excel文档
        HSSFWorkbook hwb = new HSSFWorkbook();

        //自己创建的excel数据列的实体类
        EslExcel xlsDto = null;
        // sheet 对应一个工作页
        HSSFSheet firstSheet = hwb.createSheet("firstSheet");

        HSSFRow firstrow = firstSheet.createRow(0); // 下标为0的行开始
        HSSFCell[] firstcell = new HSSFCell[10];

        String[] columns =  { "col1", "col2", "col3", "col4"};
        for (int i = 0; i < columns.length; i++)
        {
            firstcell[i] = firstrow.createCell(i);
            firstcell[i].setCellValue(columns[i]);
        }

        HSSFSheet secondSheet= hwb.createSheet("secondSheet");
        firstrow = secondSheet.createRow(0); // 下标为0的行开始
        firstcell = new HSSFCell[3];

        columns = new String[]{ "col1", "col2", "col3" };
        for (int i = 0; i < columns.length; i++)
        {
            firstcell[i] = firstrow.createCell(i);
            firstcell[i].setCellValue(columns[i]);
        }

        for (int i = 0; i < eslExcel.size(); i++)
        {
            // 创建一行
            // HSSFRow row_firstSheet = firstSheet.createRow(i + 1);
            HSSFRow row_firstSheet = firstSheet.createRow((short) (firstSheet.getLastRowNum() + 1));
            
            HSSFRow row_secondSheet = secondSheet.createRow((short) (secondSheet.getLastRowNum() + 1));
            // 得到要插入的每一条记录
            xlsDto = eslExcel.get(i);
            // for (int colu = 0; colu <= 3; colu++) {
            // 在一行内创建各列并赋值
            HSSFCell col1 = row_firstSheet.createCell(0);
            col1.setCellValue(xlsDto.getNo());

            col1 = row_secondSheet.createCell(0);
            col1.setCellValue(xlsDto.getCol1());

            HSSFCell col2 = row_firstSheet.createCell(1);
            col2.setCellValue(xlsDto.getCol2());

            col2 = row_secondSheet.createCell(1);
            col2.setCellValue(xlsDto.getCol2());

            HSSFCell col3= row_firstSheet.createCell(2);
            col3.setCellValue(xlsDto.getCol3());

            col3 = row_secondSheet.createCell(2);
            col3.setCellValue(xlsDto.getCol3());

            HSSFCell col4= row_firstSheet.createCell(3);
            col4.setCellValue(xlsDto.getCol4());            
        }

        // 创建文件输出流,准备输出电子表格
        OutputStream out = new FileOutputStream(filePath);
        hwb.write(out);
        out.close();
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值