使用Apache POI编写Excel工作表

时间:2021-11-07 15:47:52
package com.testCases;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class PoiWriteExcelFile {

    public static void main(String[] args) throws IOException {
            FileOutputStream fileOut = new FileOutputStream(
                    "D:\\User\\ExecutionResults.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet worksheet = workbook.getSheet("Sheet1"); 
            for (int i = 0; i <= 5; i++) {
                Cell cell=null;
                cell=worksheet.getRow(i).getCell(0);
                cell.setCellValue("Keyword" +i);
                cell=worksheet.getRow(i).getCell(1);
                cell.setCellValue("PASS" +i);
                workbook.write(fileOut);
            }
        } 

}

Throws exception. what goes wrong here...

引发异常。这里出了什么问题......

Exception in thread "main" java.lang.NullPointerException at com.testCases.PoiWriteExcelFile.main(PoiWriteExcelFile.java:30)

com.testCases.PoiWriteExcelFile.main(PoiWriteExcelFile.java:30)中线程“main”java.lang.NullPointerException中的异常

2 个解决方案

#1


This is working for me. Can you give it a try?

这对我有用。你能尝试一下吗?

`import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class Test {

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

        String filePath = "D:\\xecutionResults.xlsx";
        Workbook workbook;
        FileInputStream fis;
        FileOutputStream fos;
        try {
            fis = new FileInputStream(filePath);

            workbook = WorkbookFactory.create(fis);

            org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(0);

            for (int i = 0; i <= 5; i++) {

                Row row = sheet.getRow(i);

                if(row == null)
                    row = sheet.createRow(i);

                Cell cell=row.getCell(0);

                if(cell == null)
                    cell = row.createCell(0, Cell.CELL_TYPE_STRING);

                cell.setCellValue("Keyword" +i);

                cell=row.getCell(1);

                if(cell == null)
                    cell = row.createCell(1, Cell.CELL_TYPE_STRING);
                cell.setCellValue("PASS" +i);

                System.out.println("1");
            }
            fis.close();
            fos = new FileOutputStream(filePath);
            workbook.write(fos);
            fos.close();
        } catch (InvalidFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            //close out/in streams
        }

    } 

}`

使用Apache POI编写Excel工作表

#2


here the code :

这里的代码:

public void WriteStudentData() {

    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("Sheet1");
    int excel_length=10;
    for(i=0;i<excel_length;i++){
    Row row1 = sheet.createRow(i);
    //setting cells values
    for (int m = 0; m < 2; m++) {
    Cell cell = row.createCell(m);
    if (m == 0) {
    cell.setCellValue("Keyword"+i);
                  }
    if (m == 1) {
       cell.setCellValue("PASS"+i);
         }
  }
  }
 //creating excel file
  FileOutputStream fileOut;
  try {
   fileOut = new FileOutputStream("E:\\Student.xlsx");
   workbook.write(fileOut);
   fileOut.close();
   } catch (FileNotFoundException e) {
     e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }


   }

#1


This is working for me. Can you give it a try?

这对我有用。你能尝试一下吗?

`import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class Test {

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

        String filePath = "D:\\xecutionResults.xlsx";
        Workbook workbook;
        FileInputStream fis;
        FileOutputStream fos;
        try {
            fis = new FileInputStream(filePath);

            workbook = WorkbookFactory.create(fis);

            org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(0);

            for (int i = 0; i <= 5; i++) {

                Row row = sheet.getRow(i);

                if(row == null)
                    row = sheet.createRow(i);

                Cell cell=row.getCell(0);

                if(cell == null)
                    cell = row.createCell(0, Cell.CELL_TYPE_STRING);

                cell.setCellValue("Keyword" +i);

                cell=row.getCell(1);

                if(cell == null)
                    cell = row.createCell(1, Cell.CELL_TYPE_STRING);
                cell.setCellValue("PASS" +i);

                System.out.println("1");
            }
            fis.close();
            fos = new FileOutputStream(filePath);
            workbook.write(fos);
            fos.close();
        } catch (InvalidFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            //close out/in streams
        }

    } 

}`

使用Apache POI编写Excel工作表

#2


here the code :

这里的代码:

public void WriteStudentData() {

    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("Sheet1");
    int excel_length=10;
    for(i=0;i<excel_length;i++){
    Row row1 = sheet.createRow(i);
    //setting cells values
    for (int m = 0; m < 2; m++) {
    Cell cell = row.createCell(m);
    if (m == 0) {
    cell.setCellValue("Keyword"+i);
                  }
    if (m == 1) {
       cell.setCellValue("PASS"+i);
         }
  }
  }
 //creating excel file
  FileOutputStream fileOut;
  try {
   fileOut = new FileOutputStream("E:\\Student.xlsx");
   workbook.write(fileOut);
   fileOut.close();
   } catch (FileNotFoundException e) {
     e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }


   }