读取阵列结构中每条记录的每行中的excel文件

时间:2016-04-18 09:14:25

标签: java excel

我正在尝试从数组中读取java中的excel文件     import java.io.File;     import 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();
}
}

当我运行它时,结果显示1列中的所有内容。我怎样才能让它在每一行显示,以便所有内容看起来都像excel中的一样? Here is my Data in excel file

2 个答案:

答案 0 :(得分:0)

您的输出循环是:

    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();
    }

当println用新行终止每个输出时,每行最后会有一个单元格。

您需要“打印”单元格,然后在每行末尾使用“println”作为换行符。有点像:

ARCHAR2

答案 1 :(得分:0)

您可以像下面一样在更好的视图中显示它,但不能像行列方式那样显示它。

输出:

enter image description here

代码:

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();
        }