How to make an excel file available for download

I created an excel report and want the file to be uploaded by the user, so how to set response properties (content type).

+3
source share
3 answers

You need to set the headers and content type as follows:

    response.setHeader "Content-disposition", "attachment;filename=myExcel.xls"
    response.contentType = "application/vnd.ms-excel"

Then pass the contents in response.

Edit: if you need to set the content length:

    response.contentLength = 100

Content length recorded in javadoc

+8
source
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", "Attachment;Filename=\"MyFile.xls\"");
+2
source

jxl jar

:

@WebServlet("/Reportexel")
public class Reportexel extends HttpServlet
{
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        try
        {
            response.setHeader("Content-disposition", "attachment; filename=kbdemo.xls");
            response.setContentType("application/vnd.ms-excel");
            WritableWorkbook workbook = Workbook.createWorkbook(response.getOutputStream());    
            WritableSheet worksheet = workbook.createSheet("Sheet 1",0);

            Label lbl = new Label(1,1,"Hello");
            Label lbl1 = new Label(1,2,"Hi...");

            worksheet.addCell(lbl);
            worksheet.addCell(lbl1);

            workbook.write();
            workbook.close();
        }
        catch(Exception e)
        {
            System.err.println("Main Error : "+e);
        }
    }
}
0

Source: https://habr.com/ru/post/1534856/


All Articles