【问题标题】:ADD EXCEL DATA TO Multidimensional ArrayList将 EXCEL 数据添加到多维 ArrayList
【发布时间】:2016-06-13 11:57:46
【问题描述】:

我对多维 ArrayLists 感到困惑。 我正在从具有 3 行 ID、日期和信息的 excel 文件中读取数据。 我设法设置了arrayList,但每个索引中都有 3rows 数据 即数组(1)id1 date1 info1。不能单独调用 info1。 我需要创建一个multy。 arraylist,我可以在其中调用 arrayindex(1) item info1。

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.......}
    ]

【问题讨论】:

  • 您不应该使用 id、date、info 作为单独的列表。您应该使用一个带有 pojo 的列表来存储一行的 thre 值

标签: java excel multidimensional-array


【解决方案1】:

分而治之是您需要遵循的方法 - 将每个问题分解为较小的问题并尝试一个一个地解决这些问题。

在我看来,您正在尝试做两件事 - 从 excel 读取数据,将数据转换为“多维 ArrayLists”,现在您不知道什么不起作用...

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


为什么要拥有“多维数组列表”?你有行和列,那么你可以从String[][] table = new String[rows][cols]; 开始吗?


您的代码中有几个问题 - 字符串 xx 不应该存在,但这只是暂时的。

永远不要忽略异常 - 你想知道有没有异常,至少记录一些东西!

【讨论】:

    【解决方案2】:
    ///////////****** 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();
    

    【讨论】:

      猜你喜欢
      • 2021-11-30
      • 2014-08-25
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-11
      • 1970-01-01
      相关资源
      最近更新 更多