SSM+springboot+jxl实现Excel的导入导出功能

本案例是写在单元测试中的,修改后可直接用到框架中,话不多说,直接上代码

package com.demo.springbootdemo;

import com.demo.mapper.TestMapper;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Test01 {
    @Autowired
    private TestMapper mapper;

    @Test
    public void downLoadFile() throws Exception {
        //导入
        List<HashMap<String, Object>> importMaps = read();
        for(HashMap<String, Object> map:importMaps){
            mapper.isertTestByHashMap(map);
        }

        //导出
        List<HashMap<String, Object>> exportMaps = mapper.selectAll();
        write(exportMaps);
    }

    public void write(List<HashMap<String, Object>> maps) throws Exception {
        File file = new File("F:/test.xls");
        if(!file.exists()){
            file.createNewFile();
        }
        //使用jxl写入数据需要createWorkbook
        WritableWorkbook workbook = Workbook.createWorkbook(file);
        //添加工作表
        WritableSheet sheet = workbook.createSheet("测试表", 0);
        //为第一行添加表头,我直接添加字段名,具体使用根据实际情况
        if(maps.size()>0){
            int i = 0;
            for(String key:maps.get(0).keySet()){
                sheet.addCell(new Label(i, 0, key));
                i++;
            }
        }
        String key = null;
        String value = null;
        for(int i=0;i<maps.size();i++){
            for(int j=0;j<sheet.getColumns();j++){
                key = sheet.getCell(j, 0).getContents();
                value = maps.get(i).get(key).toString();
                sheet.addCell(new Label(j, i+1, value));
            }
        }
        workbook.write();
        workbook.close();
    }

    public List<HashMap<String, Object>> read() throws IOException, BiffException {
        File file = new File("F:/test.xls");
        if(!file.exists()){
            file.createNewFile();
        }
        //读取文件要getWorkbook
        Workbook workbook = Workbook.getWorkbook(file);
        Sheet sheet = workbook.getSheet(0);
        List<HashMap<String,Object>> maps = new ArrayList();
        HashMap<String,Object> map = null;
        //第一行是表头,没有数据,所有默认从第二行开始读
        for(int i=1;i<sheet.getRows();i++){
            map = new HashMap<>();
            for(int j=0;j<sheet.getColumns();j++){
                //jxl中的cell操作第一个参数是列,第二个参数是行,由于我测试数据表头用的是字段名。所以直接封装,具体使用根据自己的表格放入参数即可
                map.put(sheet.getCell(j, 0).getContents(), sheet.getCell(j, i).getContents());
            }
            maps.add(map);
        }
        workbook.close();
        return maps;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值