| Author |
In Struts2 unable to export the data to excel
|
Anjanaya swamy
Greenhorn
Joined: Jun 14, 2012
Posts: 7
|
|
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.
|
 |
 |
|
|
subject: In Struts2 unable to export the data to excel
|
|
|