工具类---xlsx文件读写

1.pom.xml

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.11</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.11</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.11</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.11</version>
        </dependency>

2.读写

package com.dzpykj.files;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public final class ExcelUtil {

    private final String TAG = "ExcelUtils";

    public static Workbook getWorkbook(String filePath) {
        Workbook workbook = null;
        try {
            InputStream inputStream = new FileInputStream(filePath);
            if (filePath.endsWith(".xls")) {
                workbook = new HSSFWorkbook(inputStream);
            } else if (filePath.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(inputStream);
            }
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return workbook;
    }

    public static boolean saveWorkbook(String filePath, Workbook workbook) {
        try {
            File file = new File(filePath);
            FileOutputStream fileoutputStream = new FileOutputStream(file);
            workbook.write(fileoutputStream);
            fileoutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static List readStoreListFromExcel(String filePath) {
        List list = new LinkedList();
        Workbook workbook = getWorkbook(filePath);
        if (workbook == null) {
            return null;
        }
        Sheet sheet = workbook.getSheetAt(0);
        int rowLength = sheet.getLastRowNum()+1;
        Row row = sheet.getRow(0);
        int colLength = row.getLastCellNum();
        //Cell hssfCell = row.getCell(0);
        //CellStyle cellStyle = hssfCell.getCellStyle();
        boolean breakFlag = false;
        for (int i = 0; i < rowLength && !breakFlag; i++) {
            //获取Excel工作表的行
            Row rowi = sheet.getRow(i);
            for (int j = 0; j < colLength && !breakFlag; j++) {
                //获取指定单元格
                Cell cell = rowi.getCell(j);
                //获取每一列中的值
                if (cell != null);// System.out.print(cell.toString() + "\t");
                else continue;

                if (isStoreField(cell.toString())) {
                    Map<String, Integer> map = new HashMap<String, Integer>();
                    for (int index = j; index < colLength; index++) {
                        map.put(rowi.getCell(index).toString(), index);
                    }
                    for (int rowIndex = i + 1; rowIndex < rowLength; rowIndex++) {
                        Row row2 = sheet.getRow(rowIndex);
//                        Store store = new Store();
//                        store.setName(row2.getCell(map.get("店名")).toString());
//                        store.setStoreKeeper(row2.getCell(map.get("店主")).toString());
//                        store.setAddress(row2.getCell(map.get("地址")).toString());
//                        store.setTelephone(row2.getCell(map.get("电话")).toString());
//                        list.add(store);
                    }
                    breakFlag = true;
                }
            }
            System.out.println(list.size());
        }
        return list;
    }

    private static boolean isStoreField(String fieldName) {
        if (fieldName.equals("店名") || fieldName.equals("店主") || fieldName.equals("电话")
                || fieldName.equals("地址")) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean writeExcelFile(String filePath, List<User> list) {
        Workbook workbook = getWorkbook(filePath);
        Sheet sheet = null;
        boolean newFile = false;
        if (workbook == null) {
            workbook = new XSSFWorkbook();
            sheet = workbook.createSheet("0");
            newFile = true;
        }else{
            sheet = workbook.getSheetAt(0);
        }
        int begin = 0;
        if(!newFile){
            begin = sheet.getLastRowNum() + 1;
        }
        for (int i = 0; i < list.size(); i++) {
            Row row = sheet.createRow(i+begin);
            row.createCell(0).setCellValue(list.get(i).getUserTel());
            row.createCell(1).setCellValue(list.get(i).getUserWant());
        }
        return saveWorkbook(filePath, workbook);
    }
}

说明:工具类,做记录,方便日后操作文件

posted @ 2021-04-29 20:46  漂渡  阅读(125)  评论(0编辑  收藏  举报