在Excel中将单元格拆分为string – Apache POI JAVA

我有一个像这样的excel文件。 寻找一种方法将col1细胞分成不同的部分(物品中的物品)。

col1 col2 col3 col4 ----------------------------- row1 | 2,3,1 _ 1 w row2 | 3,2,7 _ 2 x row3 | _ _ 3 y row4 | 4,9 _ 4 z 

码:

 import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.*; public class Expy { public static void main(String[] args) { try { FileInputStream file = new FileInputStream(new File("C:\\Users\\...\\Bioactives25s.xlsx")); XSSFWorkbook workbook = new XSSFWorkbook(file); XSSFSheet worksheet = workbook.getSheetAt(0); for (int rowIndex = 0; rowIndex<50000; rowIndex++){ for (int columnIndex = 0; columnIndex<30; columnIndex++){ XSSFCell cell = worksheet.getRow(rowIndex).getCell(0); XSSFCell COL1 = worksheet.getRow(rowIndex).getCell(2); XSSFCell COL2 = worksheet.getRow(rowIndex).getCell(3); //important region String data = ??? ; String[] items = cell.split(","); //for (String item : items) if(cell != null){ continue;} if(cell == COL1){ System.out.print("it worked!!!"); } if(cell == null){ continue; } 

我不知道要在问号区域放什么。 我试图把“文件”,我试过“工作表”,不知道我应该写什么使excel列1读取为一个string,所以我可以迭代它。 对Java很新。

相关: http : //kodejava.org/how-do-i-split-a-string/

使用XSSFCell.getStringCellValue()

 XSSFCell cell = worksheet.getRow(rowIndex).getCell(0); String contents = cell.getStringCellValue(); String[] items = contents.split(","); for (String item : items) { System.out.println("Found csv item: " + item); } 

阅读Apache POI文档以获取更多信息。