06,SpringMVC项目导入wps表格文件

(1),jar包依赖

        <!--POI 相关jar start -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>${poi.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>${poi.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>${poi.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>${poi.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-examples</artifactId>
            <version>${poi.version}</version>
        </dependency>
        <!--POI 相关jar end -->

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.2</version>
        </dependency>

(2),multipartResolver配置bean

	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="utf-8"></property>
		<!--上传文件上限单位字节,10M-->
		<property name="maxUploadSize" value="10485760"></property>
		<property name="maxInMemorySize" value="40960"></property>
	</bean>

(3),选择上传wps表格模板文件的jsp页面

注意此行不是代码:http://localhost:8085/bl_mave_wf_war_exploded/testExamp/studentListJsp.jsp 这是跳转本页的链接

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>

<%
    String path = request.getContextPath();
    String contextPath = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">
    <title></title>
</head>
<body>
    <form action="<%=contextPath %>/studentConter/importExcelFile" method="post" enctype="multipart/form-data">
        请选择要导入的excel模板文件:<input type="file" value="请选择" name="upFileName" />
        <input type="submit" value="导入" />
    </form>
</body>
<script type="text/javascript">
</script>
</html>

(4),后端java代码解析表格文件内容

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import java.io.InputStream;
/**
* @date: 2022/10/30 07:12
* @desc: 测试控制类
*/
@Controller
@RequestMapping(value = "/studentConter")
public class StudentController {
@RequestMapping(value = "/importExcelFile", method = RequestMethod.POST, produces = "text/html;charset=UTF-8" )
public String importExcelFile(@RequestParam(value = "upFileName", required = true) CommonsMultipartFile commonsMultipartFile ) {
    // 读取文件内容
    String upFileName = commonsMultipartFile.getOriginalFilename();
    System.out.println("导入的文件名称:"+ upFileName);
    try {
        readExcelFile(upFileName, commonsMultipartFile.getInputStream());
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return "";
}

// 读取文件内容
private void readExcelFile(String fileName, InputStream excelInStream) throws Exception {
    XSSFWorkbook xssfWorkbook = new XSSFWorkbook(excelInStream);
    int sheetSize = xssfWorkbook.getNumberOfSheets();
    for (int sheetIdx = 0; sheetIdx < sheetSize; sheetIdx++) {
        XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(sheetIdx);
        if (xssfSheet == null || xssfSheet.getLastRowNum() == 0)
            continue;
        // 循环sheet选项中的行
        for(int rowIdx = 0; rowIdx < xssfSheet.getLastRowNum(); rowIdx++) {
            System.out.println("开始输出表格行中的数据:");
            forHssfRow(xssfSheet.getRow(rowIdx));
        }
    }
}
// 在表格行中循环列格
private void forHssfRow(XSSFRow xssfRow) {
    int minColIdx = xssfRow.getFirstCellNum();
    int maxColIdx = xssfRow.getLastCellNum();
    for (int colIdx = minColIdx; colIdx < maxColIdx; colIdx++) {
        XSSFCell xssfCell = xssfRow.getCell(colIdx);
        if(xssfCell == null)
            continue;
        System.out.println("单元内容:"+ getStringVal(xssfCell));
    }
}
private String getStringVal(XSSFCell xssfCell) {
    switch (xssfCell.getCellType()) {
        case Cell.CELL_TYPE_BOOLEAN :
            return xssfCell.getBooleanCellValue() ?"true":"false";
        case Cell.CELL_TYPE_FORMULA :
            return xssfCell.getCellFormula();
        case Cell.CELL_TYPE_NUMERIC :
            xssfCell.setCellType(Cell.CELL_TYPE_STRING);
            return xssfCell.getStringCellValue();
        case Cell.CELL_TYPE_STRING :
            return xssfCell.getStringCellValue();
        default :
            return "";
    }
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringMVC导入Excel文件有多种方法,以下是其中一种常见的方法: 1. 准备Excel文件 首先需要准备一个Excel文件,可以使用Microsoft Excel或类似的软件创建。 2. 添加依赖 在pom.xml中添加Apache POI依赖,如下所示: ``` <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> ``` 3. 创建控制器 创建一个Spring MVC控制器来处理文件上传请求。在控制器中添加一个方法,该方法使用MultipartFile参数接收上传的文件,并使用Apache POI读取Excel文件中的数据。 ``` @Controller public class ExcelController { @PostMapping("/upload") public String uploadExcel(@RequestParam("file") MultipartFile file) throws IOException { Workbook workbook = new XSSFWorkbook(file.getInputStream()); Sheet sheet = workbook.getSheetAt(0); for (Row row : sheet) { for (Cell cell : row) { System.out.print(cell.getStringCellValue() + "\t"); } System.out.println(); } workbook.close(); return "redirect:/success"; } } ``` 4. 定义表单页面 在表单页面中添加一个文件上传组件,并将其提交到控制器中的uploadExcel方法。 ``` <form method="post" action="/upload" enctype="multipart/form-data"> <input type="file" name="file" /> <button type="submit">上传</button> </form> ``` 5. 运行应用程序 在浏览器中访问表单页面,选择要上传的Excel文件并提交表单。控制器将读取Excel文件中的数据并打印到控制台中。如果上传成功,将重定向到成功页面。 注意事项: - 在上传文件时,应该添加enctype="multipart/form-data"属性到表单标签中,否则文件将无法上传。 - Apache POI提供了许多API来处理Excel文件,可以根据具体需求进行选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值