如何使用Spring Boot读取Excel文件

Man*_*sal 4 java excel spring-boot

我正在制作一个Spring Boot应用程序,它将使用excel文件并存储其内容并将其存储在数据库中。我尝试了很多方法..但没有成功。有谁知道如何做到这一点。我不知道如何制作用于导入Excel文件的控制器。还有从Excel文件读取数据时我必须包括的任何依赖项

Man*_*sal 12

终于找到了解决方案。

用于上传表单的HTML文件是

<form th:action="@{/import}" method="post" enctype="multipart/form-data">

    <input type="file" th:name="file">

<input th:type="submit" value="Import" />
Run Code Online (Sandbox Code Playgroud)

控制器类为

@PostMapping("/import")
public void mapReapExcelDatatoDB(@RequestParam("file") MultipartFile reapExcelDataFile) throws IOException {

   List<Test> tempStudentList = new ArrayList<Test>();
    XSSFWorkbook workbook = new XSSFWorkbook(reapExcelDataFile.getInputStream());
    XSSFSheet worksheet = workbook.getSheetAt(0);

    for(int i=1;i<worksheet.getPhysicalNumberOfRows() ;i++) {
        Test tempStudent = new Test();

        XSSFRow row = worksheet.getRow(i);

        tempStudent.setId((int) row.getCell(0).getNumericCellValue());
        tempStudent.setContent(row.getCell(1).getStringCellValue());
            tempStudentList.add(tempStudent);   
    }
}
Run Code Online (Sandbox Code Playgroud)

确保添加依赖项

        <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.12</version>
    </dependency>
    <!-- excel 2007 over-->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.12</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

现在它将正常工作。


Rah*_*dik 7

使用 Apache POI 库,该库可通过 Maven 依赖项轻松获得。

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.15</version>
  </dependency>
Run Code Online (Sandbox Code Playgroud)

读取文件的代码

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;

public class ApachePOIExcelRead {

    private static final String FILE_NAME = "/tmp/MyFirstExcel.xlsx";

    public static void main(String[] args) {

        try {

            FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
            Workbook workbook = new XSSFWorkbook(excelFile);
            Sheet datatypeSheet = workbook.getSheetAt(0);
            Iterator<Row> iterator = datatypeSheet.iterator();

            while (iterator.hasNext()) {

                Row currentRow = iterator.next();
                Iterator<Cell> cellIterator = currentRow.iterator();

                while (cellIterator.hasNext()) {

                    Cell currentCell = cellIterator.next();
                    //getCellTypeEnum shown as deprecated for version 3.15
                    //getCellTypeEnum ill be renamed to getCellType starting from version 4.0
                    if (currentCell.getCellTypeEnum() == CellType.STRING) {
                        System.out.print(currentCell.getStringCellValue() + "--");
                    } else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
                        System.out.print(currentCell.getNumericCellValue() + "--");
                    }

                }
                System.out.println();

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

请根据您的要求修改上述程序。如果您知道您的 excel 文件列索引,那么您可以直接行读取单元格,例如row.getCell(0)where rowobject likeXSSFRow row = (XSSFRow) iterator.next();

希望这会帮助你

参考