poi 导出excel文件

poi导出excel表:

导出excel表在项目中经常会用到,导出的方式有许多种。这里采用poi来实现。首先需要下载好相应的jar文件,然后复制到lib目录下。然后开始导出工作:

1、准备好需要导出的对象数据list,这个一般是从数据库中提取过来的

2、new一个workbook对象,即一个即将被导出的excel文件,关键代码:HSSFWorkbook workbook = new HSSFWorkbook();这里不需要传入参数。

     对比同样操作的jxl的代码 为: WritableWorkbook book = Workbook.createWorkbook(new FileOutputStream("G:\\测试.xls"));需要传入一个文件路径作为参数。

3、得到一个workbook对象后,创建一个sheet即工作表,关键代码:HSSFSheet sheet = workbook.createSheet("学生信息表"); 

    jxl的写法为:WritableSheet sheet = book.createSheet("第一个sheet", 0);

4、创建单元格的样式:(可省)

     HSSFCellStyle style = wb.createCellStyle();  

    style.setAlignment(HSSFCellStyle.ALIGN_CENTER)// 创建一个居中格式

5、创建sheet第0行内容,即表格的标题内容,关键代码:HSSFRow row = sheet.createRow(0);

6、为各个单元格填充内容。关键代码:

     for(int i=0;i<list.size();i++){
            Student student = (Student)list.get(i);
             row = sheet.createRow(i+1);
             row.createCell(0).setCellValue((Long)student.getId());
             row.createCell(1).setCellValue(student.getName());
             row.createCell(2).setCellValue(student.getAge());
             row.createCell(3).setCellValue(student.getSex());
             row.createCell(4).setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(student.getBirthday()));
        }

完整代码如下:

package org.leno.export.util;

import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

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.ss.usermodel.Row;

public class ExportExcel2 {
    
    public static List getlists(){
        List list = null;
        try {
            
            SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
            list = new ArrayList();
            Student student = new Student(6, "wkl", 22, true, df.parse("1992-04-12"));
            Student student2 = new Student(7, "wky", 23, true, df.parse("1992-04-12"));
            Student student3 = new Student(8, "wkt", 24, true, df.parse("1992-04-12"));
            list.add(student);
            list.add(student2);
            list.add(student3);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    
    //poi导出excel方案二
    public static void main(String args[]){
        //1、第一步创建一个workbook,即一个excel文件
        HSSFWorkbook workbook = new HSSFWorkbook();
        //2、第二步创建一个sheet,相当于excel中的sheet
        HSSFSheet sheet = workbook.createSheet("学生信息表");
        //3、第三步创建一个sheet的标题第0行
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("id");
        row.createCell(1).setCellValue("名字");
        row.createCell(2).setCellValue("年龄");
        row.createCell(3).setCellValue("性别");
        row.createCell(4).setCellValue("生日");
        //4、第四步创建每个单元格的内容
        
        //获得数据
        List list = getlists();
        for(int i=0;i<list.size();i++){
            Student student = (Student)list.get(i);
             row = sheet.createRow(i+1);
             row.createCell(0).setCellValue((Long)student.getId());
             row.createCell(1).setCellValue(student.getName());
             row.createCell(2).setCellValue(student.getAge());
             row.createCell(3).setCellValue(student.getSex());
             row.createCell(4).setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(student.getBirthday()));
        }
        try  
        {  
            FileOutputStream fout = new FileOutputStream("E:/students.xls");  
            workbook.write(fout);  
            fout.close();  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
        
    }

}

poi和jxl导入的原理是一样的,只是在语法上稍有区别。

 

补充:

2.POI结构

HSSF - 提供读写Microsoft Excel XLS格式档案的功能。
XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。
HWPF - 提供读写Microsoft Word DOC格式档案的功能。
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
HDGF - 提供读Microsoft Visio格式档案的功能。
HPBF - 提供读Microsoft Publisher格式档案的功能。
HSMF - 提供读Microsoft Outlook格式档案的功能。

转载于:https://www.cnblogs.com/kailing-con/p/4208367.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值