Reputation: 121
Can someone help with this code?
I receive an error message about NullPointerException at 41 row. I know that it returns null but this value is required for following verification. The general task is to write result to excel file.
public class ExcelUtils {
public static void main(String[] args)
{
//Blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();
//Create a blank sheet
XSSFSheet sheet = workbook.createSheet("Employee Data");
String Result = "TEST TEST TEST";
int RowNum = 2;
int ColNum = 3;
XSSFSheet ExcelWSheet = sheet;
XSSFCell Cell;
XSSFRow Row;
try
{
Row = ExcelWSheet.getRow(RowNum);
Cell = Row.getCell(ColNum, org.apache.poi.ss.usermodel.Row.RETURN_BLANK_AS_NULL);
if (Cell == null) {
Cell = Row.createCell(ColNum);
Cell.setCellValue(Result);
} else {
Cell.setCellValue(Result);
}
//---------------------------------------------------
//try
// {
//Write the workbook in file system
FileOutputStream out = new FileOutputStream(new File("howtodoinjava_demo.xlsx"));
workbook.write(out);
out.close();
System.out.println("howtodoinjava_demo.xlsx written successfully on disk.");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Upvotes: 1
Views: 8409
Reputation: 121
static String sheetname = "TEST";
static XSSFWorkbook workbook = new XSSFWorkbook();
static XSSFSheet sheet = workbook.createSheet(sheetname);
public static void writeexcel(String Result,int RowNum ,int ColNum)
{
//Create a blank sheet
XSSFSheet ExcelWSheet = sheet;
XSSFCell Cell;
XSSFRow Row;
try
{
Row = ExcelWSheet.createRow(RowNum);
Cell = Row.getCell(ColNum, org.apache.poi.ss.usermodel.Row.RETURN_BLANK_AS_NULL);
if (Cell == null) {
Cell = Row.createCell(ColNum);
Cell.setCellValue(Result);
} else {
Cell.setCellValue(Result);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void writetoexcel(String namefile) throws IOException{
String namefile1 = namefile+".xlsx";
if(namefile1.equals(namefile1)){
try {
// Open the Excel file
FileInputStream ExcelFile = new FileInputStream(namefile1);
// Access the required test data sheet
workbook = new XSSFWorkbook(ExcelFile);
sheet = workbook.getSheet(sheetname);
System.out.println("This file is already exist. The system is opened this file for adding information");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
try{
FileOutputStream out = new FileOutputStream(new File(namefile1));
workbook.write(out);
out.close();
System.out.println("howtodoinjava_demo.xlsx written successfully on disk.");
}catch (Exception e)
{
e.printStackTrace();
}
}
}
}
Upvotes: 0
Reputation: 3021
pass the Result string from your test method to the method writeexcel.You don't need to create an object as the method is static
writeexcel("pass",2,3);
This will call the writeexcel method and will write the string passed as an argument ie) "pass" in the respective cell
Method :
public static void writeexcel(String Result,int RowNum ,int ColNum)
{
//Blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();
//Create a blank sheet
XSSFSheet sheet = workbook.createSheet("Employee Data");
XSSFSheet ExcelWSheet = sheet;
XSSFCell Cell;
XSSFRow Row;
try
{
Row = ExcelWSheet.createRow(RowNum);
Cell = Row.getCell(ColNum, org.apache.poi.ss.usermodel.Row.RETURN_BLANK_AS_NULL);
if (Cell == null) {
Cell = Row.createCell(ColNum);
Cell.setCellValue(Result);
} else {
Cell.setCellValue(Result);
}
FileOutputStream out = new FileOutputStream(new File("howtodoinjava_demo.xlsx"));
workbook.write(out);
out.close();
System.out.println("howtodoinjava_demo.xlsx written successfully on disk.");
}
catch (Exception e)
{
e.printStackTrace();
}
}
EDIT
public class Tests {
static XSSFWorkbook workbook = new XSSFWorkbook();
static XSSFSheet sheet = workbook.createSheet("Employee Data");
public static void main(String[] args) {
int j=3;
for(int i=2;i<=5;i++) {
String result = "Test "+i;
writeexcel(result, i, j);
}
writetoexcel();//write to cell(2,3),(3,3),(4,3)
}
public static void writeexcel(String Result,int RowNum ,int ColNum)
{
//Create a blank sheet
XSSFSheet ExcelWSheet = sheet;
XSSFCell Cell;
XSSFRow Row;
try
{
Row = ExcelWSheet.createRow(RowNum);
Cell = Row.getCell(ColNum, org.apache.poi.ss.usermodel.Row.RETURN_BLANK_AS_NULL);
if (Cell == null) {
Cell = Row.createCell(ColNum);
Cell.setCellValue(Result);
} else {
Cell.setCellValue(Result);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void writetoexcel(){
try{
FileOutputStream out = new FileOutputStream(new File("howtodoinjava_demo.xlsx"));
workbook.write(out);
out.close();
System.out.println("howtodoinjava_demo.xlsx written successfully on disk.");
}catch (Exception e)
{
e.printStackTrace();
}
}
}
Hope this helps you...Kindly get back if you need any further help
Upvotes: 1