Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

In Struts2 unable to export the data to excel

 
Anjanaya swamy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

i am trying to export the data into excel from struts 2. In my case excel is opening but no data is getting interested into the excel (blank excel sheet).

The code is struts.xml is as follows :

<action name="downloadReserveNumbersPri" class="com.wipro.vel.nmp.web.NMP.DownloadReserveAction">
<result name="success" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment; filename="excelExport.xls"</param>
<param name="bufferSize">4096</param>
</result>
</action>

The code in the action class is as follows :

private InputStream inputStream;


public void exportExcel() throws Exception {

Map session = ActionContext.getContext().getSession();
FileOutputStream file = null;
file = new FileOutputStream("poi-test.xls");

List<SearchNumberDTO> outputCityAndRangeList=(List<SearchNumberDTO>)session.get("outputCityAndRangeList");
System.out.println("numberTypes--------------->"+outputCityAndRangeList.size());

HSSFWorkbook workbook = createWorkbook(outputCityAndRangeList);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.write(file);
workbook.write(baos);
ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
inputStream = bis;


}


public HSSFWorkbook createWorkbook(List<SearchNumberDTO> totalSearchNumberDTO) throws Exception {

HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Reservation Details ");

HSSFCellStyle headerCellStyle = wb.createCellStyle();
HSSFFont boldFont = wb.createFont();
boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headerCellStyle.setFont(boldFont);

HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellStyle(headerCellStyle);
cell.setCellValue(new HSSFRichTextString("Reservation Id"));
cell = row.createCell(1);
cell.setCellStyle(headerCellStyle);
cell.setCellValue(new HSSFRichTextString("Customer Name"));
cell = row.createCell(2);
cell.setCellStyle(headerCellStyle);
cell.setCellValue(new HSSFRichTextString("STD Code"));

for(int index=0;index < totalSearchNumberDTO.size(); index++){
row = sheet.createRow(index);
cell = row.createCell(0);
SearchNumberDTO searchNumberDTO = (SearchNumberDTO) totalSearchNumberDTO.get(index);
HSSFRichTextString reservationId = new HSSFRichTextString(searchNumberDTO.getReservationId());
cell.setCellValue(reservationId);
cell = row.createCell(1);
HSSFRichTextString customerName = new HSSFRichTextString(searchNumberDTO.getCustomerName());
cell.setCellValue(customerName);
cell = row.createCell(2);
HSSFRichTextString stdCode = new HSSFRichTextString(searchNumberDTO.getStdCode());
cell.setCellValue(stdCode);
cell = row.createCell(3);

}

return wb;
}


public InputStream getInputStream() {
return inputStream;
}


public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}


Please someone help me .

Thanks in advance.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic