java操作excel

poi和easyexcel

		 <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.1.1</version>
        </dependency>
         <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>2.1.1</version>
            </dependency>
            <!--xlsx-->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>2.1.1</version>
            </dependency>
poi
 // 03版excel写操作
    @Test
    public void excel03Test() throws IOException {
        // 创建工作簿
        Workbook workbook = new HSSFWorkbook();
        // 创建一个工作表
        Sheet  sheet = workbook.createSheet("excel学习");
        // 创建行
        Row row1 = sheet.createRow(0);
        // 创建单元格
        Cell cell1 = row1.createCell(0);
        cell1.setCellValue("第一天");

        Cell cell11 = row1.createCell(1);
        cell11.setCellValue(666);

        // 第二行
        Row row2 = sheet.createRow(1);
        Cell cell2 = row2.createCell(0);
        cell2.setCellValue("时间");

        Cell cell22 = row2.createCell(1);
        cell22.setCellValue(new Date());

        FileOutputStream fileOutputStream = new FileOutputStream(PATH+"03.xls");

        workbook.write(fileOutputStream);
        fileOutputStream.close();
    }
 // 07版excel写操作
    @Test
    public void excel07Test() throws IOException {
        // 创建工作簿
        Workbook workbook = new XSSFWorkbook();
        // 创建一个工作表
        Sheet  sheet = workbook.createSheet("excel学习");
        // 创建行
        Row row1 = sheet.createRow(0);
        // 创建单元格
        Cell cell1 = row1.createCell(0);
        cell1.setCellValue("第一天");

        Cell cell11 = row1.createCell(1);
        cell11.setCellValue(666);

        // 第二行
        Row row2 = sheet.createRow(1);
        Cell cell2 = row2.createCell(0);
        cell2.setCellValue("时间");

        Cell cell22 = row2.createCell(1);
        cell22.setCellValue(new Date());

        FileOutputStream fileOutputStream = new FileOutputStream(PATH+"04.xlsx");

        workbook.write(fileOutputStream);
        fileOutputStream.close();
    }
easyexcel
public class DemoData {

    /*用阿里巴巴提供的框架读取本地excel表格数据到数据库*/

    @ExcelProperty(value = "学生编号",index = 0) // 设置excel表头名称
    private Integer sno;

    @ExcelProperty(value = "学生姓名",index = 1)
    private String sname;

    public Integer getSno() {
        return sno;
    }

    public void setSno(Integer sno) {
        this.sno = sno;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    @Override
    public String toString() {
        return "DemoData{" +
                "sno=" + sno +
                ", sname='" + sname + '\'' +
                '}';
    }
}

public class ExcelListener extends AnalysisEventListener<DemoData> {
		
	//监听
    // 一行一行读取excel的内容
    @Override
    public void invoke(DemoData demoData, AnalysisContext analysisContext) {
        System.out.println("****"+demoData);
    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        System.out.println("表头"+headMap);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
public class TestEasyExcel {

    public static void main(String[] args) {

//        getWrite();// 读
        getRead();// 写

    }


    public static void getWrite(){
        // 实现excel写的操作
        String filename = "E:\\weite.xlsx";
        List<DemoData> date = getDate();
        // 调用easyexcel中的方法实现写操作
        EasyExcel.write(filename,DemoData.class).sheet("学生列表").doWrite(date);

    }

    public static void getRead(){
        String filename = "E:\\weite.xlsx";
        EasyExcel.read(filename,DemoData.class,new ExcelListener()).sheet().doRead();
    }

    private static List<DemoData> getDate(){

        List<DemoData> list = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            DemoData demoData = new DemoData();
            demoData.setSno(i);
            demoData.setSname("lury"+i);
            list.add(demoData);
        }

        return  list;
    }


}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值