栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

获取导入excel的某一列的所有数据并查询图片批量的进行导出到压缩包

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

获取导入excel的某一列的所有数据并查询图片批量的进行导出到压缩包

首先列出 工具类解析excel,及MultipartFile转换为File

package com.ott.server.btt.webapi.utils;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
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.ss.usermodel.WorkbookFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class ReadExcelUtil {
    
    public static List getColumn(InputStream iStream, int columnIndex) throws IOException, InvalidFormatException {
        List columnList=new ArrayList<>();
        Workbook book = WorkbookFactory.create(iStream);
        Sheet sheet = book.getSheetAt(0);

        for (int runNum =1; runNum <=sheet.getLastRowNum();runNum++) {
            Row row = sheet.getRow(runNum);
            if (row != null) {
                int minColIx = row.getFirstCellNum();
                int maxColIx = row.getLastCellNum();
                //遍历该行,获取每个cell元素
                for (int colIx = minColIx; colIx < maxColIx; colIx++) {
                    Cell cell = row.getCell(colIx);
                    //获取指定的一列
                    if (cell.getColumnIndex() == columnIndex) {
                        columnList.add(cell.getStringCellValue());
                    } else {
                        continue;
                    }
                }
            }
        }
        return columnList;
    }
    
    public static File MultipartFileToFile(MultipartFile multipartFile) {

        File file = null;
        //判断是否为null
        if (multipartFile.equals("") || multipartFile.getSize() <= 0) {
            return file;
        }
        //MultipartFile转换为File
        InputStream ins = null;
        OutputStream os = null;
        try {
            ins = multipartFile.getInputStream();
            file = new File(multipartFile.getOriginalFilename());
            os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(ins != null){
                try {
                    ins.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return file;
    }
}

业务处理

  public void downInvoiceImageZip(MultipartFile file, HttpServletResponse response) throws IOException, InvalidFormatException {
        File fileNew = ReadExcelUtil.MultipartFileToFile(file);
        List invoiceCode = ReadExcelUtil.getColumn(new FileInputStream(fileNew),3);
        List invoiceNo = ReadExcelUtil.getColumn(new FileInputStream(fileNew),2);
        List> stringList = new ArrayList<>();
        byte[] imageByteArray;
        for(int i=0;i queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(PurchaseInvoiceMain::getInvoiceNo,invoiceNo.get(i));
            queryWrapper.eq(PurchaseInvoiceMain::getInvoiceCode,invoiceCode.get(i));
            //查询出匹配发票代码及发票号码的集合
            List list =this.list(queryWrapper);
            for(int t=0;t map = new HashMap<>();
                map.put(name,imageByteArray);
                stringList.add(map);
            }
        }
        //获取到所有发票代码的url地址,通过地址将图片批量下载并转换为zip
        try {
            String downloadFilename = "发票图片";//文件的名称
            downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");//转换中文否则可能会产生乱码
            response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
            response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename +".zip");// 设置在下载框默认显示的文件名
            ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
            for (int i = 0; i < stringList.size(); i++) {
                Set keySet = stringList.get(i).keySet();
                String keySting=null;
                for (String key:keySet){
                    keySting=key;
                    zos.putNextEntry(new ZipEntry(key+new Date().getTime()+".jpg"));
                }
                InputStream fis = new ByteArrayInputStream(stringList.get(i).get(keySting));
                byte[] buffer = new byte[1024];
                int r = 0;
                while ((r = fis.read(buffer)) != -1) {
                    zos.write(buffer, 0, r);
                }
                fis.close();
            }
            zos.flush();
            zos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/955985.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号