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;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章