从CSV文件导入Excel转换

问题描述 投票:0回答:2

具有使用Apache POI API难度林。我试着去导入Excel他们只选择特定的行和细胞从进口中提取。即时通讯目前可以导入,但我不能提取某些细胞。这里是代码:

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;

import java.io.File;
import java.io.IOException;

public class ExcelReader {
    public static final String path = "C:/Users/xxxx/Documents/import testing.xlsx";

    public static void main(String[] args) throws IOException, InvalidFormatException {

        // Create a workbook with data from excel file
        Workbook workbook = WorkbookFactory.create(new File(path));

        // Save sheets from workbook
        Sheet sheet = workbook.getSheetAt(0);

        // Make sure the data is saved in string format using a data formatter
        DataFormatter dataFormatter = new DataFormatter();

        // Iterate through cells and columns, printing their content
        System.out.println("\n\nThe content of the excel file: " + path + "\n");
        String cellContent;
        for (Row row: sheet) {
            for(Cell cell: row) {
                cellContent = dataFormatter.formatCellValue(cell);
                if(cellContent == null || cellContent.trim().isEmpty()){
                    // Give the empty cells the content "empty", to make it easy to filter out later on
                    cellContent = "empty";
                }
                System.out.print(cellContent + "\t");
            }
            CellReference cellReference = new CellReference("A11");
            XSSFRow rowT = sheet.getRow(cellReference.getRow());
            if (rowT != null) {
                XSSFCell cell = rowT.getCell(cellReference.getCol());
            }
            System.out.println();

        }
        // Close the connection to the workbook
        workbook.close();
    }
}
java excel apache import apache-poi
2个回答
0
投票

更改工作簿XSSFWorkbook和图纸,以XSSFSheet似乎解决编制问题。

XSSFWorkbook workbook = new XSSFWorkbook(new File(path));

XSSFSheet sheet = workbook.getSheetAt(0);

0
投票

尝试用此为GET“A11”电池

XSSFCell cell = sheet.getRow(10).getCell(0); // 10 = id of the 11th row, 0 = id of the 1st (A) column

要么

XSSFCell cell = sheet.getRow(10).getCell(CellReference.convertColStringToIndex("A"));

创建用于B12细胞参考

CellReference cr = new CellReference("B12");
row = mySheet.getRow(cr.getRow());
cell = row.getCell(cr.getCol());
© www.soinside.com 2019 - 2024. All rights reserved.