简体   繁体   English

在数组结构中的每条记录的每一行中读取Excel文件

[英]Read excel file in each line for each record in array structure

I am trying to read excel file from java in array import java.io.File; 我试图从数组导入java.io.File中的java中读取excel文件; import java.io.IOException; 导入java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class ArrayData
{

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;
}
public static void main(String[] args) throws IOException 
{
    ArrayData test = new ArrayData();
    test.setInputFile("StockTickerStreamData.xls");
    test.read();
}
}

When i run it, the result shows everything in 1 column. 当我运行它时,结果将在1列中显示所有内容。 how can i make it show in each line so that everything looks like the one in excel? 我怎样才能使它显示在每一行中,以便一切看起来都像Excel中的那一行? Here is my Data in excel file 这是我在Excel文件中的数据

Your output loop is: 您的输出循环是:

    for (int j = 0; j < data.length; j++) {
        for (int i = 0; i <data[j].length; i++) {
            System.out.println(data[j][i]);
        }
    }

As println terminates each output with a new line, you'll end up with a cell each row. 当println用新行终止每个输出时,您将在每行最后一个单元格。

You need to "print" the cells, and followup with a "println" for the newline at the end of each row. 您需要“打印”单元格,并在每行末尾的换行符后面加上“ println”。 Something along the lines of: 类似于以下内容:

    for (int j = 0; j < data.length; j++) {
        for (int i = 0; i <data[j].length; i++) {
            System.out.print(data[j][i] + ";");   // no "ln" ;-)
        }
        System.out.println();
    }

You can display it in a better view just like below but cant probably display it like in row column manner. 您可以像下面那样以更好的视图显示它,但可能无法以行列的方式显示它。

OUTPUT : 输出:

在此处输入图片说明

CODE : 代码:

for (int j = 0; j < data.length; j++) {
            for (int i = 0; i <data[j].length; i++) {
                if(data[j][i].equals("BidPrice")|| data[j][i].equals("AskPrice")){
                    System.out.print(data[j][i] + "\t");
                }else{
                    System.out.print(data[j][i] + "\t\t");
                }
            }

            System.out.println();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM