如何使它可下载使用servlet excel文件 [英] How to make it as downloadable excel file using servlets

查看:152
本文介绍了如何使它可下载使用servlet excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读一个txt文件,并将其写入Excel文件,但同时也使可下载的excel文件是不写任何数据到Excel,并给予显示此消息

I am reading a txt file and writing it into excel file, but while making it as downloadable excel file it is not writing any data to excel and giving showing this message

下载到Excel中的逻辑

logic for downloading into excel

response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition", "attachment; filename=sampleName.xls");
    String path = getServletContext().getRealPath(file);
    File file = new File(path);
    System.out.println("File:" + file);
    FileInputStream is = new FileInputStream(uploadFilePath
            + File.separator + file.getName());
    try {
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        ArrayList<ArrayList<String>> exceldata = new ArrayList<ArrayList<String>>();
        ArrayList<String> headerRow = new ArrayList<String>();
        headerRow.add("Date and Time");
        headerRow.add("NMEA Message Type");
        headerRow.add("Fragments in the message");
        headerRow.add("Fragments no");
        headerRow.add("AIS MessageType");
        headerRow.add("Repeat Indicator");
        headerRow.add("MMSI Number");
        headerRow.add("AIS Version");
        headerRow.add("IMO Number");
        headerRow.add("Navigational status");
        headerRow.add("Rate Of Turn(ROT)");
        headerRow.add("Speed Over Ground(SOG)");
        headerRow.add("Position Accuracy(PA)");
        headerRow.add("Longitude");
        headerRow.add("Latitude");
        headerRow.add("Course Over Ground(COG)");
        headerRow.add("Heading(HDG)");
        headerRow.add("Time Stamp");
        exceldata.add(headerRow);
        String strLine;
        while ((strLine = br.readLine()) != null) {
            System.out.println(strLine);
            exceldata.add(decodeAisData(strLine));
        }
        writeDataToExcelFile("praveen",exceldata);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

有关将数据写入到Excel:

For writing the data to excel:

private void writeDataToExcelFile(String string, ArrayList<ArrayList<String>> excelData) {
    HSSFWorkbook myWorkBook = new HSSFWorkbook();
    String sheetName = "";
    sheetName = "Document-" + 0;
    HSSFSheet mySheet = myWorkBook.createSheet(sheetName);                  
    HSSFRow myRow = null;
    HSSFCell myCell = null;
    for (int rowNum = 0; rowNum < excelData.size(); rowNum++) {
        ArrayList<String> rowData = excelData.get(rowNum);
        myRow = mySheet.createRow(rowNum);
        for (int cellNum = 0; cellNum < rowData.size(); cellNum++) {
            myCell = myRow.createCell(cellNum);
            myCell.setCellValue(rowData.get(cellNum));
        }
    }
    try {
        FileOutputStream out = new FileOutputStream("my.xls");
        myWorkBook.write(out);
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

所以,请给予解决方案,以它为downloadable.Thanks

So please give solution to make as it as downloadable.Thanks

推荐答案

writeDataToExcelFile 方法Excel数据写入到一个FileOutputStream - 这是没有连接到响应的OutputStream。

Your writeDataToExcelFile method writes the Excel data into a FileOutputStream - this is not connected to the response OutputStream.

您应该更新 writeDataToExcelFile 方法包括另一个参数:

You should update the writeDataToExcelFile method to include another parameter:

private void writeDataToExcelFile(String string, 
                                  ArrayList<ArrayList<String>> excelData,
                                  OutputStream outputStream) 

和数据的写入,改成这样:

and the writing of the data, change to this:

myWorkBook.write(outputStream);

这应该然后允许myWorkBook对象写回浏览器。

This should then permit the myWorkBook object to write back to the browser.

此外,改变调用该方法到该行:

Also, change the line that calls the method to:

writeDataToExcelFile("praveen",exceldata, response.getOutputStream());

这篇关于如何使它可下载使用servlet excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆