Spring Boot Operation Excel

Excel frequently used in daily operations, Spring Boot used in operating POI Excel

The project github source code download

1 New Spring Boot Maven example projects

Note: This example is the IDEA development tools

  1. File> New> Project, select the figure below Spring Initializrand then click [Next] Next
  2. Fill GroupId(package name), Artifact(project name) can be. Click Next
    groupId = com.fishpro
    artifactId = Excel
  3. The choice depends Spring Web Starterfront tick.
  4. Project name is set spring-boot-study-excel.

File upload does not require the introduction of third-party components.

2-dependent introduction Pom.xml

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

3 Operation Excel

Different classes having different versions of Excel to operation of the present example uses the suffix xls version. For details, see the official documentation

3.1 Creating Workbook

  • HSSFWorkbook Previous operations Excel2003 (including 2003) version of the extension is .xls;
  • XSSFWorkbook Is the version of the operating Excel2007, the extension is .xlsx;
  • SXSSFWorkbook Is the version of the operating Excel2007, the extension is .xlsx;
    public static void CreateNewWorkbook() {
        Workbook wb = new HSSFWorkbook();
        try {
            OutputStream fileOut = new FileOutputStream("workbook.xls");
            wb.write(fileOut);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Workbook wb2 = new XSSFWorkbook();
        try (OutputStream fileOut = new FileOutputStream("workbook.xlsx")) {
            wb2.write(fileOut);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

3.2 Creating Worksheet Sheet

  • Sheet name should not exceed 31 characters
  • The name can not contain special characters
  • WorkbookUtil.createSafeSheetName can be used to create a secure worksheet name
public static void CreateNewSheet() {
        Workbook wb = new HSSFWorkbook();  // or new XSSFWorkbook();
        Sheet sheet1 = wb.createSheet("new sheet");
        Sheet sheet2 = wb.createSheet("new second sheet");
        String safeName = WorkbookUtil.createSafeSheetName("[O'Brien's sales*?]"); // returns " O'Brien's sales   "
        Sheet sheet3 = wb.createSheet(safeName);
        try {
            OutputStream fileOut = new FileOutputStream("workbook.xls");
            wb.write(fileOut);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

3.3 Creating Cell Cells

  • First, there is a column line, first created in Creating Row Cell
  • Creating a style
  • Create a value of type date
  • Creation date, decimals, characters, boolean type, etc.
  • Create a border type cell
  • Data formatting unit cell
 /**
     *## 3.3 创建单元格 Cells
     * - 先有行在有列,先要创建 Row 在创建 Cell
     * */
    public void CreateNewCell() {
        Workbook wb = new HSSFWorkbook();  // or new XSSFWorkbook();
        Sheet sheet1 = wb.createSheet("new sheet");
        //先行后列
        Row row = sheet1.createRow(0);

        //创建列
        Cell cell = row.createCell(0);
        cell.setCellValue(new Date());
        //创建一个列的样式
        CellStyle cellStyle = wb.createCellStyle();
        //获取一个帮助类设置样式
        CreationHelper createHelper = wb.getCreationHelper();
        cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
        cell = row.createCell(1);
        cell.setCellValue(new Date());
        cell.setCellStyle(cellStyle);
        //使用 Calendar
        cell = row.createCell(2);
        cell.setCellValue(Calendar.getInstance());
        cell.setCellStyle(cellStyle);
        //创建不同的类型的单元格
        row.createCell(3).setCellValue(1.1);
        row.createCell(4).setCellValue(new Date());
        row.createCell(5).setCellValue(Calendar.getInstance());
        row.createCell(6).setCellValue("a string");
        row.createCell(7).setCellValue(true);
        row.createCell(8).setCellType(CellType.ERROR);

        try {
            OutputStream fileOut = new FileOutputStream("workbook.xls");
            wb.write(fileOut);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Read and get Excel 3.4

File read manner using Excel
`` `Java
public static void OpenExcelByFile () {
the try {
the Workbook WorkbookFactory.create WB = (new new File (" workbook.xls "));
// read
Sheet sheet = wb.getSheetAt (0 ); // first
Sheet sheet1 = wb.getSheet ( "sheet1" ); // read the name of The
row row = sheet.getRow (0); // get the line
Cell cell = row.getCell (0); / / get the first line

    }catch (Exception ex){

    }

}

```

Use FileInputStream need memory support

public static void OpenExcelByFileInputStream(){
       try {
           Workbook wb = WorkbookFactory.create(new FileInputStream("workbook.xls"));
           //遍历
       }catch (Exception ex){

       }
   }

reference:

https://poi.apache.org/

https://www.iteye.com/blog/chenhailong-1498528

Guess you like

Origin www.cnblogs.com/fishpro/p/spring-boot-study-excel.html