2

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 Warning message while opening the xls file

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

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

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

4
  • 2
    Instead of instantiating a FileOutputStream, have you tried passing the response OutputStream, and writing to that instead - otherwise, the content is not being sent to the browser. Commented Nov 18, 2013 at 9:05
  • @Crollster No, can you explain clearly Commented Nov 18, 2013 at 9:09
  • Will add it as an answer... Commented Nov 18, 2013 at 9:09
  • @Crollster how to get the response, and pass to FileOutputStream Commented Nov 18, 2013 at 9:11

2 Answers 2

4

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

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

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()); 
Sign up to request clarification or add additional context in comments.

5 Comments

How to create multiple sheets in single excel file. I have different types of data. I want different sheets for each type of data
According to the JavaDocs (poi.apache.org/apidocs/index.html?org/apache/poi/hssf/usermodel/…), you should use the createSheet(String) method of the HSSFWorkbook class to add more sheets to a workbook.
Which is the better approaching for the sorting the array list by the column value "MMSI Number" in the above code
Probably wrapping each row in an object, implementing Comparable (and checking against the MMSI property, and storing them all in a TreeSet. This should then ensure they are in the order of MMSI (or whatever you chose to check against in the compareTo method.
Once can you check what is wrong with is logic stackoverflow.com/questions/20064481/…
0

I would strongly discourage implementing this on your own, since you are very likely forget some special cases, for example concerning different separators, special characters, ...

Instead, use one of the many existing libraries for that.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.