最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java接收解析excel方法及注意事項說明

 更新時間:2026年05月26日 09:11:13   作者:ArrowL3Zz  
這篇文章主要介紹了Java接收解析excel方法及注意事項說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

前言

提示:本文著重講解java如何處理excel:

例如:本文從java接收excel,解析excel,以及業(yè)務(wù)場景中基于阻塞隊列處理大批量的Excel。

提示:以下是本篇文章正文內(nèi)容,下面案例可供參考

一、Java接收Excel

示例:pandas 是基于NumPy 的一種工具,該工具是為了解決數(shù)據(jù)分析任務(wù)而創(chuàng)建的。

1.引入庫

pom如下:

       <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.3</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.21</version>
        </dependency>

2.Controller層代碼

提示: 本文MultipartFile接收,請求方式為form表單提交

       /**
     * 傳入excel
     *
     * @return
     */
    @PostMapping(value = "/inputExcel", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation("傳入excel")
    public R inputExcel(@RequestPart("file") MultipartFile file) {
        InputExcelDTO inputExcelDTO = new InputExcelDTO();
        inputExcelDTO.setFile(file);
        if (Objects.isNull(inputExcelDTO.getExcelName()) || Objects.isNull(inputExcelDTO.getFile())) {
            return R.failed("必填參數(shù)不可為空");
        }
        return excelService.inputExcel(inputExcelDTO);
    }

二、解析excel

1.將MultipartFile 轉(zhuǎn) File作為臨時文件存儲在本地,解析

代碼如下:

工具類 :

package com.byt.form.convert.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: author
 * @date: 2021/8/27 8:46
 * @description: excel處理工具類
 */
public class ExcelDealUtils {
    /**
     * MultipartFile 轉(zhuǎn) File
     *
     * @param file
     * @throws Exception
     */
    public static File multipartFileToFile(MultipartFile file) throws Exception {

        File toFile = null;
        if (file.equals("") || file.getSize() <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            ins = file.getInputStream();
            toFile = new File(file.getOriginalFilename());
            inputStreamToFile(ins, toFile);
            ins.close();
        }
        return toFile;
    }


    //獲取流文件
    private static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream 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);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 刪除本地臨時文件
     *
     * @param file
     */
    public static void deleteTempFile(File file) {
        if (file != null) {
            File del = new File(file.toURI());
            del.delete();
        }
    }


    /**
     * 處理數(shù)據(jù)
     *
     * @param str
     * @return
     */
    public static String getDelDay(String str) {
        str = str.trim();
        String reg = "[\u4e00-\u9fa5]";
        Pattern pat = Pattern.compile(reg);
        Matcher mat = pat.matcher(str);
        String repickStr = mat.replaceAll("");
        return repickStr;
    }
}

ServiceImpl實現(xiàn)層

 File excel= ExcelDealUtils.multipartFileToFile(inputExcelDTO.getFile());
    try {
            //excel最后修改時間
            String excelTime = modTime;
            if (excel.isFile() && excel.exists()) {   //判斷文件是否存在
                String[] split = excel.getName().split("\\."); //特殊字符轉(zhuǎn)義
                Workbook wb;
                //根據(jù)文件后綴(xls/xlsx)進行判斷
                if ("xls".equals(split[split.length - 1])) {
                    FileInputStream fis = new FileInputStream(excel);   //文件流對象
                    wb = new HSSFWorkbook(fis);
                    fis.close();
                } else if (("xlsx".equals(split[split.length - 1]))) {
                    ZipSecureFile.setMinInflateRatio(-1.0d);
                    FileInputStream fis = new FileInputStream(excel);
                    wb = new XSSFWorkbook(fis);
                    fis.close();
                } else {
                    log.info("文件類型錯誤");
                    return;
                }

                JSONArray data = new JSONArray();
                if (sheet == null) {
                    return;
                }
                log.info("處理sheet為{}", sheet.getSheetName());
                int firstRowIndex = sheet.getFirstRowNum();   //第一行是列名ABC...字母,所以不讀
                int lastRowIndex = sheet.getLastRowNum();
                for (int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {   //遍歷行
                    JSONArray rowArray = new JSONArray();
                    Row row = sheet.getRow(rIndex);
                    if (row != null) { //這里只保存了不為空的行
                        int firstCellIndex = row.getFirstCellNum();
                        int lastCellIndex = row.getLastCellNum();
                        for (int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex++) {   //遍歷列
                            Cell cell = row.getCell(cIndex);
                            if (cell != null) {
                                rowArray.add(getCellFormatValue(cell));
                            } else {
                                rowArray.add("");
                            }
                        }
                    }
                    if (rowArray.size() > 5) {
                        data.add(rowArray);
                    }
                }

               //data為二維數(shù)組,順序與excel一致
                wb.close();
            } else {
                log.info("找不到指定文件");
            }
        } catch (Exception e) {
            log.info("解析錯誤 報表id為{},錯誤日志為{}", excelId, e);
        }

    /**
     * 處理公式數(shù)據(jù),如果是公式直接讀值,excel日期解析到的是數(shù)字,需要轉(zhuǎn)換成日期
     *根據(jù)項目情況處理
     * @param cell
     * @return
     */
    public static Object getCellFormatValue(Cell cell) {
        Object cellValue = null;
        if (cell != null) {
            //判斷cell類型
            switch (cell.getCellType()) {
                case Cell.CELL_TYPE_NUMERIC: {
                    cell.setCellType(Cell.CELL_TYPE_STRING); //數(shù)字型特殊處理,使獲取的整型不帶有小數(shù)點
                    cellValue = cell.getRichStringCellValue().getString();
                    // cellValue = String.valueOf(cell.getNumericCellValue());
                    break;
                }
                case Cell.CELL_TYPE_FORMULA: {
                    //非法字符處理
                    try {
                        cellValue = cell.getStringCellValue();
                    } catch (Exception e) {
                        //特殊處理
                    }
                    try {
                        cellValue = String.valueOf(cell.getNumericCellValue());
                    } catch (Exception e) {
                    }
                    break;
                }
                case Cell.CELL_TYPE_STRING: {
                    cellValue = cell.getRichStringCellValue().getString();
                    break;
                }
                default:
                    cellValue = "";
            }
        } else {
            cellValue = "";
        }
        return cellValue;
    }


2.大批量的excel同時丟上來容易內(nèi)存溢出,故用阻塞隊列處理

全局參數(shù):

  // 能容納100個文件
    final BlockingQueue<File> queue = new LinkedBlockingQueue<File>(100);
    // 讀個數(shù)
    final AtomicInteger rc = new AtomicInteger();

    //   final File exitFile = new File("");
    HashMap<String, DelFileBO> fileMap = new HashMap<>();

存入阻塞隊列:

 /**
  *存入阻塞隊列 
 */
 private void (File file){
 //存入Map,主要是為了避免線程同時處理多個,所以用map記錄
        fileMap.put(delFile.getName(), file);
        //放入阻塞隊列
        scanFile(delFile);
 } 

 public void scanFile(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles(new FileFilter() {
                public boolean accept(File pathname) {
                    return pathname.isDirectory()
                            || pathname.getPath().endsWith(".java");
                }
            });
            for (File one : files)
                scanFile(one);
        } else {
            try {
                int index = rc.incrementAndGet();
                queue.put(file);
            } catch (InterruptedException e) {
            }
        }
    }

起一個線程從隊列拿數(shù)據(jù)處理

 @PostConstruct
    public void delExcelFile() {

        taskExecutor.execute(() -> {
            while (true) {
                this.delFile();
            }
        });
    }


    public void delFile() {
        try {
            //從隊列拿文件處理
            File file = queue.take();
            //業(yè)務(wù)邏輯
        } catch (InterruptedException e) {
            
        }
    }

總結(jié)

以上就是今天要講的內(nèi)容,本文僅僅簡單介紹了java如何解析excel,針對自身業(yè)務(wù)也可以維護一些其余配置,如報表id以及處理方式等。

這些僅為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot日志信息以及Lombok的常用注解詳析

    SpringBoot日志信息以及Lombok的常用注解詳析

    日志在我們的日常開發(fā)當(dāng)中是必定會用到的,這篇文章主要給大家介紹了關(guān)于SpringBoot日志信息以及Lombok的常用注解的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2023-12-12
  • 實戰(zhàn)分布式醫(yī)療掛號系統(tǒng)之整合Swagger2到通用模塊

    實戰(zhàn)分布式醫(yī)療掛號系統(tǒng)之整合Swagger2到通用模塊

    這篇文章主要為大家介紹了實戰(zhàn)分布式醫(yī)療掛號系統(tǒng)之整合Swagger2到通用模塊,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-04-04
  • Java多線程學(xué)習(xí)筆記

    Java多線程學(xué)習(xí)筆記

    常用的實現(xiàn)多線程的兩種方式:Thread和Runnable。之所以說是“常用”,是因為在Java 5后可以通過java.util.concurrent包中的線程池來實現(xiàn)多線程
    2021-09-09
  • springboot與vue實現(xiàn)簡單的CURD過程詳析

    springboot與vue實現(xiàn)簡單的CURD過程詳析

    這篇文章主要介紹了springboot與vue實現(xiàn)簡單的CURD過程詳析,圍繞springboot與vue的相關(guān)資料展開實現(xiàn)CURD過程的過程介紹,需要的小伙伴可以參考一下
    2022-01-01
  • Mybatis-plus查詢語句加括號(.or(),.and())問題

    Mybatis-plus查詢語句加括號(.or(),.and())問題

    這篇文章主要介紹了Mybatis-plus查詢語句加括號(.or(),.and())問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • 解決springboot configuration processor對maven子模塊不起作用的問題

    解決springboot configuration processor對maven子模塊不起作用的問題

    這篇文章主要介紹了解決springboot configuration processor對maven子模塊不起作用的問題,本文通過圖文實例代碼給大家講解的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • Integer IntegerCache源碼閱讀

    Integer IntegerCache源碼閱讀

    這篇文章主要介紹了Integer IntegerCache源碼閱讀,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • Spring集成Web環(huán)境的實例詳解

    Spring集成Web環(huán)境的實例詳解

    這篇文章主要介紹了Spring集成Web環(huán)境,本文通過實例圖文相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-02-02
  • Java Mybatis一級緩存和二級緩存

    Java Mybatis一級緩存和二級緩存

    緩存是內(nèi)存當(dāng)中一塊存儲數(shù)據(jù)的區(qū)域,目的是提高查詢效率,降低服務(wù)器和數(shù)據(jù)庫的壓力,這篇文章主要介紹了Mybatis一級緩存和二級緩存,感興趣的同學(xué)可以參考閱讀本文
    2023-04-04
  • Java多線程通信問題深入了解

    Java多線程通信問題深入了解

    下面小編就為大家?guī)硪黄钊肜斫釰AVA多線程之線程間的通信方式。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-07-07

最新評論

新疆| 陆川县| 景宁| 天峻县| 永登县| 棋牌| 苗栗县| 辽中县| 裕民县| 修水县| 苏尼特左旗| 镇安县| 汝州市| 靖远县| 郧西县| 四会市| 卓尼县| 乌鲁木齐市| 湖北省| 博客| 南京市| 神池县| 衢州市| 济阳县| 双峰县| 汉寿县| 全椒县| 湖北省| 白玉县| 监利县| 德化县| 祁门县| 双江| 浦县| 柘荣县| 准格尔旗| 珲春市| 龙胜| 玉林市| 昌图县| 米泉市|