将EXCEL数据添加到多维ArrayList

问题描述:

我正在使用多维ArrayLists。 我正在读取具有3行ID,日期和信息的Excel文件中的数据。 我设法设置arrayList,但在每个索引 即3列数据,即数组(1)id1 date1 info1。无法单独调用info1。 我需要创建一个multy。 arraylist在那里我可以打电话给arrayindex(1)项目info1。将EXCEL数据添加到多维ArrayList

public class MainActivity extends AppCompatActivity { 
String xx; 

String show; 
int count=0; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


} 
    public void order(View v) { 
     try { 
      AssetManager am=getAssets(); 
      InputStream is=am.open("test.xls"); 
      Workbook wb =Workbook.getWorkbook(is); 
      Sheet s=wb.getSheet(0); 
      int row =s.getRows(); 
      int col=s.getColumns(); 
      xx=""; 
      for(int i=0; i<row ; i++) 
      { 
       for (int c=0; c<col; c++) 
       { 
        Cell z=s.getCell(c,i); 
        xx=xx+z.getContents(); 
        xx=xx+" "; 
         mainArray(); 

       } 
       xx=xx+"\n"; 
      } 

      display(xx); 
     } 
     catch (Exception e) 
     { 
     } 
    } 
public void display(String value) 
{ 
    TextView x=(TextView) findViewById(R.id.display); 
    x.setText(value); 


} 
public void mainArray() { 

    ArrayList arrayList = new ArrayList(); 



    arrayList.add(xx); 
    Log.d("log1","Size of al after additions: " + arrayList.size()); 


    Log.d("log1","Contents of al: " + arrayList); 

} 

}

如何启动一个多维数组列表(ID,日期,资讯),以及如何在迭代我可以根据行/线,数据在正确的名单追加。

ArrayList 
    [ 
    list_Id {id1, id2,id3,.............} 
    list_Date {date1, date2,date3.......} 
    list_Info {info1, info2, info3.......} 
    ] 
+0

您不应该使用id,date,info as seperate Lists。你应该使用一个List来存储行的三维值 – Jens

分而治之的是你需要采取的办法 - 打破每一个问题,以较小的问题,并试图解决这些一个接一个。

这在我看来,你正在尝试做的两件事情 - 从Excel中读取数据,将数据转换为“多维的ArrayList”,现在你不知道什么是不工作...

请,尝试从Excel首先打印数据到控制台 - 您将验证,您的阅读部分正在工作。


为什么你想要“多维ArrayLists”?你有行和列,所以你可以从String[][] table = new String[rows][cols];开始?


有在你的代码的几个问题 - 字符串XX不应该存在的,但它只是一个临时我会说。

永远不要忽略异常 - 你如何知道有什么异常,至少记录一些东西!

///////////****** WRITE Excel TO SD ***///////////// 
    Fnamexls="testfile" + ".xls"; 
    File sdCard = Environment.getExternalStorageDirectory(); 
    directory = new File (sdCard.getAbsolutePath() + "/My File"); 
    directory.mkdirs(); 
    sdFile = new File(directory, Fnamexls); 
    if(sdFile.exists()) { 
//Do something 
     Log.d("logW","exixts"); 
    } 
    else { 
// Do something else. 
     Log.d("logW", "does not exixt"); 
     wbSettings = new WorkbookSettings(); 
     wbSettings.setLocale(new Locale("en", "EN")); 
     try { 
      wrWorkbook = Workbook.createWorkbook(sdFile, wbSettings); 
      writeSheet = wrWorkbook.createSheet("First Sheet", 0); 
      int row = s.getRows(); 
      int col = s.getColumns(); 
      xx = ""; 
      for (int c = 0; c < col; c++) { 
       for (int r = 0; r < row; r++) { 
        Cell z = s.getCell(c, r); 
        xx = z.getContents(); 
        Label label4 = new Label(c, r, String.valueOf(xx)); 
        try { 
         writeSheet.addCell(label4); 
        } catch (RowsExceededException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } catch (WriteException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
      } 
      wrWorkbook.write(); 
      try { 
       wrWorkbook.close(); 
      } catch (WriteException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      //createExcel(excelSheet); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    readSD();