Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.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_Nullpointerexception_Hssf - Fatal编程技术网

Java-无法在excel单元格中写入

Java-无法在excel单元格中写入,java,excel,nullpointerexception,hssf,Java,Excel,Nullpointerexception,Hssf,我试图编写现有的excel文件,但它在注释区域抛出了java.lang.NullPointerException。非常感谢您的任何建议或意见 在代码中,此行HSSFRow row=sheet.createRow((短)0)仅在位置0处创建新行。除此之外的任何内容仍然是null,因此,当您尝试对其调用任何方法时,都将抛出NPE 要能够写入行中的单元格,需要首先在特定位置创建一行 FileInputStream file = new FileInputStream(new File("//Users

我试图编写现有的excel文件,但它在注释区域抛出了
java.lang.NullPointerException
。非常感谢您的任何建议或意见

在代码中,此行
HSSFRow row=sheet.createRow((短)0)仅在位置
0
处创建新行。除此之外的任何内容仍然是
null
,因此,当您尝试对其调用任何方法时,都将抛出NPE

要能够写入行中的单元格,需要首先在特定位置创建一行

FileInputStream file = new FileInputStream(new File("//Users//"+ usr +"//Desktop//TNA//output//output.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheet("Sheet1");
Cell name_c = null;
Cell department_c = null;
Cell prev_depart_c = null;
HSSFRow row = sheet.createRow((short) 0);
HSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(HSSFColor.LIGHT_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
department_c = sheet.getRow(2).getCell(1); // throws exception here
department_c.setCellValue(department);
prev_depart_c = sheet.getRow(3).getCell(1);
prev_depart_c.setCellValue(prev_depart);
emp_no_c = sheet.getRow(4).getCell(1);
emp_no_c.setCellValue(emp_no);
file.close();
FileOutputStream outFile =new FileOutputStream(new File("//Users//"+ usr +"//Desktop//TNA//output//output10.xls"));
workbook.write(outFile);
outFile.close();

在代码中,此行
HSSFRow row=sheet.createRow((短)0)仅在位置
0
处创建新行。除此之外的任何内容仍然是
null
,因此,当您尝试对其调用任何方法时,都将抛出NPE

要能够写入行中的单元格,需要首先在特定位置创建一行

FileInputStream file = new FileInputStream(new File("//Users//"+ usr +"//Desktop//TNA//output//output.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheet("Sheet1");
Cell name_c = null;
Cell department_c = null;
Cell prev_depart_c = null;
HSSFRow row = sheet.createRow((short) 0);
HSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(HSSFColor.LIGHT_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
department_c = sheet.getRow(2).getCell(1); // throws exception here
department_c.setCellValue(department);
prev_depart_c = sheet.getRow(3).getCell(1);
prev_depart_c.setCellValue(prev_depart);
emp_no_c = sheet.getRow(4).getCell(1);
emp_no_c.setCellValue(emp_no);
file.close();
FileOutputStream outFile =new FileOutputStream(new File("//Users//"+ usr +"//Desktop//TNA//output//output10.xls"));
workbook.write(outFile);
outFile.close();

如果工作表中不存在单元格,则需要创建它们:

HSSFRow row = sheet.createRow(2); // create a row at rownum 2
// use the created row and add/edit cells in it.

如果工作表中不存在单元格,则需要创建它们:

HSSFRow row = sheet.createRow(2); // create a row at rownum 2
// use the created row and add/edit cells in it.

顺便说一下,您在文件路径中使用了冗余斜杠

public class ExcelExample {

    public static void main(String[] args) throws IOException {
        FileInputStream file = new FileInputStream(new File("/output.xls"));
        HSSFWorkbook workbook = new HSSFWorkbook(file);
        HSSFSheet sheet = workbook.getSheet("Sheet1");

        Cell name_c = null;
        Cell department_c = null;
        Cell prev_depart_c = null;


        HSSFRow row = sheet.createRow((short) 0);
        HSSFCellStyle style = workbook.createCellStyle();
        style.setFillForegroundColor(HSSFColor.LIGHT_BLUE.index);
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

        HSSFRow row2 = sheet.createRow(2);
        HSSFCell cell = row2.createCell(1);
        cell.setCellValue("5");

        HSSFRow row3 = sheet.createRow(3);
        HSSFCell cell2 = row2.createCell(1);
        cell2.setCellValue("5");

        file.close();
        FileOutputStream outFile =new FileOutputStream(new File("/output10.xls"));
        workbook.write(outFile);
        outFile.close();
    }
}

一个斜杠就足够了。斜杠(/)不必像反斜杠那样在字符串中转义。

顺便说一句,您在文件路径中使用了冗余斜杠

public class ExcelExample {

    public static void main(String[] args) throws IOException {
        FileInputStream file = new FileInputStream(new File("/output.xls"));
        HSSFWorkbook workbook = new HSSFWorkbook(file);
        HSSFSheet sheet = workbook.getSheet("Sheet1");

        Cell name_c = null;
        Cell department_c = null;
        Cell prev_depart_c = null;


        HSSFRow row = sheet.createRow((short) 0);
        HSSFCellStyle style = workbook.createCellStyle();
        style.setFillForegroundColor(HSSFColor.LIGHT_BLUE.index);
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

        HSSFRow row2 = sheet.createRow(2);
        HSSFCell cell = row2.createCell(1);
        cell.setCellValue("5");

        HSSFRow row3 = sheet.createRow(3);
        HSSFCell cell2 = row2.createCell(1);
        cell2.setCellValue("5");

        file.close();
        FileOutputStream outFile =new FileOutputStream(new File("/output10.xls"));
        workbook.write(outFile);
        outFile.close();
    }
}

一个斜杠就足够了。斜杠(/)不必像反斜杠一样在字符串中转义。

这意味着
sheet.getRow(2)
为null(asuming
workbook.createCellStyle()
不返回null)。是的。我需要在其中写入值的特定单元格是空的…请检查您的工作表是否有三行,这意味着
工作表。getRow(2)
为空(asuming
workbook.createCellStyle()
不返回空值)。是的。那个特定的单元格是空的,我需要在那里写入值…检查您的工作表是否有三行