Nov 10, 2018

Example: Download Excel File From Web (JAVA)

import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; fileName=yourFileName.xls");

ServletOutputStream out = response.getOutputStream();
StringBuilder writer = new StringBuilder();

//use .xls as extension in your file name.

// create a new workbook
HSSFWorkbook book = new HSSFWorkbook();

// create a bold font
HSSFFont bold = book.createFont();
bold.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

// create a blue font
HSSFFont blue = book.createFont();
blue.setColor(HSSFColor.BLUE.index);
// row data

HSSFRow row;
HSSFRichTextString name;
HSSFRichTextString value;

// create a new worksheet
HSSFSheet summary = book.createSheet("Summary");
summary.setColumnWidth(0, 7000);
summary.setColumnWidth(1, 20000);

row = summary.createRow(0);
name = new HSSFRichTextString("Subject:");
value = new HSSFRichTextString("your subject");
name.applyFont(bold);
value.applyFont(blue);
row.createCell(0).setCellValue(name);
row.createCell(1).setCellValue(value);

row = summary.createRow(1);
name = new HSSFRichTextString("second row:");
value = new HSSFRichTextString("your content");
name.applyFont(bold);
value.applyFont(blue);
row.createCell(0).setCellValue(name);
row.createCell(1).setCellValue(value);
// get the output stream
OutputStream out = response.getOutputStream();
// write the work book to the output stream
book.write(out);

// close the output stream
out.flush();
out.close();