我有一个看起来像这样的 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 作为字符串读取,以便我可以迭代它。对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 文档了解更多信息。