如何使用servlet将其作为可下载的excel文件

时间:2013-11-18 08:52:00

标签: java excel servlets apache-poi

我正在读取一个txt文件并将其写入excel文件,但是当它作为可下载的excel文件时,它不会将任何数据写入excel并显示此消息 Warning message while opening the xls file

下载到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:

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();
    }

}

所以请提供解决方案,使其可以下载。谢谢

2 个答案:

答案 0 :(得分:4)

您的writeDataToExcelFile方法将Excel数据写入FileOutputStream - 这未连接到响应OutputStream。

您应该更新writeDataToExcelFile方法以包含其他参数:

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

并写入数据,改为:

myWorkBook.write(outputStream);

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

另外,将调用方法的行更改为:

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

答案 1 :(得分:0)

我强烈反对自己实施这一点,因为你很可能会忘记一些特殊情况,例如关于不同的分隔符,特殊字符,......

相反,请使用众多现有库中的一个。