简体   繁体   English

Java仅打印数据的最后一个单元格

[英]Java prints only last cell of data

I am trying to read content from a csv and print it out to an excel file. 我正在尝试从csv中读取内容并将其打印到excel文件中。 So far, I have this, however it only prints out the last value of the csv into the first cell of the excel file. 到目前为止,我有这个,但它只将csv的最后一个值打印到excel文件的第一个单元格中。 I tried using an increment for the column and row, but I get a null pointer exception error. 我尝试使用列和行的增量,但我得到一个空指针异常错误。

int i=0;
int j=0;
int k=0;
int row=68;
int column=0;
String [] part={"052","064"};
try{
    //InputStream inp = new FileInputStream();
    //Workbook wb = WorkbookFactory.create(inp);
    Workbook wb = WorkbookFactory.create(new File("30_results_w_graphs.xls"));


    for (k=0; k<part.length; k++)
    {
        Sheet sheet = wb.getSheet(part[k]);
            Cell cell = null;
        Scanner scanner = new Scanner(new File(part[k]+".csv"));
        scanner.useDelimiter(",");


        while(scanner.hasNext()){
            cell=sheet.getRow(row).getCell(column);
            cell.setCellValue(scanner.next());
            //row++;
            //column++;
            }
        FileOutputStream fileOut = new FileOutputStream("30_results_U_w_graphs.xls");
        wb.write(fileOut);
        scanner.close();
    }

}

I believe it is a nested for loop to increment the row and the column, but even in regular increments i get a null pointer exception. 我相信它是一个嵌套的for循环来增加行和列,但即使以常规增量,我得到一个空指针异常。

I don't know what exactly you want to do, but this code shows some basic steps in reading a CSV file into an existing Excel file using Apache POI. 我不知道您想要做什么,但此代码显示了使用Apache POI将CSV文件读取到现有Excel文件中的一些基本步骤。 Existing sheets/rows/cells will not be deleted. 现有的工作表/行/单元格不会被删除。

The CSV file looks like this: CSV文件如下所示:

12,14,18,21
82,19,18,44
9,4,31,12

Read the file into a sheet named after the file name: 将文件读入以文件名命名的工作表:

    File excel = new File("/tmp/result.xls");

    //Open existing Excel file:
    Workbook wb = WorkbookFactory.create(excel);

    //Create a new Excel file:
    //Workbook wb = new HSSFWorkbook();

    //Read in CSV
    String name = "test";
    Sheet sheet = wb.getSheet(name);
    if (sheet == null) {
        sheet = wb.createSheet(name);
    }

    int rowCount = 0;
    Scanner scanner = new Scanner(new File("/tmp/" + name + ".csv"));
    while (scanner.hasNextLine()) {
        String[] rowData = scanner.nextLine().split(",");
        for (int col = 0; col < rowData.length; col++) {
            Row row = sheet.getRow(rowCount);
            if (row == null)
                row = sheet.createRow(rowCount);
            Cell cell = row.getCell(col);
            if (cell == null) {
                cell = row.createCell(col);
            }
            cell.setCellValue(rowData[col]);
        }
        rowCount++;
    }

    wb.write(new FileOutputStream(excel));
}

You don't increase row nor column . 您不会增加rowcolumn Use createRow(int row) and createCell(int column) tor creating new cells. 使用createRow(int row)createCell(int column)创建新单元格。

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

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