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

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

這篇具有很好參考價值的文章主要介紹了使用Java導入、導出excel詳解(附有封裝好的工具類)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

  • ?? ? ??????? :是江迪呀
  • ??本文關(guān)鍵詞JavaExcel、導出工具類、后端
  • ??每日?? 一言:有些事情不是對的才去堅持,而是堅持了它才是對的!

前言

我們在日常開發(fā)中,一定遇到過要將數(shù)據(jù)導出為Excel的需求,那么怎么做呢?在做之前,我們需要思考下Excel的組成。Excel是由四個元素組成的分別是:WorkBook(工作簿)、Sheet(工作表)、Row(行)、Cell(單元格),其中包含關(guān)系是從左至右,,一個WorkBook可以包含多個Sheet,一個Sheet又是由多個Row組成,一個Row是由多個Cell組成。知道這些后那么我們就使用java來將數(shù)據(jù)以Excel的方式導出。讓我們一起來學習吧??!


一、引入Apache POI依賴

使用Java實現(xiàn)將數(shù)據(jù)以Excel的方式導出,需要依賴第三方的庫。我們需要再pom.xml中引入下面的依賴:

 <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
 </dependency>
 <dependency>
     <groupId>org.apache.poi</groupId>
     <artifactId>poi-ooxml</artifactId>
     <version>4.1.2</version>
 </dependency>

二、用法&步驟

2.1 創(chuàng)建Excel的元素

(1)創(chuàng)建WokrBook

Workbook workbook = new XSSFWorkbook();

(2)創(chuàng)建Sheet

 Sheet sheet = workbook.createSheet();

設置sheet的名稱

 Sheet sheet = workbook.createSheet("sheet名稱");

(3)創(chuàng)建行Row

Row row = sheet.createRow(0);

(4)創(chuàng)建單元格Cell

Cell cell = row.createCell(0, CellType.STRING);

可以指定單元格的類型,支持的類型有下面7種:

    _NONE(-1),
    NUMERIC(0),
    STRING(1),
    //公式
    FORMULA(2),
    BLANK(3),
    //布爾
    BOOLEAN(4),
    ERROR(5);

(5) 填充數(shù)據(jù)

 cell.setCellValue("蘋果");

2.3 樣式和字體

如果我們需要導出的Excel美觀一些,如設置字體的樣式加粗、顏色、大小等等,就需要創(chuàng)建樣式和字體。
創(chuàng)建樣式:

CellStyle cellStyle = workbook.createCellStyle();

(1)左右垂直居中

//左右居中
excelTitleStyle.setAlignment(HorizontalAlignment.CENTER);
// 設置垂直居中
excelTitleStyle.setVerticalAlignment(VerticalAlignment.CENTER);

(2)字體加粗、顏色

創(chuàng)建加粗樣式并設置到CellStyle 中:

Font font = workbook.createFont();
//字體顏色為紅色
font.setColor(IndexedColors.RED.getIndex());
//字體加粗
font.setBold(true);
cellStyle.setFont(font);

指定Cell單元格使用該樣式:

cell.setCellStyle(style);

(3)調(diào)整列寬和高

Sheet sheet = workbook.createSheet();
//自動調(diào)整列的寬度來適應內(nèi)容
sheet.autoSizeColumn(int column); 
// 設置列的寬度
sheet.setColumnWidth(2, 20 * 256); 

autoSizeColumn()傳遞的參數(shù)就是要設置的列索引。setColumnWidth()第一個參數(shù)是要設置的列索引,第二參數(shù)是具體的寬度值,寬度 = 字符個數(shù) * 256(例如20個字符的寬度就是20 * 256)

(4)傾斜、下劃線

Font font = workbook.createFont();
font.setItalic(boolean italic); 設置傾斜
font.setUnderline(byte underline); 設置下劃線

2.4 進階用法

(1)合并單元格

Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet(fileName);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));
  • CellRangeAddress()方法四個參數(shù)分別是fristRow:起始行、lastRow:結(jié)束行、fristCol:起始列、lastCol:結(jié)束列。
    如果你想合并從第一行到第二行從一列到第十列的單元格(一共合并20格),那么就是CellRangeAddress(0,1,0,10)

(2)字段必填

//創(chuàng)建數(shù)據(jù)驗證
DataValidationHelper dvHelper = sheet.getDataValidationHelper();
//創(chuàng)建要添加校驗的單元格對象
CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 10);
//創(chuàng)建必填校驗規(guī)則
DataValidationConstraint constraint = validationHelper.createCustomConstraint("NOT(ISBLANK(A1))");
//設置校驗
DataValidation validation = dvHelper.createValidation(constraint, addressList);
//校驗不通過 提示
validation.setShowErrorBox(true);
sheet.addValidationData(validation);
  • CellRangeAddressList()方法傳遞四個參數(shù),分別是:fristRow:起始行、lastRow:結(jié)束行、fristCol:起始列、lastCol:結(jié)束列。CellRangeAddressList(0, 0, 0, 10)表示的就是給第一行從第一列開始到第十列一共十個單元格添加數(shù)據(jù)校驗。

(3)添加公式

  • SUM:求和函數(shù)
//創(chuàng)建SUM公式
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
Cell sumCell = row.createCell(0);
sumCell.setCellFormula("SUM(A1:A10)");
//計算SUM公式結(jié)果
Cell sumResultCell = row.createCell(1);
sumResultCell.setCellValue(evaluator.evaluate(sumCell).getNumberValue());
  • AVERAGE:平均數(shù)函數(shù)
//創(chuàng)建AVERAGE公式
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
Cell averageCell = row.createCell(0);
averageCell.setCellFormula("AVERAGE(A1:A10)");

//計算AVERAGE公式結(jié)果
Cell averageResultCell = row.createCell(1);
averageResultCell.setCellValue(evaluator.evaluate(averageCell).getNumberValue());
  • COUNT:計數(shù)函數(shù)
//創(chuàng)建COUNT公式
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
Cell countCell = row.createCell(0);
countCell.setCellFormula("COUNT(A1:A10)");

//計算COUNT公式結(jié)果
Cell countResultCell = row.createCell(1);
countResultCell.setCellValue(evaluator.evaluate(countCell).getNumberValue());

  • IF:條件函數(shù)
//創(chuàng)建IF公式
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
Cell ifCell = row.createCell(0);
ifCell.setCellFormula("IF(A1>B1,\"Yes\",\"No\")");

//計算IF公式結(jié)果
Cell ifResultCell = row.createCell(1);
ifResultCell.setCellValue(evaluator.evaluate(ifCell).getStringValue());

  • CONCATENATE:連接函數(shù)
//創(chuàng)建CONCATENATE公式
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
Cell concatenateCell = row.createCell(0);
concatenateCell.setCellFormula("CONCATENATE(A1,\" \",B1)");

//計算CONCATENATE公式結(jié)果
Cell concatenateResultCell = row.createCell(1);
concatenateResultCell.setCellValue(evaluator.evaluate(concatenateCell).getStringValue());

(4)下拉選擇

//下拉值
private List<String> grade = Arrays.asList("高", "中", "低");
(此處省略n行代碼)
Sheet sheet = workbook.createSheet("sheet");
DataValidation dataValidation = this.addPullDownConstraint(i, sheet, grade );
sheet.addValidationData(dataValidation);

(5)設置單元格的數(shù)據(jù)類型

  • 數(shù)字格式
// 設置單元格樣式 - 數(shù)字格式
CellStyle numberCellStyle = workbook.createCellStyle();
numberCellStyle.setDataFormat(workbook.createDataFormat().getFormat("#,##0.00"));
//指定單元格
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellStyle(numberCellStyle);
  • 日期格式
// 設置單元格樣式 - 日期格式
CellStyle dateCellStyle = workbook.createCellStyle();
dateCellStyle.setDataFormat(workbook.createDataFormat().getFormat("yyyy-MM-dd"));
//指定單元格
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellStyle(dateCellStyle);

三、導出完整示例

下面的示例使用SpringBoot項目來演示:
pom.xml依賴:

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.7.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.7.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.16</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.21</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>

Controller層代碼:

package shijiangdiya.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import shijiangdiya.utils.ExportUtils;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/sync")
public class ExportController {
}

(1)代碼

    @Autowired
    private HttpServletResponse response;

    @PostMapping("/export")
    public void export() {
    	//模擬json數(shù)據(jù)
        String data = "[{\n" +
                "    \"studentId\": \"20210101\",\n" +
                "    \"name\": \"Alice\",\n" +
                "    \"age\": 20,\n" +
                "    \"credit\": 80\n" +
                "  },\n" +
                "  {\n" +
                "    \"studentId\": \"20210102\",\n" +
                "    \"name\": \"Bob\",\n" +
                "    \"age\": 21,\n" +
                "    \"credit\": 85\n" +
                "  },\n" +
                "  {\n" +
                "    \"studentId\": \"20210103\",\n" +
                "    \"name\": \"Charlie\",\n" +
                "    \"age\": 22,\n" +
                "    \"credit\": 90\n" +
                "  },\n" +
                "  {\n" +
                "    \"studentId\": \"20210104\",\n" +
                "    \"name\": \"David\",\n" +
                "    \"age\": 20,\n" +
                "    \"credit\": 75\n" +
                "  },\n" +
                "  {\n" +
                "    \"studentId\": \"20210105\",\n" +
                "    \"name\": \"Emily\",\n" +
                "    \"age\": 21,\n" +
                "    \"credit\": 82\n" +
                "  },\n" +
                "  {\n" +
                "    \"studentId\": \"20210106\",\n" +
                "    \"name\": \"Frank\",\n" +
                "    \"age\": 22,\n" +
                "    \"credit\": 88\n" +
                "  },\n" +
                "  {\n" +
                "    \"studentId\": \"20210107\",\n" +
                "    \"name\": \"Grace\",\n" +
                "    \"age\": 20,\n" +
                "    \"credit\": 81\n" +
                "  },\n" +
                "  {\n" +
                "    \"studentId\": \"20210108\",\n" +
                "    \"name\": \"Henry\",\n" +
                "    \"age\": 21,\n" +
                "    \"credit\": 89\n" +
                "  },\n" +
                "  {\n" +
                "    \"studentId\": \"20210109\",\n" +
                "    \"name\": \"Isaac\",\n" +
                "    \"age\": 22,\n" +
                "    \"credit\": 92\n" +
                "  },\n" +
                "  {\n" +
                "    \"studentId\": \"20210110\",\n" +
                "    \"name\": \"John\",\n" +
                "    \"age\": 20,\n" +
                "    \"credit\": 78\n" +
                "  },\n" +
                "  {\n" +
                "    \"studentId\": \"20210111\",\n" +
                "    \"name\": \"Kelly\",\n" +
                "    \"age\": 21,\n" +
                "    \"credit\": 84\n" +
                "  },\n" +
                "  {\n" +
                "    \"studentId\": \"20210112\",\n" +
                "    \"name\": \"Linda\",\n" +
                "    \"age\": 22,\n" +
                "    \"credit\": 87\n" +
                "  },\n" +
                "  {\n" +
                "    \"studentId\": \"20210113\",\n" +
                "    \"name\": \"Mike\",\n" +
                "    \"age\": 20,\n" +
                "    \"credit\": 77\n" +
                "  },\n" +
                "  {\n" +
                "    \"studentId\": \"20210114\",\n" +
                "    \"name\": \"Nancy\",\n" +
                "    \"age\": 21,\n" +
                "    \"credit\": 83\n" +
                "  },\n" +
                "  {\n" +
                "    \"studentId\": \"20210115\",\n" +
                "    \"name\": \"Oscar\",\n" +
                "    \"age\": 22,\n" +
                "    \"credit\": 91\n" +
                "  },\n" +
                "  {\n" +
                "    \"studentId\": \"20210116\",\n" +
                "    \"name\": \"Paul\",\n" +
                "    \"age\": 20,\n" +
                "    \"credit\": 76\n" +
                "  },\n" +
                "  {\n" +
                "    \"studentId\": \"20210117\",\n" +
                "    \"name\": \"Queen\",\n" +
                "    \"age\": 21,\n" +
                "    \"credit\": 86\n" +
                "  },\n" +
                "  {\n" +
                "    \"studentId\": \"20210118\",\n" +
                "    \"name\": \"Rachel\",\n" +
                "    \"age\": 22,\n" +
                "    \"credit\": 94\n" +
                "  },\n" +
                "  {\n" +
                "    \"studentId\": \"20210119\",\n" +
                "    \"name\": \"Sarah\",\n" +
                "    \"age\": 20,\n" +
                "    \"credit\": 79\n" +
                "  },\n" +
                "  {\n" +
                "    \"studentId\": \"20210120\",\n" +
                "    \"name\": \"Tom\",\n" +
                "    \"age\": 21,\n" +
                "    \"credit\": 80\n" +
                "  }\n" +
                "]\n";
        ExportUtils.exportExcel("學生信息", data, Student.class, response);
    }

(2)工具類

  /**
     * 數(shù)據(jù)導出
     * @param fileName 導出excel名稱
     * @param data 導出的數(shù)據(jù)
     * @param c 導出數(shù)據(jù)的實體class
     * @param response 響應
     * @throws Exception
     */
    public static void exportExcel(String fileName, String data, Class<?> c, HttpServletResponse response) throws Exception {
        try {
            // 創(chuàng)建表頭
            // 創(chuàng)建工作薄
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet();
            // 創(chuàng)建表頭行
            Row rowHeader = sheet.createRow(0);
            if (c == null) {
                throw new RuntimeException("Class對象不能為空!");
            }
            Field[] declaredFields = c.getDeclaredFields();
            List<String> headerList = new ArrayList<>();
            if (declaredFields.length == 0) {
                return;
            }
            for (int i = 0; i < declaredFields.length; i++) {
                Cell cell = rowHeader.createCell(i, CellType.STRING);
                String headerName = String.valueOf(declaredFields[i].getName());
                cell.setCellValue(headerName);
                headerList.add(i, headerName);
            }
            // 填充數(shù)據(jù)
            List<?> objects = JSONObject.parseArray(data, c);
            Object obj = c.newInstance();
            if (!CollectionUtils.isEmpty(objects)) {
                for (int o = 0; o < objects.size(); o++) {
                    Row rowData = sheet.createRow(o + 1);
                    for (int i = 0; i < headerList.size(); i++) {
                        Cell cell = rowData.createCell(i);
                        Field nameField = c.getDeclaredField(headerList.get(i));
                        nameField.setAccessible(true);
                        String value = String.valueOf(nameField.get(objects.get(o)));
                        cell.setCellValue(value);
                    }
                }
            }
            response.setContentType("application/vnd.ms-excel");
            String resultFileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + resultFileName + ";" + "filename*=utf-8''" + resultFileName);
            workbook.write(response.getOutputStream());
            workbook.close();
            response.flushBuffer();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

(2)結(jié)果

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

四、導入完整示例

(1)代碼

@PostMapping("/import")
    public void importExcel(@RequestParam("excel") MultipartFile excel){
        Workbook workbook = null;
        try {
            workbook = WorkbookFactory.create(excel.getInputStream());
            Sheet sheet = workbook.getSheetAt(0);
            List<Student> students = new ArrayList<>();
            int i = 0;
            for (Row row : sheet) {
                Row row1 = sheet.getRow(i + 1);
                if(row1 != null){
                    Student data = new Student();
                    data.setStudentId(Integer.parseInt(row1.getCell(0).getStringCellValue()));
                    data.setName(row1.getCell(1).getStringCellValue());
                    data.setAge(Integer.parseInt(row1.getCell(2).getStringCellValue()));
                    data.setCredit(Integer.parseInt(row1.getCell(3).getStringCellValue()));
                    students.add(data);
                }
            }
            System.out.println(students);
            workbook.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

(2)工具類

 /**
     * 導入
     * @param workbook 工作簿
     * @param c 實體類
     * @return 實體類集合
     */
    public static <T> List<T> importExcel(Workbook workbook,Class<?> c){
        List<T> dataList = new ArrayList<>();
        try {
            Sheet sheet = workbook.getSheetAt(0);
            int i = 0;
            T o = null;
            for (Row row : sheet) {
                Row row1 = sheet.getRow(i + 1);
                if(row1 != null){
                    o = (T) c.newInstance();
                    Field[] declaredFields = c.getDeclaredFields();
                    for (int i1 = 0; i1 < declaredFields.length; i1++) {
                        String name = declaredFields[i1].getName();
                        Field declaredField1 = o.getClass().getDeclaredField(name);
                        declaredField1.setAccessible(true);
                        Cell cell = row1.getCell(i1);
                        String type = declaredFields[i1].getType().getName();
                        String value = String.valueOf(cell);
                        if(StringUtils.equals(type,"int") || StringUtils.equals(type,"Integer")){
                            declaredField1.set(o,Integer.parseInt(value));
                        } else if(StringUtils.equals(type,"java.lang.String") || StringUtils.equals(type,"char") || StringUtils.equals(type,"Character") ||
                                StringUtils.equals(type,"byte") || StringUtils.equals(type,"Byte")){
                            declaredField1.set(o,value);
                        } else if(StringUtils.equals(type,"boolean") || StringUtils.equals(type,"Boolean")){
                            declaredField1.set(o,Boolean.valueOf(value));
                        } else if(StringUtils.equals(type,"double") || StringUtils.equals(type,"Double")){
                            declaredField1.set(o,Double.valueOf(value));
                        } else if (StringUtils.equals(type,"long") || StringUtils.equals(type,"Long")) {
                            declaredField1.set(o,Long.valueOf(value));
                        } else if(StringUtils.equals(type,"short") || StringUtils.equals(type,"Short")){
                            declaredField1.set(o,Short.valueOf(value));
                        } else if(StringUtils.equals(type,"float") || StringUtils.equals(type,"Float")){
                            declaredField1.set(o,Float.valueOf(value));
                        }
                    }
                }
                dataList.add(o);
            }
            workbook.close();
            return dataList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return dataList;
    }

注意:導入工具類僅限Java的八大基礎(chǔ)數(shù)據(jù)類型String類型。如果還有其他類型需要自己擴展。

(3)結(jié)果

學生信息集合:
使用Java導入、導出excel詳解(附有封裝好的工具類)文章來源地址http://www.zghlxwxcb.cn/news/detail-459569.html

到了這里,關(guān)于使用Java導入、導出excel詳解(附有封裝好的工具類)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

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

領(lǐng)支付寶紅包贊助服務器費用

相關(guān)文章

  • excel文件導入或?qū)С鯦ava代碼示例

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

    ? ?判斷excel的格式,同時兼容2003和2007 ?獲取行數(shù)據(jù) ?//獲取excel列表內(nèi)的對應數(shù)據(jù)格式 3、以下為可能會用到的導出實例文件,上傳文件服務器的過程? File格式轉(zhuǎn)換MultipartFile格式的例子 ? -------------------------------------以下無正文-----------------------------------------------------------

    2024年02月16日
    瀏覽(21)
  • Java導出Excel模板,導出數(shù)據(jù)到指定模板,通過模板導入數(shù)據(jù)(一)

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

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

    2024年02月03日
    瀏覽(30)
  • 關(guān)于java操作excel導入導出三種方式

    關(guān)于java操作excel導入導出三種方式

    在平時的業(yè)務系統(tǒng)開發(fā)中,少不了需要用到導出、導入excel功能,今天我們就一起來總結(jié)一下,如果你正為此需求感到困惑,那么閱讀完本文,你一定會有所收獲! 大概在很久很久以前,微軟的電子表格軟件 Excel 以操作簡單、存儲數(shù)據(jù)直觀方便,還支持打印報表,在誕生之初

    2024年02月05日
    瀏覽(19)
  • Easys Excel的表格導入(讀)導出(寫)-----java

    Easys Excel的表格導入(讀)導出(寫)-----java

    可以學習一些新知識: EasyExcel官方文檔 - 基于Java的Excel處理工具 | Easy Excel excel的一些優(yōu)點和缺點 java解析excel的框架有很多 : poi jxl,存在問題:非常的消耗內(nèi)存, easyexcel 我們遇到再大的excel都不會出現(xiàn)內(nèi)存溢出的問題 能夠?qū)⒁粋€原本3M excel文件,poi來操作將會占用內(nèi)存 100MB,

    2024年02月13日
    瀏覽(24)
  • Java原生POI實現(xiàn)的Excel導入導出(簡單易懂)

    Java原生POI實現(xiàn)的Excel導入導出(簡單易懂)

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

    2024年02月11日
    瀏覽(27)
  • 使用luckysheet實現(xiàn)excel導入導出

    使用luckysheet實現(xiàn)excel導入導出

    luckysheet-demo: luckysheet-demoexcel導入導出實例 使用組件 1.?luckysheet在線excel 2. luckyexcel excel導入插件 3. exceljs 導出excel數(shù)據(jù) 導出exceljs的二次封裝,可直接使用如下

    2024年02月12日
    瀏覽(19)
  • 使用EasyExcel實現(xiàn)Excel的導入導出

    在真實的開發(fā)者場景中,經(jīng)常會使用excel作為數(shù)據(jù)的載體,進行數(shù)據(jù)導入和導出的操作,使用excel的導入和導出有很多種解決方案,本篇記錄一下EasyExcel的使用。 EasyExcel是一個開源的項目,是阿里開發(fā)的。EasyExcel可以簡化Excel表格的導入和導出操作,使用起來簡單快捷,易上手

    2023年04月15日
    瀏覽(33)
  • 使用EasyExcel實現(xiàn)Excel表格的導入導出

    使用EasyExcel實現(xiàn)Excel表格的導入導出

    Java解析、生成Excel比較有名的框架有Apache poi、jxl。但他們都存在一個嚴重的問題就是非常的耗內(nèi)存,poi有一套SAX模式的API可以一定程度的解決一些內(nèi)存溢出的問題,但POI還是有一些缺陷,比如07版Excel解壓縮以及解壓后存儲都是在內(nèi)存中完成的,內(nèi)存消耗依然很大。 easyexcel重

    2024年02月12日
    瀏覽(18)
  • springboot中使用EasyExcel實現(xiàn)Excel 導入導出

    springboot中使用EasyExcel實現(xiàn)Excel 導入導出

    EasyExcel 是一款基于 Java 的簡單易用的 Excel 文件操作工具。它提供了豐富的 API,可以方便地讀取、寫入和操作 Excel 文件,支持常見的 Excel 操作,如讀取/寫入單元格數(shù)據(jù)、合并單元格、設置樣式、處理大數(shù)據(jù)量等。EasyExcel能大大減少占用內(nèi)存的主要原因是在解析Excel時沒有將

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

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

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

    2024年02月01日
    瀏覽(42)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包