国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

excel文件導(dǎo)入或?qū)С鯦ava代碼示例

這篇具有很好參考價(jià)值的文章主要介紹了excel文件導(dǎo)入或?qū)С鯦ava代碼示例。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

1、excel文件導(dǎo)入

controller層接口內(nèi)容

excel文件導(dǎo)入或?qū)С鯦ava代碼示例,excel,java,運(yùn)維開發(fā)

?service層代碼

?excel文件導(dǎo)入或?qū)С鯦ava代碼示例,excel,java,運(yùn)維開發(fā)

?serviceImpl內(nèi)代碼內(nèi)容

 @Override
    @Transactional(rollbackFor = Exception.class)
    public void importCheckItemExcel(MultipartFile file, Long checkPkgId) throws Exception {
        if (file.isEmpty()){
            throw new IOException("請(qǐng)選擇上傳文件");
        }
        Workbook work = this.getWorkbook(file);
        if (null == work) {
            throw new HolliException(DeviceExceptionEnum.EXCEL_IS_NULL_EXCEPTION);
        }
        Iterator<Sheet> sheetIterator = work.sheetIterator();
        while (sheetIterator.hasNext()) {
            Sheet sheet = sheetIterator.next();
            if (sheet == null) {
                return;
            }
            //先獲取首列標(biāo)題
            Map<String, Integer> validColumns = new HashMap<>();
            List<String> headerCells = getFirstRow(sheet, 0);
            if (headerCells == null) {
                return;
            }
            for (int i = 0; i < headerCells.size(); i++) {
                Object header = headerCells.get(i);
                if (header == null) {
                    continue;
                }
                if (ObjectUtil.contains(header, "修程")) {
//                    validColumns.put("checkLevel", i);//數(shù)字
                    validColumns.put("checkLevelName", i);//含義
                }else if (ObjectUtil.contains(header, "工作項(xiàng)目")) {
                    validColumns.put("checkName", i);
                }else if (ObjectUtil.contains(header, "單位")) {
//                    validColumns.put("checkUnit", i);//數(shù)字
                    validColumns.put("checkUnitName", i);//含義
                }else if (ObjectUtil.contains(header, "周期")) {
                    validColumns.put("checkCycle", i);
                }else if (ObjectUtil.contains(header, "工作內(nèi)容及標(biāo)準(zhǔn)")) {
                    validColumns.put("checkContent", i);
                }else if (ObjectUtil.contains(header, "ATP型號(hào)")) {
                    validColumns.put("atpModel", i);
                }
            }
//標(biāo)題集合
            if (validColumns.size() == 0) {
                return;
            }
            Map<String, Object> map = new HashMap<String, Object>();
            //遍歷當(dāng)前sheet中的所有行
            //包涵頭部,所以要小于等于最后一列數(shù),這里也可以在初始值加上頭部行數(shù),以便跳過頭部
            for (int j = 1; j <= sheet.getLastRowNum(); j++) {
                //讀取一行
                List<Object> rows = getRow(sheet, j);
                if (rows == null) {
                    continue;
                }
                Set<String> keySet = validColumns.keySet();
                for (String key : keySet) {
                    int col_index = validColumns.get(key);
                    if (col_index >= rows.size()) {
                        continue;
                    }
                    Object cell = rows.get(col_index);
                    map.put(key, cell);
                }
//取出對(duì)應(yīng)屬性及值內(nèi)容 以便存儲(chǔ)數(shù)據(jù)庫
 //業(yè)務(wù)邏輯,數(shù)據(jù)存儲(chǔ)
                DevCheckItemVo devCheckItemVo = new DevCheckItemVo();
                BeanUtil.copyProperties(map, devCheckItemVo);
                this.insertDevCheckItem(checkPkgId, devCheckItemVo, j + 1);
            }
        }
    }


?判斷excel的格式,同時(shí)兼容2003和2007


    protected final static String excel2003L = ".xls";    //2003- 版本的excel
    protected final static String excel2007U = ".xlsx";   //2007+ 版本的excel
 

/**
     * 描述:根據(jù)文件后綴,自適應(yīng)上傳文件的版本
     */
    public static Workbook getWorkbook(MultipartFile file) throws Exception {
        Workbook wb = null;
        InputStream is = file.getInputStream();
        String fileType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        if (excel2003L.equals(fileType)) {
            wb = new XSSFWorkbook(is);  //2007+
//            因HSSFWorkbook報(bào)(You need to call a different part of POI to process this data (eg XSSF instead of HSSF)),故直接使用XSSFWorkbook處理
//            wb = new HSSFWorkbook(is);  //2003-
        } else if (excel2007U.equals(fileType)) {
            wb = new XSSFWorkbook(is);  //2007+
        } else {
            throw new IOException("解析的文件格式有誤!");
        }
        return wb;
    }

?獲取行數(shù)據(jù)

/**
     * 獲取行數(shù)據(jù)
     * @param sheet
     * @param rowIndex
     * @return
     */
    public List<Object> getRow(Sheet sheet, int rowIndex) {
        List<Object> li = new ArrayList<Object>();
        if (sheet == null) {
            return null;
        }
        Row row = null;
        Cell cell = null;
        //讀取一行
        row = sheet.getRow(rowIndex);
        //去掉空行和表頭
        if (row == null) {
            return null;
        }
        /**為去掉空行,若第一列為空,則直接跳過該行*/
        if (ObjectUtil.isEmpty(row.getCell(0))) {
            return null;
        }
        //遍歷所有的列
        for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
            cell = row.getCell(y);
            li.add(this.getCellFormatValue(cell));

        }
        return li;
    }

?//獲取excel列表內(nèi)的對(duì)應(yīng)數(shù)據(jù)格式


//獲取excel列表內(nèi)的對(duì)應(yīng)數(shù)據(jù)格式
    protected Object getCellFormatValue(Cell cell) {
        Object val = "";
        if (null != cell) {
            if (cell.getCellType() == CellType.NUMERIC || cell.getCellType() == CellType.FORMULA)
            {
                val = cell.getNumericCellValue();
                if (DateUtil.isCellDateFormatted(cell))
                {
                    val = DateUtil.getJavaDate((Double) val); // POI Excel 日期格式轉(zhuǎn)換
                }
                else
                {
                    if ((Double) val % 1 != 0)
                    {
                        val = new BigDecimal(val.toString());
                    }
                    else
                    {
                        val = new DecimalFormat("0").format(val);
                    }
                }
            }
            else if (cell.getCellType() == CellType.STRING)
            {
                val = cell.getStringCellValue();
            }
            else if (cell.getCellType() == CellType.BOOLEAN)
            {
                val = cell.getBooleanCellValue();
            }
            else if (cell.getCellType() == CellType.ERROR)
            {
                val = cell.getErrorCellValue();
            }
        } else {
            val = "";
        }
        return val;
    }

?2、excel導(dǎo)出

controller層接口內(nèi)容

excel文件導(dǎo)入或?qū)С鯦ava代碼示例,excel,java,運(yùn)維開發(fā)

??service層代碼

excel文件導(dǎo)入或?qū)С鯦ava代碼示例,excel,java,運(yùn)維開發(fā)

??serviceImpl內(nèi)代碼內(nèi)容

 @Override
    public void exportCheckItemExcel(String strIds, HttpServletResponse response) throws Exception {
        List<DevCheckItem> devCheckItemList = new ArrayList<>();
        long[] itemIdArray = StrUtil.splitToLong(strIds,",");
        //查詢檢修項(xiàng)id
        for (int i = 0; i< itemIdArray.length; i++){
            DevCheckItem devCheckItem = this.getById(itemIdArray[i]);
            if (ObjectUtil.isNotEmpty(devCheckItem)){
                devCheckItemList.add(devCheckItem);
            }
        }
        if (ObjectUtil.isNotEmpty(devCheckItemList) && devCheckItemList.size() > 0){
            //導(dǎo)出excel
            XSSFWorkbook workbook=new XSSFWorkbook();
            //獲得名字為sheet的工作本對(duì)象,這個(gè)是看你的模板工作表的名字
            XSSFSheet sheet = workbook.createSheet("sheet1");
            // 序號(hào)
            int serialNo = 1;
            //行號(hào)
            int rowIndex = 1;

            //創(chuàng)建表頭
            XSSFRow row_excel = sheet.createRow(0);

//行業(yè)務(wù)數(shù)據(jù)填充
            XSSFCell cell = row_excel.createCell(0);
            cell.setCellValue("序號(hào)");

            cell = row_excel.createCell(1);
            cell.setCellValue("ATP型號(hào)");

            cell = row_excel.createCell(2);
            cell.setCellValue("修程");

            cell = row_excel.createCell(3);
            cell.setCellValue("工作項(xiàng)目");

            cell = row_excel.createCell(4);
            cell.setCellValue("單位");

            cell = row_excel.createCell(5);
            cell.setCellValue("周期(小時(shí))");

            cell = row_excel.createCell(6);
            cell.setCellValue("工作內(nèi)容及標(biāo)準(zhǔn)");

            //遍歷填充數(shù)據(jù)
            for (DevCheckItem devCheckItem : devCheckItemList){
                //獲取當(dāng)前行
                row_excel = sheet.getRow(rowIndex);
                if (row_excel == null){
                    //創(chuàng)建行
                    row_excel = sheet.createRow(rowIndex);
                }
                //內(nèi)容填充
                if (ObjectUtil.isNotEmpty(devCheckItem)){
                    XSSFCell cellNew = row_excel.createCell(0);
                    cellNew.setCellValue(serialNo);

                    cell = row_excel.createCell(1);
                    if (ObjectUtil.isNotEmpty(devCheckItem.getAtpModel())){
                        cell.setCellValue(devCheckItem.getAtpModel());
                    }

                    cell = row_excel.createCell(2);
                    if (ObjectUtil.isNotEmpty(devCheckItem.getCheckLevel())){
                        String levelName = DevCheckLevelEnum.getLevelNameByType(devCheckItem.getCheckLevel());
                        cell.setCellValue(levelName);
                    }

                    cell = row_excel.createCell(3);
                    if (ObjectUtil.isNotEmpty(devCheckItem.getCheckName())){
                        cell.setCellValue(devCheckItem.getCheckName());
                    }

                    cell = row_excel.createCell(4);
                    if (ObjectUtil.isNotEmpty(devCheckItem.getCheckUnit())){
                        String checkUnit = this.getCheckUnitNameByNum(devCheckItem.getCheckUnit());
                        cell.setCellValue(checkUnit);
                    }

                    cell = row_excel.createCell(5);
                    if (ObjectUtil.isNotEmpty(devCheckItem.getCheckCycle())){
                        cell.setCellValue(devCheckItem.getCheckCycle());
                    }

                    cell = row_excel.createCell(6);
                    if (ObjectUtil.isNotEmpty(devCheckItem.getCheckContent())){
                        cell.setCellValue(devCheckItem.getCheckContent());
                    }
                }
                serialNo++;
                rowIndex++;
            }
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Type", "application/vnd.ms-excel");
            ServletOutputStream out = response.getOutputStream();
            workbook.write(out);

        }
    }

以上已導(dǎo)出完成。

3、以下為可能會(huì)用到的導(dǎo)出實(shí)例文件,上傳文件服務(wù)器的過程?

File格式轉(zhuǎn)換MultipartFile格式的例子

//上傳minio服務(wù)器
String fileName = "檢修工作項(xiàng)目.xlsx";
File cacheFile = new File(fileName);
if (cacheFile.exists()) {
    cacheFile.delete();
}
  
cacheFile.createNewFile();//生成文件
OutputStream out = new FileOutputStream(cacheFile);
workbook.write(out);
---------------------------------------------------------------------------------------(上面為導(dǎo)出實(shí)例文件)
//下面為上傳文件流格式轉(zhuǎn)換的格式。
FileInputStream fileInputStream = new FileInputStream(cacheFile);
MultipartFile multipartFile = new MockMultipartFile("cacheFile", cacheFile.getName(), "text/plain", IOUtils.toByteArray(fileInputStream));

?

-------------------------------------以下無正文-----------------------------------------------------------

注:僅供學(xué)習(xí),記錄問題和參考,共勉!文章來源地址http://www.zghlxwxcb.cn/news/detail-589319.html

到了這里,關(guān)于excel文件導(dǎo)入或?qū)С鯦ava代碼示例的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Java導(dǎo)出Excel模板,導(dǎo)出數(shù)據(jù)到指定模板,通過模板導(dǎo)入數(shù)據(jù)(一)

    Java導(dǎo)出Excel模板,導(dǎo)出數(shù)據(jù)到指定模板,通過模板導(dǎo)入數(shù)據(jù)(一)

    本文章主要是介紹阿里巴巴的easyexcel的使用 1. 首先需要我們導(dǎo)入easyexcel的依賴包 2. 前期工作準(zhǔn)備 編寫相關(guān)導(dǎo)出模板和導(dǎo)入模板。在項(xiàng)目的resources下創(chuàng)建文件夾,命名為excel 導(dǎo)出模板(此處僅做示例,字段根據(jù)自己項(xiàng)目來): ?導(dǎo)入模板(導(dǎo)入時(shí)需要哪些字段根據(jù)自己項(xiàng)目業(yè)

    2024年02月03日
    瀏覽(30)
  • Java原生POI實(shí)現(xiàn)的Excel導(dǎo)入導(dǎo)出(簡(jiǎn)單易懂)

    Java原生POI實(shí)現(xiàn)的Excel導(dǎo)入導(dǎo)出(簡(jiǎn)單易懂)

    首先是Controller入口方法 這個(gè)接口在postman上傳參是下面這樣的: 注意里面的參數(shù)名稱要和接口上的一致,不然會(huì)拿不到值 還有file那里key的類型要選file類型的,這樣就可以在后面value里面選擇文件 然后是Service方法 首先是Controller入口 strJson是用來接受其它參數(shù)的,一般導(dǎo)出的

    2024年02月11日
    瀏覽(27)
  • 使用Java導(dǎo)入、導(dǎo)出excel詳解(附有封裝好的工具類)

    使用Java導(dǎo)入、導(dǎo)出excel詳解(附有封裝好的工具類)

    ?? 作 ? ??????? 者 :是江迪呀 ?? 本文 : Java 、 Excel 、 導(dǎo)出 、 工具類 、 后端 ?? 每日?? 一言 :有些事情不是對(duì)的才去堅(jiān)持,而是堅(jiān)持了它才是對(duì)的! 我們?cè)谌粘i_發(fā)中,一定遇到過要將數(shù)據(jù)導(dǎo)出為 Excel 的需求,那么怎么做呢?在做之前,我們需要思考

    2024年02月06日
    瀏覽(22)
  • Java,excel大量百萬數(shù)據(jù)導(dǎo)出優(yōu)化措施,SXSSFWorkbook流式、分批次導(dǎo)出示例

    在導(dǎo)出百萬級(jí)的數(shù)據(jù)時(shí),如果不采用適當(dāng)?shù)膬?yōu)化措施,確實(shí)可能會(huì)造成死機(jī)和內(nèi)存崩潰等問題。 為避免這些問題,可以采用以下優(yōu)化措施: 分批次讀取數(shù)據(jù):將需要導(dǎo)出的數(shù)據(jù)分成多個(gè)批次進(jìn)行讀取和寫入,每次讀取部分?jǐn)?shù)據(jù),寫入 Excel 后即時(shí)清除內(nèi)存。這樣可以避免一次

    2024年02月16日
    瀏覽(35)
  • java實(shí)現(xiàn)excel的導(dǎo)入導(dǎo)出(帶參數(shù)校驗(yàn):非空校驗(yàn)、數(shù)據(jù)格式校驗(yàn))

    java實(shí)現(xiàn)excel的導(dǎo)入導(dǎo)出(帶參數(shù)校驗(yàn):非空校驗(yàn)、數(shù)據(jù)格式校驗(yàn))

    本次封裝引入阿里開源框架EasyExcel,EasyExcel是一個(gè)基于Java的簡(jiǎn)單、省內(nèi)存的讀寫Excel的開源項(xiàng)目。在盡可能節(jié)約內(nèi)存的情況下支持讀寫百M(fèi)的Excel。 github地址:GitHub - alibaba/easyexcel: 快速、簡(jiǎn)潔、解決大文件內(nèi)存溢出的java處理Excel工具 。 64M內(nèi)存20秒讀取75M(46W行25列)的Excel(3.0.2

    2024年02月01日
    瀏覽(42)
  • Java實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出到excel文件

    Java實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出到excel文件

    使用的依賴:Apache提供的poi包 首先導(dǎo)入依賴 ?核心實(shí)現(xiàn) ?這個(gè)工作表指的是這個(gè) 運(yùn)行結(jié)果 ? 現(xiàn)在你完成了往一個(gè)單元格里寫數(shù)據(jù),如果想要做成一個(gè)表格,那就需要循環(huán) 例如現(xiàn)在有一個(gè)需求:現(xiàn)在要統(tǒng)計(jì)學(xué)生的簽到情況,需要把學(xué)生的簽到記錄導(dǎo)出到Excel表中 這里我們用假

    2024年02月11日
    瀏覽(31)
  • 【JAVA】easyexcel 導(dǎo)出excel文件帶多個(gè)圖片

    【JAVA】easyexcel 導(dǎo)出excel文件帶多個(gè)圖片

    最終效果 ?pom版本 實(shí)現(xiàn)代碼 ?

    2024年02月16日
    瀏覽(26)
  • 【Java結(jié)合EasyExcel,模板文件填充并導(dǎo)出Excel】

    【Java結(jié)合EasyExcel,模板文件填充并導(dǎo)出Excel】

    需求描述: 客戶網(wǎng)頁上填一個(gè)Excel表格,數(shù)據(jù)存到數(shù)據(jù)庫,這個(gè)導(dǎo)出接口要做的就是從數(shù)據(jù)庫中的獲取數(shù)據(jù)并填充到模板文件,最后通過response返給前端一個(gè)下載鏈接,用戶即可獲取填充好的Excel文件。 方案一: 一開始使用的是easypoi,發(fā)現(xiàn)當(dāng)填充一行數(shù)據(jù)時(shí)是OK的,但是如果

    2024年02月09日
    瀏覽(28)
  • Java aspose 將HTML導(dǎo)出成Excel文件

    有一批表格的html文件,需要將這些表格導(dǎo)出成excel文件 使用第三方庫 aspose

    2024年02月10日
    瀏覽(21)
  • java若依框架ruoyi導(dǎo)入Excel(附詳細(xì)代碼)

    【版權(quán)所有,文章允許轉(zhuǎn)載,但須以鏈接方式注明源地址,否則追究法律責(zé)任】 【創(chuàng)作不易,點(diǎn)個(gè)贊就是對(duì)我最大的支持】 僅作為學(xué)習(xí)筆記,供大家參考 總結(jié)的不錯(cuò)的話,記得點(diǎn)贊收藏關(guān)注哦! Excel導(dǎo)入 可能出現(xiàn)的問題 1. 開發(fā)模板下載功能(如需定制列,可以單獨(dú)創(chuàng)建一

    2024年01月23日
    瀏覽(48)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包