java实现excel的导入

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_20009015/article/details/84647245

package com.nchu.wechatOrder.controller;

import jxl.Cell;

import jxl.Sheet;

import jxl.Workbook;

import jxl.read.biff.BiffException;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import java.io.FileInputStream;

import java.io.IOException;

扫描二维码关注公众号,回复: 4306484 查看本文章

import java.io.InputStream;

import java.util.ArrayList;

import java.util.List;

@Controller

public class ExcelController {

private String filePath;

private List list = new ArrayList();

public ExcelController(String filePath) {

this.filePath = filePath;

}

private void readExcel() throws IOException, BiffException {

//创建输入流

InputStream stream = new FileInputStream(filePath);

//获取Excel文件对象

Workbook rwb = Workbook.getWorkbook(stream);

//获取文件的指定工作表 默认的第一个

Sheet sheet = rwb.getSheet(0);

//行数(表头的目录不需要,从1开始)

for (int i = 0; i < sheet.getRows(); i++) {

//创建一个数组 用来存储每一列的值

String[] str = new String[sheet.getColumns()];

Cell cell = null;

//列数

for (int j = 0; j < sheet.getColumns(); j++) {

//获取第i行,第j列的值

cell = sheet.getCell(j, i);

str[j] = cell.getContents();

}

//把刚获取的列存入list

list.add(str);

}

}

private void outData() {

for (int i = 0; i < list.size(); i++) {

String[] str = (String[]) list.get(i);

for (int j = 0; j < str.length; j++) {

System.out.print(str[j] + '\t');

}

System.out.println();

}

}

public static void main(String args[]) throws BiffException, IOException {

ExcelController excel = new ExcelController("/Users/user12/Desktop/student1.xls");

excel.readExcel();

excel.outData();

}

@RequestMapping("toExcel")

public String toExcel() {

return "admin/Excel";

}

//上传excel

}

猜你喜欢

转载自blog.csdn.net/qq_20009015/article/details/84647245
今日推荐