Use AbstractJExcelView to generate excel file and send through MailSender in Spring Framework

StackOverflow https://stackoverflow.com/questions/21428831

  •  04-10-2022
  •  | 
  •  

Question

Currently my project uses a Controller class and utilizes AbstractJExcelView Class to generate an Excel Report which gives a prompt to user to either open or save the excel file. Basicall it uses http protocol for calling the controller. Now what I need to do is generate the same excel file during (Spring) batch jobs and send it as an attachment directly through email. We're using org.springframework.mail.MailSender for sending mails. I'm not supposed to use any other external API and I have no clue where to start from.

This is a similar code of what we're doing :-

Controller

public class RevenueReportController extends AbstractController{

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
    HttpServletResponse response) throws Exception {

    String output =
        ServletRequestUtils.getStringParameter(request, "output");

    //dummy data
    Map<String,String> revenueData = new HashMap<String,String>();
    revenueData.put("Jan-2010", "$100,000,000");
    revenueData.put("Feb-2010", "$110,000,000");
    revenueData.put("Mar-2010", "$130,000,000");
    revenueData.put("Apr-2010", "$140,000,000");
    revenueData.put("May-2010", "$200,000,000");

    if(output ==null || "".equals(output)){
        //return normal view
        return new ModelAndView("RevenueSummary","revenueData",revenueData);

    }else if("EXCEL".equals(output.toUpperCase())){
        //return excel view
        return new ModelAndView("ExcelRevenueSummary","revenueData",revenueData);

    }else{
        //return normal view
        return new ModelAndView("RevenueSummary","revenueData",revenueData);

    }   
}

View

public class ExcelRevenueReportView extends AbstractJExcelView{

@Override
protected void buildExcelDocument(Map model, WritableWorkbook workbook,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {

   Map<String,String> revenueData = (Map<String,String>) model.get("revenueData");
   WritableSheet sheet = workbook.createSheet("Revenue Report", 0);

       sheet.addCell(new Label(0, 0, "Month"));
       sheet.addCell(new Label(1, 0, "Revenue"));

       int rowNum = 1;
   for (Map.Entry<String, String> entry : revenueData.entrySet()) {
    //create the row data
    sheet.addCell(new Label(0, rowNum, entry.getKey()));
        sheet.addCell(new Label(1, rowNum, entry.getValue()));
        rowNum++;
       }
   }

}

Is there any way to do that ? I need to generate the excel sheet with passed and failed job data. I can handle populating and creating the file but I have no idea how to generate the excel in buffer or may be in some temp directory which could again be attached while sending the mail. The user is not required to respond to any prompts. Neither are we going to use any http request.

Thanks in advance!

Was it helpful?

Solution 2

I did the following in and it served the purpose :-

private ByteArrayOutputStream outputStream;
outputStream = new ByteArrayOutputStream();
WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
.
.
.
.       
workbook.write();
workbook.close();

Now, a getter to fetch the value :-

public ByteArrayOutputStream getOutputStream()
{
  return this.outputStream;
}

And finally, for email attachement :-

JavaMailSenderImpl sender = new JavaMailSenderImpl();
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);     
helper.setTo(destiny);
helper.setSubject(subject);
helper.setText(msg);
helper.setFrom(from);
.
.
.
FileSystemResource fsr = new FileSystemResource(file);
helper.addAttachment("Sample.xls", fsr);

Thanks for the help guyz!

OTHER TIPS

There is an MimeMessageItemWriter in Spring Batch that you should be able to use to send the emails. You'll need to create an ItemProcessor that creates the MimeMessage. You can either create the Excel file and add it as an attachment within that ItemProcessor, or have one step generate all the Excel files that need to be sent, then in the next step, you can send them. I have an example of sending emails from Spring Batch in my book: Pro Spring Batch

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top