Reputation: 5095
I am working on a requirement to download excel file using Apache POI and spring MVC. I followed a tutorial and I could successfully complete the functionality. However every time I click download, the filename of the downloaded excel is the ending part of string in the request URL
eg: URL: http://localhost:8080/myproject/downloadExcel and the downloaded file will be with name download Excel.
I want to dynamically create a filename and the downloaded file should have that name instead of the above. Can anyone help me how to achieve the required functionality?
@Controller public class MainController {
/**
* Handle request to the default page
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String viewHome() {
return "home";
}
/**
* Handle request to download an Excel document
*/
@RequestMapping(value = "/downloadExcel", method = RequestMethod.GET)
public ModelAndView downloadExcel() {
// create some sample data
List<Book> listBooks = new ArrayList<Book>();
listBooks.add(new Book("Effective Java", "Joshua Bloch", "0321356683",
"May 28, 2008", 38.11F));
listBooks.add(new Book("Head First Java", "Kathy Sierra & Bert Bates",
"0596009208", "February 9, 2005", 30.80F));
listBooks.add(new Book("Java Generics and Collections",
"Philip Wadler", "0596527756", "Oct 24, 2006", 29.52F));
listBooks.add(new Book("Thinking in Java", "Bruce Eckel", "0596527756",
"February 20, 2006", 43.97F));
listBooks.add(new Book("Spring in Action", "Craig Walls", "1935182358",
"June 29, 2011", 31.98F));
// return a view which will be resolved by an excel view resolver
return new ModelAndView("excelView", "listBooks", listBooks);
}
}
public class ExcelBuilder extends AbstractExcelView {
@Override
protected void buildExcelDocument(Map<String, Object> model,
HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response)
throws Exception {
// get data model which is passed by the Spring container
List<Book> listBooks = (List<Book>) model.get("listBooks");
// create a new Excel sheet
HSSFSheet sheet = workbook.createSheet("Java Books");
sheet.setDefaultColumnWidth(30);
// create style for header cells
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontName("Arial");
style.setFillForegroundColor(HSSFColor.BLUE.index);
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setColor(HSSFColor.WHITE.index);
style.setFont(font);
// create header row
HSSFRow header = sheet.createRow(0);
header.createCell(0).setCellValue("Book Title");
header.getCell(0).setCellStyle(style);
header.createCell(1).setCellValue("Author");
header.getCell(1).setCellStyle(style);
header.createCell(2).setCellValue("ISBN");
header.getCell(2).setCellStyle(style);
header.createCell(3).setCellValue("Published Date");
header.getCell(3).setCellStyle(style);
header.createCell(4).setCellValue("Price");
header.getCell(4).setCellStyle(style);
// create data rows
int rowCount = 1;
for (Book aBook : listBooks) {
HSSFRow aRow = sheet.createRow(rowCount++);
aRow.createCell(0).setCellValue(aBook.getTitle());
aRow.createCell(1).setCellValue(aBook.getAuthor());
aRow.createCell(2).setCellValue(aBook.getIsbn());
aRow.createCell(3).setCellValue(aBook.getPublishedDate());
aRow.createCell(4).setCellValue(aBook.getPrice());
}
}
}
view configuration:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="excelView" class="net.codejava.spring.ExcelBuilder" />
</beans>
View Resolver
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="net.codejava.spring" />
<bean id="viewResolver1" class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="order" value="1"/>
<property name="location" value="/WEB-INF/views.xml"/>
</bean>
<bean id="viewResolver2"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2"/>
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Upvotes: 0
Views: 12530
Reputation: 13814
Pass the name of the file (you want to create) in the argument for the OutputStream you are writing the data to.
String excelFileName = "C:/Test.xls"; //name of excel file
OutputStream fileOut = new FileOutputStream(excelFileName);
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet(sheetName) ;
//Write your code for the content generation here.
//write this workbook to an Outputstream.
wb.write(fileOut);
fileOut.flush();
fileOut.close();
Since you are using servlet
for making the file downloadable, you should do the following:
String fileName = "MyFile.xls"; //Your file name here.
response.setContentType("application/vnd.ms-excel"); //Tell the browser to expect an excel file
response.setHeader("Content-Disposition", "attachment; filename="+fileName); //Tell the browser it should be named as the custom file name
HSSFWorkbook workbook = createExcel();
workbook.write(response.getOutputStream());
Content-Disposition
header will instruct the browser to use the filename
attribute as the file name for the download.
Upvotes: 3