Maven环境下SSM框架excel的数据读取

0.准备工作

1.上传文件代码查看本人之前的博客:
https://blog.youkuaiyun.com/qq_37067955/article/details/85338375

1.编写excel读取的util的工具

#ReadExcel.java

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.springframework.web.context.request.NativeWebRequest;


import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
 
public class ReadExcel {
	public void excel(String  fileUrl) throws Exception {
		//不支持Excel2007格式(也就是xlsx格式文件)
		File file = new File(fileUrl);
		Workbook wb = Workbook.getWorkbook(file);
		Sheet[] sheets = wb.getSheets();
		
		
		//遍历每页
		for(Sheet s : sheets){
			System.out.println(s.getName() + " : ");
			int rows = s.getRows();
			if(rows > 0){
				//遍历每行
				for(int i = 2 ;i < rows ; i++){
					System.out.print("行" + i + " : ");
					Cell[] cells = s.getRow(i);
					if(cells.length > 0){
						//遍历每行中的每列
						String str = "";
						for(Cell c : cells){
							str = c.getContents().trim();//此处是获取到的每个单元格的内容,可以在此进行操作
							System.out.print(str + ";");
						}
						System.out.println();
					}
				}
			}
		}
	}
}

3 工具类的调用(在controller中调用)

@RequestMapping(value="upload",method=RequestMethod.POST)
    @ResponseBody
    public String upload(MultipartFile file,HttpServletRequest request) throws IOException{  
		String path = request.getSession().getServletContext().getRealPath("upload");
		
        String fileName = file.getOriginalFilename();    
        File dir = new File(path,fileName);          
        if(!dir.exists()){  
            dir.mkdirs();  
        }  

        file.transferTo(dir);
        
        ReadExcel readExcel = new ReadExcel();
			try {
				System.out.println(path+"\\"+fileName);
				readExcel.excel(path+"\\"+fileName);// 此处调用工具类,需要传一个excel的文件的路径
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        return fileName;  
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DYS_房东的猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值