简体   繁体   English

将excel字符串数据存储到数组列表

[英]Store excel string data to an Array list

I need to store excel cell data to an array list.我需要将 excel 单元格数据存储到数组列表中。 I've managed to get the cell data as 2 strings (data of two columns).我设法将单元格数据作为 2 个字符串(两列的数据)获取。 Now I need to store the string data to array list (or two).现在我需要将字符串数据存储到数组列表(或两个)。 My code is as follows:我的代码如下:

XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sh = wb.getSheetAt(0);

int  i;
for (i=0; i <= sh.getLastRowNum(); i++)

    {

XSSFRow r = sh.getRow(i);

//Data from first column.
String col1 = r.getCell(0).getStringCellValue();

//Data from second column.
double col2 = r.getCell(1).getNumericCellValue();
}

Now I need to store them ( col1 and col2 ) into array list.现在我需要将它们( col1col2 )存储到数组列表中。 How can this be done?如何才能做到这一点? I need to use this array to write the data into seperate sheet later.稍后我需要使用此数组将数据写入单独的工作表。

Thanks for your help.谢谢你的帮助。

You can use List data = new ArrayList() , but when you'll be trying to get data from this list you'll need to cast each object to String or double which can be unsafe, if you'll be inserting new values you'll get a warning about raw type.您可以使用List data = new ArrayList() ,但是当您尝试从此列表中获取数据时,您需要将每个对象强制转换为Stringdouble这可能是不安全的,如果您要插入新值将收到有关原始类型的警告。 In your case maybe consider using Map<String, Double> , it'll be more safe and natural.在您的情况下,可以考虑使用Map<String, Double> ,它会更安全和自然。

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

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