POI Excel文件导出

 

工具类:


package com.university.bibased.modules.TestController;

import org.apache.log4j.Logger;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
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;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;


public class ExcelUtil {
    private static Logger logger = Logger.getLogger(ExcelUtil.class);
    private final static String xls = "xls";
    private final static String xlsx = "xlsx";

    public static List<String[]> readExcel(MultipartFile file) throws IOException {
        checkFile(file);
        Workbook workbook = getWorkBook(file);
        List<String[]> list = new ArrayList<>();
        if(list != null) {
            for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {
                Sheet sheet = workbook.getSheetAt(sheetNum);
                if (sheet == null) {
                    continue;
                }
                int firstRowNo = sheet.getFirstRowNum();
                int lastRowNo = sheet.getLastRowNum();
                int firstColumn = 0;
                for (int rowNum = firstRowNo + 1; rowNum <= lastRowNo; rowNum++) {
                    Row row = sheet.getRow(rowNum);
                    if (row == null) {
                        continue;
                    }
                    int firstCellNo = 0;
                    int lastCellNo = row.getPhysicalNumberOfCells();
                    if (rowNum == firstRowNo + 1) {
                        firstColumn = lastCellNo+1;
                    }
                    String[] cells = new String[firstColumn];
                    for (int cellNum = firstCellNo; cellNum < firstColumn; cellNum++) {
                        Cell cell = row.getCell(cellNum);
                        cells[cellNum] = getCellValue(cell);
                    }
                    list.add(cells);
                }
            }
        }
        return list;
    }

    public static void checkFile(MultipartFile file) throws IOException {
        if(null == file){
            logger.error("文件不存在");
            throw new FileNotFoundException("文件不存在!");
        }
        String fileName = file.getOriginalFilename();
        if(!fileName.endsWith(xls) && !fileName.endsWith(xlsx)){
            logger.error(fileName+"不是Excel文件!");
            throw new IOException(fileName+"不是Excel文件!");
        }
    }

    public static Workbook getWorkBook(MultipartFile file){
        Workbook workbook = null;
        try {
            InputStream in = file.getInputStream();
            if (!in.markSupported()) {
                in = new PushbackInputStream(in, 8);
            }
            if (POIFSFileSystem.hasPOIFSHeader(in)) {
                return new HSSFWorkbook(in);
            }
            if (POIXMLDocument.hasOOXMLHeader(in)) {
                return new XSSFWorkbook(OPCPackage.open(in));
            }
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return workbook;
    }

    public static String getCellValue(Cell cell){
        DecimalFormat df = new DecimalFormat("#");
        String cellValue = "";
        if(cell == null){ return ""; }
        //把数字当成String来读,避免出现1读成1.0的情况
        if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){ cell.setCellType(Cell.CELL_TYPE_STRING); }
        //判断数据的类型
        switch (cell.getCellType()){
            case Cell.CELL_TYPE_NUMERIC: //数字 //
                cellValue = String.valueOf(cell.getNumericCellValue());
                double c = cell.getNumericCellValue();
                cellValue = df.format(c);
                break;
            case Cell.CELL_TYPE_STRING: //字符串
                cellValue = cell.getStringCellValue().toString();
                break;
            case Cell.CELL_TYPE_BOOLEAN: //Boolean
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA: //公式
                cellValue = String.valueOf(cell.getCellFormula());
                break;
            case Cell.CELL_TYPE_BLANK: //空值
                cellValue = "";
                break;
            case Cell.CELL_TYPE_ERROR: //故障
                cellValue = "非法字符";
                break;
            default: cellValue = "未知类型";
                break;
        }
        return cellValue;
    }

}

使用这些方法就可以将Excel导出

猜你喜欢

转载自blog.csdn.net/qq_36826506/article/details/81916305