• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

export to excel code not working on linux server

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have created export to excel data code using java poi. This code gets executed very finely on my local tomcat server, but i upload my project on the linux server, and there the code is not getting executed.
Below is my export to java function, anybody please tell me if i am going wrong in this code.

in this code i m first creating an exel file and saving in 'temp' folder i.e. the xls file get stored at this C:\Documents and Settings\Admin\.netbeans\7.1\apache-tomcat-7.0.22.0_base\temp\temp4750857184897442788.xls

public class ExportData {

Connection conn = null;
ResultSet rs1 = null;
PreparedStatement ps1 = null;
Workbook wb = new HSSFWorkbook();
HSSFSheet sheet = (HSSFSheet) wb.createSheet("new sheet");
HSSFRow rowhead = sheet.createRow((short) 2);
int index = 3;
int sno = 0;

public void exportCre(String sql) {

try {
conn = DbConnection.getConnection();
ps1 = conn.prepareStatement(sql);
rs1 = ps1.executeQuery();

rowhead.createCell((short) 0).setCellValue("SNo");
rowhead.createCell((short) 1).setCellValue("Date");
rowhead.createCell((short) 2).setCellValue("Login Name");


System.out.println("Sql completed");

while (rs1.next()) {
sno++;

HSSFRow row = sheet.createRow((short) index);
row.createCell((short) 0).setCellValue(sno);
row.createCell((short) 1).setCellValue(rs1.getString(1));
row.createCell((short) 2).setCellValue(rs1.getString(2));

index++;

}

File tempXlsx = File.createTempFile("temp", ".xls");
wb.write(new FileOutputStream(tempXlsx));
Desktop.getDesktop().open(tempXlsx);


} catch (Exception e) {
e.printStackTrace();
}

}
}

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code includes a line:

If you run this on a Linux server that you access remotely through a web browser, then this will try to open the Excel sheet on the server, not the client's computer.
 
Vinod suryawanshi
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Jasper.

Even I was doubting on this line. Could you please help us out with this. I need this code to execute on linux server and open the excel file automatically on client's computer.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think it's possible to do this without permission from the user - if it were possible to always automatically open files, without asking the user if (s)he wants to open the file, there would be a security problem with the browser.

If you have this in a web page, then a servlet could return the content of the file in the body of the HTTP response; you'd have to set the right content type in the HTTP header. The user would then get a "would you like to download or open this file" prompt from the browser.
 
Vinod suryawanshi
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jasper,

I have bit modified my code but now i am getting a null pointer exception at response.setContentType(). Please help me in this. Need it very urgently.


public class ExportData {

Connection conn = null;
ResultSet rs1 = null;
PreparedStatement ps1 = null;
Workbook wb = new HSSFWorkbook();
HSSFSheet sheet = (HSSFSheet) wb.createSheet("new sheet");
HSSFRow rowhead = sheet.createRow((short) 2);
int index = 3;
int sno = 0;
HttpServletResponse response;

public void exportCre(String sql) {

try {
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
conn = DbConnection.getConnection();
ps1 = conn.prepareStatement(sql);
rs1 = ps1.executeQuery();

rowhead.createCell((short) 0).setCellValue("SNo");
rowhead.createCell((short) 1).setCellValue("Date");
rowhead.createCell((short) 2).setCellValue("Login Name");


System.out.println("Sql completed");

while (rs1.next()) {
sno++;

HSSFRow row = sheet.createRow((short) index);
row.createCell((short) 0).setCellValue(sno);
row.createCell((short) 1).setCellValue(rs1.getString(1));
row.createCell((short) 2).setCellValue(rs1.getString(2));

index++;

}

File tempXlsx = File.createTempFile("temp", ".xls");
wb.write(new FileOutputStream(tempXlsx));
response.setHeader("Content-Disposition", "attachment; filename=\"" + tempXlsx.getName() + "\"");

} catch (Exception e) {
e.printStackTrace();
}

}
}

Thanks in advance
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vinod suryawanshi wrote:Hi Jasper,
I have bit modified my code...


Vinod:
1. When you post code, please UseCodeTags (←click). And please read the page thoroughly, because there are a few gotchas to know about (one being very long lines).
2. It's 'Jesper', not 'Jasper'.

Please help me in this. Need it very urgently.


3. EaseUp (←click). We're all volunteers here, and urgency is your problem, not ours.

However, from what I can see, the main problem is that you're not setting 'response' to anything; therefore you're bound to get an NPE when you try to use it.

Winston
 
Vinod suryawanshi
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper for your Time and Reply.....

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic