java - 如何从 excel 文件中读取值并存储在数组中?

标签 java jxl

我想读取 excel 工作表值并将这些值存储在 Java 数组中。

我已经准备好读取 Excel 工作表的代码,但我无法自定义它以将这些值存储在数组中。

这是我读取 excel 表的代码:

package com.core.testscripts;

import java.io.File;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class NewExcel 
{

    private String inputFile;

    public void setInputFile(String inputFile) 
    {
        this.inputFile = inputFile;
    }

    public void read() throws IOException  
    {
        File inputWorkbook = new File(inputFile);
        Workbook w;
        try 
        {
            w = Workbook.getWorkbook(inputWorkbook);
            // Get the first sheet
            Sheet sheet = w.getSheet(0);
            // Loop over first 10 column and lines

            for (int j = 0; j < sheet.getColumns(); j++) 
            {
                for (int i = 0; i < sheet.getRows(); i++) 
                {
                    Cell cell = sheet.getCell(j, i);
                    System.out.println(cell.getContents());
                }
            }
        } 
        catch (BiffException e) 
        {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException 
    {
        NewExcel test = new NewExcel();
        test.setInputFile("D:/hellohowareyou.xls");
        test.read();
    }

}

最佳答案

import java.io.File;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class NewExcel 
{

    private String inputFile;
    String[][] data = null;
    public void setInputFile(String inputFile) 
    {
        this.inputFile = inputFile;
    }

    public String[][] read() throws IOException  
    {
        File inputWorkbook = new File(inputFile);
        Workbook w;

        try 
        {
            w = Workbook.getWorkbook(inputWorkbook);
            // Get the first sheet


            Sheet sheet = w.getSheet(0);
            data = new String[sheet.getColumns()][sheet.getRows()];
            // Loop over first 10 column and lines
       //     System.out.println(sheet.getColumns() +  " " +sheet.getRows());
            for (int j = 0; j <sheet.getColumns(); j++) 
            {
                for (int i = 0; i < sheet.getRows(); i++) 
                {
                    Cell cell = sheet.getCell(j, i);
                    data[j][i] = cell.getContents();
                  //  System.out.println(cell.getContents());
                }
            }

         /*   for (int j = 0; j < data.length; j++) 
            {
                for (int i = 0; i <data[j].length; i++) 
                {

                    System.out.println(data[j][i]);
                }
            } */

        } 
        catch (BiffException e) 
        {
            e.printStackTrace();
        }
    return data;
    }


}

关于java - 如何从 excel 文件中读取值并存储在数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9871093/

相关文章:

java - Apache POI 生成损坏的 Excel 文件

java - 在 Java 中使用 JXL 复制工作表

java - 不能从静态上下文引用非静态变量

java - 如何使用servlet发送excel文件

java - Spring 启动 + LocalDate : "No primary or default constructor"

java - 功能与 Linux 和 Java

java - 使用 Java 和 JXL API 编辑 Excel 电子表格

java - 使用 Java 从现有 Excel 文件读取单元格并将其写入新的 Excel 文件

java - 具有特定边框颜色和特定背景颜色的裁剪矩形 JAVA

java - 为什么在遇到断点时使用 JAR 应用程序的 GDB 调试 native 代码会挂起 Ubuntu UI(X11 进程)?