Apache poi and Service Task

Hello,

I want to generate a excel file in a service task. For this iam using the apache poi in my eclipse. If I execute the process in my Tomcat Server iam always become a the message “Internal Server Error”.
Maybe I have a Error in my Code, Im don`t know.
Here is my code and the log file:

catalina.2016-11-25.log (231.0 KB)

Code.txt (2.0 KB)

Hi @Timkne,

what is happening in at de.tim.speed.file.LoggerDelegate.execute(LoggerDelegate.java:40) ?

Cheers,
Askar

public class LoggerDelegate implements JavaDelegate {

 private final Logger LOGGER = Logger.getLogger(LoggerDelegate.class.getName());
@Override

public void execute(DelegateExecution execution) throws Exception {

  		// TODO Auto-generated method
  		
  		File src=new File("E:\\Arbeitsmappe1.xlsx");
  		FileInputStream fis=new FileInputStream(src);
  		
  		XSSFWorkbook wb=new XSSFWorkbook(fis);
  		
  		XSSFSheet sheet1=wb.getSheetAt(0);
  		
  		sheet1.getRow(2).createCell(3).setCellValue(99);
  		sheet1.getRow(3).createCell(3).setCellValue(299);
  		sheet1.getRow(4).createCell(3).setCellValue(399);
  		
  		FileOutputStream fout=new FileOutputStream(src);
  		
  		wb.write(fout);
  		
  		wb.close();
 	
  		LOGGER.info("\n\n  ... Camunda is Working "
  	            + "processDefinitionId=" + execution.getProcessDefinitionId()
  	            + ", activtyId=" + execution.getCurrentActivityId()
  	            + ", activtyName='" + execution.getCurrentActivityName() + "'"
  	            + ", processInstanceId=" + execution.getProcessInstanceId()
  	            + ", businessKey=" + execution.getProcessBusinessKey()
  	            + ", executionId=" + execution.getId()
  	            + " \n\n");

}

}

@Timkne, which line is number 40?

Sry… line Nb. 40 is between this two lines:

39 XSSFWorkbook wb=new XSSFWorkbook(fis);
40
41 XSSFSheet sheet1=wb.getSheetAt(0);

Actually, I would just write with a ServiceTask into an Excel file.
I thought the easiest way is with the apache poi.

If you can tell me or show how I do the best I would be very grateful to you.

Cheers

Check the POI docs.
You cannot get a sheet that does not exist. You should create it first.

wb.createSheet(“someSheetName”);

1 Like

Iam tested the code in a simple java application and it works. The Excel-file is exists already but I will test it.

If the file and sheet exist already you can ignore my suggestion. Most probably he cannot read or find the existing file in that case.

I have this simple java Application

> import java.io.File;
> import java.io.FileInputStream;
> import java.io.FileOutputStream;
> import java.util.Scanner;

> import org.apache.poi.xssf.usermodel.XSSFSheet;
> import org.apache.poi.xssf.usermodel.XSSFWorkbook;



> public class ExcelWriter {


> 	public static void main(String[] args) throws Exception {
> 		// TODO Auto-generated method stub
> 		
> 		double Wert=1;
> 		double Wert2=1;
> 		double Wert3=1;
> 		
> 		
> 		File src=new File("C:\\Users\\s-Knechtel.t\\Noten.xlsx");
> 		FileInputStream fis=new FileInputStream(src);
> 		
> 		XSSFWorkbook wb=new XSSFWorkbook(fis);
> 		
> 		XSSFSheet sheet1=wb.getSheetAt(0);
> 		
> 		sheet1.getRow(2).createCell(3).setCellValue(Wert);
> 		sheet1.getRow(3).createCell(3).setCellValue(Wert2);
> 		sheet1.getRow(4).createCell(3).setCellValue(Wert3);
> 		
> 		FileOutputStream fout=new FileOutputStream(src);
> 		
> 		wb.write(fout);
> 		
> 		wb.close();
> 		


> }
> 	}

and it work.

How can I add this in an Service Task… This is first a test, later I will write in a Excel Sheet with a Variables from a Task-Form.

try wrapping the logic in a File exist check to see if you still get the exception.
What is E: btw? A local drive or a mapped network drive?

File src=new File(“E:\Arbeitsmappe1.xlsx”);
if(src.exists()) {

// logic here

}

Yes “E” is a network drive.

I will check your Idea

SRC is OK. I get the same Error messages :sweat:

Network drive. Checking Google, I assume that is the cause.
Looks like you cannot simply use ‘E:\’.

You should do something like
\\server_alias\somesharedfolder\Arbeitsmappe1.xlsx
or
\\192.168.12.24\somesharedfolder\Arbeitsmappe1.xlsx

Have not tried it before.

I copy the file to my normal C-Drive and tested it.-same error.

Is this part right?

public class LoggerDelegate implements JavaDelegate {
 private final Logger LOGGER = Logger.getLogger(LoggerDelegate.class.getName());
@Override
public void execute(DelegateExecution execution) throws Exception

if I use this code here:

public class ExcelWriter {

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

  double Wert=1;
  double Wert2=1;
  double Wert3=1;
  
  
  File src=new File("C:\\Users\\s-Knechtel.t\\Noten.xlsx");
  FileInputStream fis=new FileInputStream(src);
  
  XSSFWorkbook wb=new XSSFWorkbook(fis);
  
  XSSFSheet sheet1=wb.getSheetAt(0);
  
  sheet1.getRow(2).createCell(3).setCellValue(Wert);
  sheet1.getRow(3).createCell(3).setCellValue(Wert2);
  sheet1.getRow(4).createCell(3).setCellValue(Wert3);
  
  FileOutputStream fout=new FileOutputStream(src);
  
  wb.write(fout);
  
  wb.close();

}
}

And if I use a local file?

Yes you are right, there is a problem with the path from my excel file, if I create a new excel file without path the process is working. The File is saved in the Camunda-bpm-tomcat7.5/server/apache-Tomcat-8.0.24/bin
So, Thank you very much.:slight_smile:

But I dont know how to set a local file like this:> File src=new File(“C:\Users\s-Knechtel.t\Noten.xlsx”);`

Thanks:+1:

Hi @Timkne
wondering if you managed to impelent a service task writing form data into an excel and if yes, how did you do it? Thanks

@DaniHorn see the details from the other thread you opened.