远程Excel相关操作

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.8</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;


public class RemoteFileUtils {
   
    public static String getTextByFileUrl(String fileUrl) throws IOException {
        URL url = new URL(fileUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(3000);
        conn.setReadTimeout(3 * 60 * 1000);
        InputStream inputStream = conn.getInputStream();
        byte[] getData = null ;
        try{
             getData = readInputStream(inputStream);
        }catch (Exception e){
             throw new IOException(e);
        }finally {
            inputStream.close();
        }
        return new String(getData, "utf-8");
    }

    
    public static List<Integer> getPoiIdsByFileUrl(String fileUrl) throws IOException {
        List<Integer> allRows = new ArrayList<Integer>();
        InputStream is = null;
        Workbook wb = null;
        try {
            URL url = new URL(fileUrl);
            URLConnection conn = url.openConnection();
            conn.setConnectTimeout(3000);
            conn.setReadTimeout(3 * 60 * 1000);
            is = conn.getInputStream();
            wb = WorkbookFactory.create(is);
            Sheet sheet = wb.getSheetAt(0);
            int maxRowNum = sheet.getLastRowNum();
            int minRowNum = sheet.getFirstRowNum();
            for (int i = minRowNum; i <= maxRowNum; i++) {
                Row row = sheet.getRow(i);
                if (row == null) {
                    continue;
                }
                Cell cell = row.getCell(0);
                if (cell == null) {
                    continue;
                }
                cell.setCellType(Cell.CELL_TYPE_STRING);
                String str = cell.getStringCellValue() ;
                if((str != null && StringUtils.isNotEmpty(str.trim()) )  && isNumber(str)){
                    allRows.add(Integer.parseInt(str)) ;
                }
            }

        } catch (Exception e) {
            throw new IOException(e);
        } finally {
            if (is != null) {
                is.close();
            }
            if (wb != null && wb instanceof SXSSFWorkbook) {
                SXSSFWorkbook xssfwb = (SXSSFWorkbook) wb;
                xssfwb.dispose();
            }
        }
        return allRows;
    }

    
    public static boolean checkShopIdFile(InputStream inputStream) throws IOException{
        Workbook workbook = null ;
        try {
            workbook = WorkbookFactory.create(inputStream) ;
            Sheet sheet = workbook.getSheetAt(0) ;
            int minRowNum = sheet.getFirstRowNum() ;
            int maxRowNum = sheet.getLastRowNum() ;
            for(int $i = minRowNum ; $i <= maxRowNum ; $i++){
                Row row = sheet.getRow($i) ;
                if(row == null){
                    continue ;
                }
                Cell cell = row.getCell(0);
                if (cell == null) {
                    continue;
                }
                cell.setCellType(Cell.CELL_TYPE_STRING);
                String str = cell.getStringCellValue() ;
                if((str != null && StringUtils.isNotEmpty(str.trim()) )  && !isNumber(str)){
                    return false ;
                }
            }
        } catch (Exception e) {
            throw new IOException(e);
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (workbook != null && workbook instanceof SXSSFWorkbook) {
                SXSSFWorkbook xssfwb = (SXSSFWorkbook) workbook;
                xssfwb.dispose();
            }
        }
        return true ;
    }

    private static boolean isNumber(String str){
        for(char c : str.toCharArray()){
            if(! Character.isDigit(c)){
                return false ;
            }
        }
        return true ;
    }

    private static byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }


    private static List<Integer> readLine(Row row) {
        short minColNum = row.getFirstCellNum();
        short maxColNum = row.getLastCellNum();
        List<Integer> dataList = new ArrayList<Integer>();
        for (short colIndex = minColNum; colIndex < maxColNum; colIndex++) {
            Cell cell = row.getCell(colIndex);
            if (cell == null) {
                continue;
            }
            int cellType = cell.getCellType();
            Integer value = null;
            if (Cell.CELL_TYPE_NUMERIC == cellType) {
                value = (int) cell.getNumericCellValue();
            } else if (Cell.CELL_TYPE_STRING == cellType && NumberUtils.isNumber(cell.getStringCellValue())) {
                value = Integer.valueOf(cell.getStringCellValue());
            }
            if(value != null){
                dataList.add(value);
            }
        }
        return dataList;
    }

}
MultipartFile file;
InputStream inputStream = file.getInputStream();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值