please help me....................
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
please help me....................
Last edited by viral.bhat; March 30th, 2014 at 11:39 PM.
If you are generating the excel in a browser just call the method you want to generate the excel file based on a url and set the response properties like this,
//1.Fill the data from db //2.Set the response properties String fileName = "Excel.xls"; response.setHeader("Content-Disposition", "inline; filename=" + fileName); // Make sure to set the correct content type(the below content type is ok) response.setContentType("application/vnd.ms-excel"); //3.Write to the output stream Writer.write();//call write method of Writer class to write the data to o/p stream
Writer Class:
public class Writer { private static Logger logger = Logger.getLogger("service"); /** * Writes the report to the output stream */ public static void write(HttpServletResponse response, HSSFSheet worksheet) { logger.debug("Writing excel data to the stream"); try { // Retrieve the output stream ServletOutputStream outputStream = response.getOutputStream(); // Write to the output stream worksheet.getWorkbook().write(outputStream); // Flush the stream outputStream.flush(); } catch (Exception e) { logger.error("Unable to write excel data to the output stream"); } } }
You can read the article
try {
File file = new File(mFile);
XSSFWorkbook wb;
XSSFSheet sheet;
XSSFRow row;
XSSFCell cell;
int rowIndex = 0;
if (file.exists()) {
InputStream excelFile = new FileInputStream(file);
wb = new XSSFWorkbook(excelFile);
sheet = wb.getSheetAt(0);
rowIndex = sheet.getLastRowNum();
rowIndex += 1;
row = sheet.createRow(rowIndex);
}
else {
wb = new XSSFWorkbook();
sheet = wb.createSheet();
row = sheet.createRow(0);
cell = row.createCell(0);
cell.setCellValue("1");
cell = row.createCell(1);
cell.setCellValue("2");
cell = row.createCell(2);
cell.setCellValue("3");
row = sheet.createRow(1);
}
cell = row.createCell(0);
cell.setCellValue("data");
FileOutputStream out = new FileOutputStream(mFile);
wb.write(out);
out.flush();
out.close();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Last edited by copeg; March 31st, 2014 at 11:12 AM. Reason: Removed link
@Cheungxin and @yushulx, welcome to the forums. Please read the forum guidelines and http://www.javaprogrammingforums.com...n-feeding.html - your posts can not only be considered as such, but are also out of context (servlets?) and neglect to mention any required 3rd party libraries.
I recommend the original poster go to the source of the required 3rd party library required to run any code in the replies of this thread...a library which has ample examples to get one started: Apache POI