java读取Excel表格中的数据

1、需求

用java代码读取hello.xls表格中的数据

2、hello.xls表格

3、java代码

所需要的jar包(maven)

 <!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl -->
        <dependency>
            <groupId>net.sourceforge.jexcelapi</groupId>
            <artifactId>jxl</artifactId>
            <version>2.6.12</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/jexcelapi/jxl -->
        <dependency>
            <groupId>jexcelapi</groupId>
            <artifactId>jxl</artifactId>
            <version>2.4.2</version>
        </dependency>
package com.test;  
import java.io.File;  
import jxl.*;   
public class ReadExcel{  
    public static void main(String[] args) {  
        int i;  
        Sheet sheet;  
        Workbook book;  
        Cell cell1,cell2,cell3,cell4,cell5,cell6,cell7;  
        try {   
            //hello.xls为要读取的excel文件名  
            book= Workbook.getWorkbook(new File("D://hello.xls"));   
              
            //获得第一个工作表对象(ecxel中sheet的编号从0开始,0,1,2,3,....)  
            sheet=book.getSheet(0);   
            //获取左上角的单元格  
            cell1=sheet.getCell(0,0);  
            System.out.println("标题:"+cell1.getContents());   
              
            i=1;  
            while(true)  
            {  
                //获取每一行的单元格   
                cell1=sheet.getCell(0,i);//(列,行)  
                cell2=sheet.getCell(1,i);  
                cell3=sheet.getCell(2,i);  
                cell4=sheet.getCell(3,i);  
                cell5=sheet.getCell(4,i);  
                cell6=sheet.getCell(5,i);  
                cell7=sheet.getCell(6,i);  
                if("".equals(cell1.getContents())==true)    //如果读取的数据为空  
                    break;  
                System.out.println(cell1.getContents()+"\t"+cell2.getContents()+"\t"+cell3.getContents()+"\t"+cell4.getContents()  
                        +"\t"+cell5.getContents()+"\t"+cell6.getContents()+"\t"+cell7.getContents());   
                i++;  
            }  
            book.close();   
        }  
        catch(Exception e)  { }   
    }  
}

4、读取结果

猜你喜欢

转载自blog.csdn.net/qq_39999478/article/details/106319192
今日推荐