excel操作

package com.sgm.carpur.common.utils;
import com.sgm.carpur.common.base.CommonConstants;
import org.apache.http.HttpResponse;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

/**
 * @description: 自定义文件处理工具类
 * @author: yxia
 * @create: 2021-05-11 20:19
 **/
public class FileUtils {
    private static final Logger log = LoggerFactory.getLogger(FileUtils.class);

    /**
     * 解析字符串类型文件
     * @param is
     * @param maxCol
     * @return
     * @throws IOException
     */
    public static String[][] parsingExcelStr(InputStream is,int maxCol) {
        Workbook wb = null;
        Sheet sheet = null;
        Row row = null;

        try {
            wb = new XSSFWorkbook(is);
            //获取第一个sheet
            sheet = wb.getSheetAt(0);
            //最大行标
            int rownum=sheet.getLastRowNum();
            String [][]datas=new String[rownum+1][maxCol];
            for(int i=0;i<=rownum;i++){
                String [] columns=new String[maxCol];
                datas[i]=columns;
                row = sheet.getRow(i);
                for(int j=0;j<maxCol;j++){
                    Cell cell=row.getCell(j);
                    if(cell==null){
                        columns[j]=null;
                    }else{
                        columns[j]=cell.getStringCellValue().trim();
                    }
                }
            }
            return datas;
        } catch (Exception e) {
            log.info("文件解析异常",e);
            return null;
        }
    }
    public static String fileUploadToSGMServe(byte[] bytes,String fileName,String appId,String uploadUrl)throws  IOException{
        //创建HTTPClient MultipartEntityBuilder对象
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        //将appId, folder, file添加到Entity中
        builder.addTextBody("appId", appId);
//        builder.addTextBody("folder", "test"); //optional parameter
        builder.addBinaryBody("file", bytes, ContentType.APPLICATION_OCTET_STREAM, URLEncoder.encode(fileName,"UTF-8"));
        //发起HTTP POST请求访问单文件上传服务
        return  Request.Post(uploadUrl).body(builder.build()).execute().returnContent().asString();
    }
    public static InputStream downloadFileFromSGMServe(String fileUri,String appId,String downloadUrl) throws IOException {
        HttpResponse res = Request.Get(downloadUrl + URLEncoder.encode(fileUri,"UTF-8") + "?appId="+appId).execute().returnResponse();
        //文件下载失败 抛出文件IO异常
        if(CommonConstants.ERROR_CODE==res.getStatusLine().getStatusCode()){
            throw new IOException("下载失败");
        }
        return  res.getEntity().getContent();
    }
}

 

posted @ 2021-06-10 10:58  根目录97  阅读(86)  评论(0编辑  收藏  举报