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.

Results 1 to 4 of 4

Thread: how to create excel file in java

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to create excel file in java

    please help me....................
    Attached Images Attached Images
    Last edited by viral.bhat; March 30th, 2014 at 11:39 PM.


  2. #2
    Junior Member
    Join Date
    Mar 2014
    Location
    Hong Kong
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to create excel file in java

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

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to create excel file in java

    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

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: how to create excel file in java

    @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

Similar Threads

  1. java code for saveas Excel 2007 file to Excel97
    By manoj.shirgire in forum Computer Support
    Replies: 3
    Last Post: January 30th, 2014, 07:44 AM
  2. create excel file
    By deependeroracle in forum Java Theory & Questions
    Replies: 4
    Last Post: February 11th, 2012, 01:26 AM
  3. Replies: 10
    Last Post: January 12th, 2011, 05:48 AM
  4. how to import excel file to database table using java
    By palani in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 3rd, 2010, 12:17 AM
  5. insert(embed) a file object (.txt file) in MS excel sheet using java.
    By jyoti.dce in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 12th, 2010, 08:16 AM