Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java设置所有类型的Excel单元格值_Java_Excel - Fatal编程技术网

使用Java设置所有类型的Excel单元格值

使用Java设置所有类型的Excel单元格值,java,excel,Java,Excel,我需要将Excel行从一个工作表复制到另一个工作表。但是在复制而不是实际值之后,我得到了不同的值。具有字符串值的单元格没有问题,但是具有数值的单元格没有问题。Excel单元格可以包含任何类型的值。我的代码应该接受所有这些,并将它们复制到另一个工作表中。请帮我解决这个问题 if(row.getCell(getLastCell)!=null && ((row.getCell(getLastCell).toString().equalsIgnoreCase(FAIL))|| (row.

我需要将Excel行从一个工作表复制到另一个工作表。但是在复制而不是实际值之后,我得到了不同的值。具有字符串值的单元格没有问题,但是具有数值的单元格没有问题。Excel单元格可以包含任何类型的值。我的代码应该接受所有这些,并将它们复制到另一个工作表中。请帮我解决这个问题

if(row.getCell(getLastCell)!=null && ((row.getCell(getLastCell).toString().equalsIgnoreCase(FAIL))|| (row.getCell(getLastCell).toString().equalsIgnoreCase("Invalid CR Statement"))))
            {
                failuresRow=failuresSheet.createRow(createNewRow++);
                for(int ii=0;ii<=getLastCell;ii++)
                {
                    failuresCell=failuresRow.createCell(ii);
                    failuresCell.setCellValue(row.getCell(ii)+"");
                    failuresCell.setCellType(row.getCell(ii).getCellType());
                    failuresCell.setCellStyle(row.getCell(ii).getCellStyle());
             }

            }

如果单元格的数值为9200278,则输出为1717。我不明白我哪里做错了。在这种情况下,请帮助我。

这是因为您得到的是单元格而不是值。您可以使用以下方法代替
row.getCell(int).toString()

if(row.getCell(getLastCell)!=null && ((row.getCell(getLastCell).toString().equalsIgnoreCase(FAIL))|| (row.getCell(getLastCell).toString().equalsIgnoreCase("Invalid CR Statement"))))
            {
                failuresRow=failuresSheet.createRow(createNewRow++);
                for(int ii=0;ii<=getLastCell;ii++)
                {
                    failuresCell=failuresRow.createCell(ii);
                    failuresCell.setCellValue(row.getCell(ii)+"");
                    failuresCell.setCellType(row.getCell(ii).getCellType());
                    failuresCell.setCellStyle(row.getCell(ii).getCellStyle());
             }

            }
在调用该方法之前,只需检查单元格的类型。下面是一个例子:

if (cellType == HSSFCell.CELL_TYPE_BOOLEAN) {
    row.getCell(int).getBooleanCellValue();
} else if (cellType == HSSFCell.CELL_TYPE_ERROR) {
    row.getCell(int).getErrorCellValue();
} else if (cellType == HSSFCell.CELL_TYPE_FORMULA) {
    row.getCell(int).getCellFormula();
} else if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
    row.getCell(int).getNumericCellValue();
} else if (cellType == HSSFCell.CELL_TYPE_STRING) {
    row.getCell(int).getStringCellValue();
}

我只是事先把它们都调好了

try {
        FileInputStream fileInputStream = new FileInputStream(excelTemplate);
        HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
        for (int i=0; i < workbook.getNumberOfSheets(); i++) {
            for (Row row : workbook.getSheetAt(i)) {
                for (Cell cell : row) {
                    if (cell != null)
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                }
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
试试看{
FileInputStream FileInputStream=新的FileInputStream(excelTemplate);
HSSF工作簿=新的HSSF工作簿(fileInputStream);
对于(int i=0;i
您使用的是apache poi库吗?put failuresCell.setCellType(row.getCell(ii).getCellType());failuresCell.setCellStyle(row.getCell(ii).getCellStyle());故障前cell.setCellValue(row.getCell(ii)+”);伟大的您建议的代码在我的应用程序中工作。非常感谢你!!
try {
        FileInputStream fileInputStream = new FileInputStream(excelTemplate);
        HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
        for (int i=0; i < workbook.getNumberOfSheets(); i++) {
            for (Row row : workbook.getSheetAt(i)) {
                for (Cell cell : row) {
                    if (cell != null)
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                }
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }