java 导出Excel文件

时间:2023-03-10 04:50:57
java 导出Excel文件
最近在做一个文件导出功能,发现大部分博客上通过引用各种的util工具包,其实说白了还是利用apache的poi,在项目中直接导入poi包就可以。直面其原理,随个人喜好封装。
 
1、首先准备一些poi的jar包
 java 导出Excel文件

2、准备好jar包之后就可以写代码了,接下来我就把代码写简单,让大家都能看懂。至于你后面想用多么牛逼的代码实现它,都可以,只要原理懂了,其它的都不是事,对吧。

首先我要建一个实体类:Student.class

package testExport;
/**
 *
 * @author lf
 *
 */
public class Student {
    private Integer id;
    private String name;
    private String sex;
     
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
}
还有一个导出类  ExportExcel.class
package testExport;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
//下面是和数据导出有关的包
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExportExcel {
    public void Export(){
        // 声明一个工作薄
        HSSFWorkbook wb = new HSSFWorkbook();
        //声明一个单子并命名
        HSSFSheet sheet = wb.createSheet(学生表);
        //给单子名称一个长度
        sheet.setDefaultColumnWidth((short)15);
        // 生成一个样式 
        HSSFCellStyle style = wb.createCellStyle();
        //创建第一行(也可以称为表头)
        HSSFRow row = sheet.createRow(0);
        //样式字体居中
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //给表头第一行一次创建单元格
        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue(学生编号);
        cell.setCellStyle(style);
        cell = row.createCell( (short) 1); 
                cell.setCellValue(学生姓名); 
                cell.setCellStyle(style); 
                cell = row.createCell((short) 2); 
                cell.setCellValue(学生性别); 
                cell.setCellStyle(style);
         
               //添加一些数据,这里先写死,大家可以换成自己的集合数据
               List<student> list = new ArrayList<student>();
               list.add(new Student(111,张三,男));
               list.add(new Student(111,李四,男));
               list.add(new Student(111,王五,女));
               //向单元格里填充数据
               for (short i = 0; i < list.size(); i++) {
                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(list.get(i).getId());
                row.createCell(1).setCellValue(list.get(i).getName());
                row.createCell(2).setCellValue(list.get(i).getSex());
            }
         
               try {
            //默认导出到E盘下
            FileOutputStream out = new FileOutputStream(E://学生表.xls);
            wb.write(out);
            out.close();
            JOptionPane.showMessageDialog(null, 导出成功!);
        } catch (FileNotFoundException e) {
            JOptionPane.showMessageDialog(null, 导出失败!);
            e.printStackTrace();
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, 导出失败!);
            e.printStackTrace();
        }
    }
}
 下面看一下效果图:
java 导出Excel文件