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 5 of 5

Thread: Excel Report

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Excel Report

    I have a excel report that I created and now I need help with the formating. The report is currently hand written and follows a certain format in order to fit all the information on one page if printed. Now the problem is the report goes down and then to the right. Under each column is a supervisor's name and under that is the information regarding all of the projects information that supervisor is responsible including who is assigned to each project. Sort of like the following.


    Supervisor A Supervisor B Supervisor C
    Project Info Project Info Project Info
    Employees Employees Employees
    Project Info Project Info Project Info
    Employees Employees Employees

    I need to know if there is way to write the report in the code that will allow me to write down a column then come back to the top of the column to the right if so then how. Or if there is any tutorial I can follow to get sort of close to what I need. I tried searching but couldn't find anything.

    Below is a breif sample of my code for the report.


    public boolean generateTechPhotoReport(Calendar passedDate, String passedFileName){
     
    StringBuffer reportOutput = new StringBuffer();
     
    reportOutput.append("Project #/ Title, Start Time, Start Date\n");
     
    BufferedWriter out = new BufferedWriter(new FileWriter(passedFileName));
    out.write(reportOutput.toString());
    out.close();
     
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Excel Report

    Two main ways I would do it:

    1. Build up the entire report in memory first. You can then write out an entire row at a time to the file, avoiding having to write to a previous section of the file.

    2. Use an external library like Apache POI to directly modify the contents of an Excel Spreadsheet. I would recommend this way, it's intuitive and allows you to do advance stuff with your report that the first method can't do.

    More information about ApachePOI (including where to get it and links to a few quick reference guides and tutorials): http://www.javaprogrammingforums.com...xcel-java.html

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Excel Report

    I was thinking about doing the report in memory also, I have done that with other reports. I did that with a report that generates the employees that are off from a selected date plus 7. I used Vectors to hold the data for each day. The reason I don't think I would be able to it with this report is there may be different amount of supervisors from one day to the next. Do you know of a way to handle this?

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Excel Report

    Read the entire report into memory, append/remove what you need, then re-write the entire file.

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    banny7 (October 18th, 2011)

  6. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Excel Report

    I forgot to post that I figured a way to fix my issue. I did it by writing it to a pdf instead of excel and I used nested tables. I a little like the following.


     
    try {
    //Create the Document
    Document document = new Document(PageSize.LETTER.rotate());
    //Create the PDF writer
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(passedFileName));
    //Open the document
    document.open();
     
    //The table for other tables to be nested in
    PdfPTable fullTable = new PdfPTable(1);
    fullTable .setWidthPercentage(100);
     
    //Set the table so each supervisor has a column, nested table
    PdfPTable supervisorTable = new PdfPTable(numOfCol);
    //numOfCol was determined by the size of the result set that I got back 
     
     
    PdfPTable projectTable = new PdfPTable(sizeForTable);
     
    PdfPCell cell;
     
    for(int i = 0; i < numOfCol; i++){ //Used for loop to get each supervisor into the column header
     
    String currentSupervisorsName = "Supervisor " + Integer.toString(i);
    cell = new PdfPCell(new Paragraph(currentSupervisorsName, fontBold));
    cell.setBorderWidth(1);
    supervisorTable.addCell(cell);
     
    //Nested Table, needed to be empty each time through so I add this table to projectTable 
    PdfPTable projectInformationTable = new PdfPTable(1);
     
    cell = new PdfPCell(new Paragraph(/*data*/, fontNormal));
    cell.setBorderWidth(1);
    projectInformationTable.addCell(cell);
     
    projectTable.addCell(projectInformationTable);
    }
     
     
    fullTable.addCell(supervisorTable);
    fullTable.addCell(projectTable);
     
     
    document.add(fullTable);
    document.close();
     
    } catch (Exception ex) {
    ex.printStackTrace();
    JOptionPane.showMessageDialog(null, "Unable to load reports at this time. Please try again later.", "Application Error", JOptionPane.ERROR_MESSAGE);
    }

Similar Threads

  1. Jasper Report
    By zurich91 in forum AWT / Java Swing
    Replies: 0
    Last Post: September 25th, 2011, 01:35 PM
  2. Replies: 3
    Last Post: September 1st, 2011, 10:49 AM
  3. Dynamic Jasper report
    By manojkp in forum Web Frameworks
    Replies: 0
    Last Post: April 6th, 2011, 05:41 AM
  4. Creating a formatted printable report
    By bradleysm in forum Java Theory & Questions
    Replies: 6
    Last Post: January 27th, 2011, 04:46 PM
  5. Calling Crystal Report
    By goutamdaphtari in forum Java Theory & Questions
    Replies: 0
    Last Post: July 15th, 2010, 12:02 AM