Java Excel 操作

Java Excel 操作

pom.xml

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.1</version>
        </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>

操作

import com.npy.computer.student.model.Student;
import com.npy.computer.student.service.StudentService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * @author NNShadow
 * @date 2019/11/14 12:50
 */
@RestController
public class ExcalImport {
    @Autowired
    private StudentService studentService;

    public void readExcel(File file) throws Exception {
        InputStream inputStream = new FileInputStream(file);
        String fileName = file.getName();
        Workbook wb = null;
        //.xls 使用
        wb = new HSSFWorkbook(inputStream);
        //.xlsx 使用
//        wb=new XSSFWorkbook(inputStream);
        //对应 Excel 中的表格 id
        Sheet sheet = wb.getSheetAt(0);
        //获取第一行
        int firstRowIndex = sheet.getFirstRowNum();
        //获取最后一行
        int lastRowIndex = sheet.getLastRowNum();
        //第一行普遍是标题,故不用
        for (int rowIndex = firstRowIndex + 1; rowIndex <= lastRowIndex; rowIndex++) {
            Row row = sheet.getRow(rowIndex);
            if (row != null) {
                //Student 为实例化的对象,可根据自己需求构建
                Student student = new Student();
                Cell cell = row.getCell(0);
                student.setStudentId(cell.toString());
                cell = row.getCell(1);
                student.setStudentName(cell.toString());
                cell = row.getCell(2);
                student.setGender(cell.toString());
                cell = row.getCell(3);
                student.setBorn(cell.toString());
                cell = row.getCell(4);
                student.setNation(cell.toString());
                cell = row.getCell(5);
                student.setBirthPlace(cell.toString());
                cell = row.getCell(6);
                student.setLive(cell.toString());
                display(student);
            }
        }
    }

    public void display(Student student) {
        System.out.println("excel display: "
                + student.getStudentId() + " "
                + student.getStudentName() + " "
                + student.getGender() + " "
                + student.getBorn() + " "
                + student.getNation() + " "
                + student.getBirthPlace() + " "
                + student.getLive() + " ");
    }

    public void make() throws Exception {
        //读取 excal
        File file = new File("文件所在位置");
        readExcel(file);
        System.out.println("finish");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值