【问题标题】:Excel Apache POI is printing only last element while performing setCellValue()Excel Apache POI 在执行 setCellValue() 时仅打印最后一个元素
【发布时间】:2018-12-07 22:07:59
【问题描述】:

我正在从 excel 中读取一些数据并以列的形式将其写入同一文件中的另一张表,并且我希望将列打印在行中。当我执行迭代时 createRow().setCellValue() 只打印最后一个元素。

package com.editDistance;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadColumnsEditDistance {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub

		File src = new File("C:\\Users\\xyz\\Desktop\\folder\\file.xlsx");
		FileInputStream file = new FileInputStream(src);
		Workbook workbook = new XSSFWorkbook(file);
		Sheet sheet1 = workbook.getSheetAt(0);
		int rows = sheet1.getPhysicalNumberOfRows();
		workbook.createSheet();
		Sheet sheet2 = workbook.getSheetAt(1);
		for (int Readingrowindex = 1; Readingrowindex < rows; Readingrowindex++) {

			String ah = sheet1.getRow(Readingrowindex).getCell(1).getStringCellValue();
			sheet2.createRow(Readingrowindex).createCell(0).setCellValue(ah);
			sheet2.createRow(0).createCell(Readingrowindex).setCellValue(ah);

		}

		FileOutputStream fout = new FileOutputStream(src);
		workbook.write(fout);
	}
}

【问题讨论】:

    标签: java excel eclipse


    【解决方案1】:

    您想在第一列和第一行创建具有相同单元格的工作表,例如命名表吗?如果是,你需要修改你的代码,因为你总是重写第一行,这就是为什么你只得到最后一项。这样做:

        File src = new File("C:\\Users\\xyz\\Desktop\\folder\\file.xlsx");
        FileInputStream file = new FileInputStream(src);
        Workbook workbook = new XSSFWorkbook(file);
        Sheet sheet1 = workbook.getSheetAt(0);
        int rows = sheet1.getPhysicalNumberOfRows();
        workbook.createSheet();
        Sheet sheet2 = workbook.getSheetAt(1);
        Row sheet2FirstRow = sheet2.createRow(0);
        for (int Readingrowindex = 1; Readingrowindex < rows; Readingrowindex++) {
    
            String ah = sheet1.getRow(Readingrowindex).getCell(1).getStringCellValue();
            sheet2.createRow(Readingrowindex).createCell(0).setCellValue(ah);
            Cell cell = sheet2FirstRow.createCell(Readingrowindex);
            cell.setCellValue((String) ah);
    
        }
    
        FileOutputStream fout = new FileOutputStream(src);
        workbook.write(fout);
    

    【讨论】:

      【解决方案2】:

      我认为这是因为您一遍又一遍地创建第 0 行,所以它会覆盖您已经创建的内容。

      你可能想做这样的事情:

      if(sheet2.getRow() == null){
          sheet2.createRow(0).createCell(Readingrowindex).setCellValue(ah);
      }else{
          sheet2.getRow(0).createCell(Readingrowindex).setCellValue(ah);
      }
      

      【讨论】:

      • 应该在循环本身然后如何避免
      • getRow() 不能为空
      【解决方案3】:

      我在写入代码时遇到了类似的问题(它只打印最后一行)。

      这是我的代码的解决方案:

      失败的解决方案(仅打印最后一个单元格)即“Hello3”

                          sheet.createRow(8).createCell(0).setCellValue("Hello1");
                          sheet.createRow(8).createCell(1).setCellValue("Hello2");
                          sheet.createRow(8).createCell(2).setCellValue("Hello3");
      

      工作解决方案(打印所有数据):

              XSSFRow row;
              row = sheet.createRow(8);
              row.createCell(0).setCellValue("India 123");
              row.createCell(1).setCellValue("India 234");
              row.createCell(2).setCellValue("India 3453");
      
              row = sheet.createRow(9);
              row.createCell(0).setCellValue("Test 123");
              row.createCell(1).setCellValue("Test 234");
              row.createCell(2).setCellValue("Test 3453");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多