PDA

View Full Version : Howto export excel after clicking a button?



yugho
1 Feb 2010, 10:45 PM
Dear all,

I need help to export excel after clicking a button. I don't know what script I still miss...
When I click on the button, it returns System Error: -1072896748.
Thanks a lot.

This is my javascript:


btnSaveMf.addListener('click',function(){
Ext.Ajax.request({
url:'test.do?action=exportExcel'
});
});
This is my java code:


public void exportExcel(HttpServletRequest request,
HttpServletResponse response) throws IOException {
response.setContentType("application/vnd.ms-excel");

//Create Excel
String[][] str = new String[1][1];
str[0][0] = "Employee Name";

HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Java Class Info");
for (int i = 0; i < str.length; i++) {
HSSFRow row = sheet.createRow((short) i);
for (int j = 0; j < str[i].length; j++) {
HSSFCell cell = row.createCell(j);
cell.setCellValue(str[i][j]);
}
}

//Output Excel
BufferedOutputStream fileOut = new BufferedOutputStream(response
.getOutputStream());
response.setHeader("Content-disposition",
"attachment; filename=filename.xls");
response.setHeader("Content-Type","application/force-download");

workbook.write(fileOut);
fileOut.flush();
fileOut.close();
}

Animal
2 Feb 2010, 12:10 AM
You can't download with Ajax.

You have to use a LinkButton

jjulian
2 Feb 2010, 6:53 AM
Alternately, you can open the spreadsheet in a new browser window:



btnSaveMf.addListener('click',function(){
window.open('test.do?action=exportExcel');
});


Depending on the browser settings, this will either prompt the user to download or display the spreadsheet directly.