Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    helensyk is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Smile Save results excel

    I have a large simulation and I would like to save my results in an excel file.

    I have a main class and a class where there are the results, and I ama able to print them, but I can't save them in a file.
    How can I do it? Where do I have to put the parts here...? I can create a file with only the last parameters printed now...

    Thanks for your help, sorry for the silly question.

    This is the code for excel:

    Java Code:
    import java.io.*;
    import java.io.FileNotFoundException.*;
    import java.util.Date; 
    import jxl.*;
    import  org.apache.poi.hssf.usermodel.HSSFSheet;  
    import  org.apache.poi.hssf.usermodel.HSSFWorkbook; 
    import  org.apache.poi.hssf.usermodel.HSSFRow;
    import  org.apache.poi.hssf.usermodel.HSSFCell;
    import java.util.Arrays.*;
    
    public class CreateExcelFile {
    public static void main(String[]args){
    
    try{
    
    String filename="...Results.xls" ;
    HSSFWorkbook wb=new HSSFWorkbook();
    HSSFSheet sheet =  wb.createSheet("BTObserver");
    HSSFRow row;
    HSSFCell cell;
    
    for (short rowIndex = 0; rowIndex < 1000; rowIndex++) {
        
            row.createCell(1).setCellValue(((BitTorrent)(Network.get(i).getProtocol(pid))).getThisNodeID());
            row.createCell(2).setCellValue("(" + peerStatus + ")");
            row.createCell(3).setCellValue(numberOfCompletedPieces);
            row.createCell(4).setCellValue(((BitTorrent)(Network.get(i).getProtocol(pid))).nPiecesDown);
            row.createCell(5).setCellValue(((BitTorrent)(Network.get(i).getProtocol(pid))).nPiecesUp);
            row.createCell(6).setCellValue(CommonState.getTime());
    
    }
    
    FileOutputStream fileOut =  new FileOutputStream(filename);
    wb.write(fileOut);
    fileOut.close();
    System.out.println("Your excel file has been generated!");
    }catch(Exception ex){
        System.err.println(ex);
    }
    }
    }
    Last edited by helensyk; 07-31-2012 at 12:19 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Save results excel

    Please use [code] tags [/code] when posting code.

    Does that even compile?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Save results excel

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    helensyk is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default Re: Save results excel

    Quote Originally Posted by DarrylBurke View Post
    Moved from New to Java

    db
    Please help me, I don't know java, but I have to save my results in a file... Where do I have to use new...? How can I pass the object etc? I have a main and the class where I print my results, but I don't know how to save them.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Save results excel

    That code (which still doesn't have code tags) doesn't look like it compiles...
    So I have no idea how you've run it.

    So, what have you got and what errors are you getting?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Save results excel

    Quote Originally Posted by helensyk View Post
    Please help me, I don't know java
    Start by correcting that: The Java™ Tutorials

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    helensyk is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default Re: Save results excel

    I have multiple classes, and I want to post a new row every time the specified class is called. Could you please help me? Thanks!
    Last edited by helensyk; 07-31-2012 at 01:01 PM.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Save results excel

    OK.
    Here's the API for HSSFSheet.

    When adding rows you need to use the createRow() method (as shown in that API).
    That will give you a row that you can then work on.
    That is what's missing in your code above.

    But you are really going to have to learn to provide relevant information, because that code does not compile, which means you would have had a compilation error, which is the sort of thing you need to supply to a forum like this if you want help. You can't expect us to copy and paste code into an IDE in order to see the problem. You can't expect us to run the code to see the problem.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    helensyk is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default Re: Save results excel

    These are parts of the code, what you told me isn't a problem. I know how to use that code, but the real problem is:

    Example in pseudocode:

    if the upper code is a class.(writing a row in excel)

    main class(){...
    ...
    CreateExcel a=new CreateExcel;}
    And where do I need to write the ofstrem(output?)
    At the end maybe? of what?
    How can I sent the object etc...

    Thanks in apropriate.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Save results excel

    When you're finished with the document.
    create (or open) workbook
    do stuff (like add rows)
    do more stuff (if needed)
    close workbook
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    helensyk is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default Re: Save results excel

    Java Code:
    package peersim.bittorrent;
    
    import peersim.config.*;
    import peersim.core.*;
    import peersim.util.*;
    
    /**
     * This {@link Control} provides a way to keep track of some
     * parameters of the BitTorrent network.
     */
     public class BTObserver implements Control {
          
    	/**
    	 *	The protocol to operate on.
    	 *	@config
    	 */
    	private static final String PAR_PROT="protocol";
    	
    	/**
    	 *	Protocol identifier, obtained from config property
    	 */
    	private final int pid;
    	
    	/**
    	 *	The basic constructor that reads the configuration file.
    	 *	@param prefix the configuration prefix for this class
    	 */
    	public BTObserver(String prefix) {           
    		pid = Configuration.getPid(prefix + "." + PAR_PROT);
    	}
    	
    	/**
    	 * Prints information about the BitTorrent network
    	 * and the number of leechers and seeders.
    	 * Please refer to the code comments for more details.
    	 * @return always false
    	 */
    	public boolean execute() {
    		IncrementalFreq nodeStatusStats = new IncrementalFreq();
    		IncrementalStats neighborStats = new IncrementalStats();
    		
    		int numberOfNodes = Network.size();
    		int numberOfCompletedPieces = 0;
    		
    		// cycles from 1, since the node 0 is the tracker
    		for (int i=1; i<numberOfNodes; ++i) {
    			
    			// stats on number of leechers and seeders in the network
    			// and consequently also on number of completed files in the network
    			nodeStatusStats.add(((BitTorrent)(Network.get(i).getProtocol(pid))).getPeerStatus());
    			
    			// stats on number of neighbors per peer
    			neighborStats.add(((BitTorrent)(Network.get(i).getProtocol(pid))).getNNodes());
    		}
    		
    		// number of the pieces of the file, equal for every node, here 1 is chosen,
    		// since 1 is the first "normal" node (0 is the tracker)
    		int numberOfPieces = ((BitTorrent)(Network.get(1).getProtocol(pid))).nPieces;
    		
    		for (int i=1; i<numberOfNodes; ++i) {
    			numberOfCompletedPieces = 0;
    			
    			// discovers the status of the current peer (leecher or seeder)
    			int ps = ((BitTorrent)(Network.get(i).getProtocol(pid))).getPeerStatus();
    			String peerStatus;
    			if (ps==0) {
    				peerStatus = "L"; //leecher
    			}
    			else {
    				peerStatus = "S"; //seeder
    			}
    			
    			
    			if (Network.get(i)!=null) {
    				
    				// counts the number of completed pieces for the i-th node
    				for (int j=0; j<numberOfPieces; j++) {
    					if ( ((BitTorrent)(Network.get(i).getProtocol(pid))).getFileStatus()[j] == 16) {
    						numberOfCompletedPieces++;
    					}
    				}
    				
    				/*
    				 * Put here the output lines of the Observer. An example is provided with
    				 * basic information and stats.
    				 * CommonState.getTime() is used to print out time references
    				 * (useful for graph plotting).
    				 */
    				
    				System.out.println("OBS: node " + ((BitTorrent)(Network.get(i).getProtocol(pid))).getThisNodeID() + "(" + peerStatus + ")" + "\t pieces completed: " + numberOfCompletedPieces + "\t \t down: " + ((BitTorrent)(Network.get(i).getProtocol(pid))).nPiecesDown + "\t up: " + ((BitTorrent)(Network.get(i).getProtocol(pid))).nPiecesUp + " time: " + CommonState.getTime());
    				//System.out.println("[OBS] t " + CommonState.getTime() + "\t pc " + numberOfCompletedPieces + "\t n " + ((BitTorrent)(Network.get(i).getProtocol(pid))).getThisNodeID());
                                    }                       
    			else {
    				//System.out.println("[OBS] t " + CommonState.getTime() + "\t pc " + "0" + "\t n " + "0");
    			}
    		
    		}
    		
    		// prints the frequency of 0 (leechers) and 1 (seeders)
    		nodeStatusStats.printAll(System.out);
    		
    		// prints the average number of neighbors per peer
    		System.out.println("Avg number of neighbors per peer: " + neighborStats.getAverage());
    
                    return false;
    	}
    }
    I hope it helps, I have to save the results which are printed. I found the code above for the excel.

    I also have a main class. My main class just calls the configuration file, now.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Save results excel

    Take a step back and think about what you are trying to achieve, because at the moment it makes no sense.
    Is this Excel file acting as a log?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    helensyk is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default Re: Save results excel

    I know how to create an excel with the last row, only.
    I know how to create an excel with many rows, when I know what to write.
    I am not good in Java.
    I have the last code and a main class and I have to put the first code in order to save my results, somebody told me to use append.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Save results excel

    Append won't work as it's an excel file, and simply appending some random stuff (which would be the entire Excel document) isn't going to work.
    With POI you generally read the file into a Workbook, edit the sheets however you want, and then write the file back out...the whole thing, because otherwise you are simply going to muck up the format of the file.

    So, again -
    Open in workbook
    edit the data
    write back

    That's it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    helensyk is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default Re: Save results excel

    I have to use objects and everything, how could it take place? I can create the workbook in the main class, I have to write the rows in the specified class and at the end I have to write and close everything in the main class. I have no idea on how to do it all. I have to create every row and at the end to put them all in the excel.
    Last edited by helensyk; 07-31-2012 at 05:21 PM.

  16. #16
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Save results excel

    I'm sorry, but I really can't see where your problem is.
    And I can't see what you are trying to do.

    I don't know where the BTObserver class fits in at all, how it is called.
    It's not called in that main() method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  18. #18
    helensyk is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default Re: Save results excel

    It's not you, it's difficult to explain! Thanks a lot! I don't know how to create a new row each time I want to print something... Especially if I have created the object in main... How could I add rows in another class? Everytime the loop runs I need a new row to be added! Please could you try again?

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Save results excel

    Pass the Sheet into the class that is going to add the rows.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Save results excel

    Java Code:
    class SomeClass {
        public void addRows(HSSFSheet sheet) {
            // add rows to the sheet here
        }
    }
    then from main:
    Java Code:
    HSSFWorkbook wb=new HSSFWorkbook();
    HSSFSheet sheet =  wb.createSheet("BTObserver"); 
    SomeClass sc = new SomeClass();
    sc.addRows(sheet);
    Now this all depends where the object that's going to be adding the rows is located.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Page 1 of 2 12 LastLast

Similar Threads

  1. (in Java mysql to excel) where i have specify the path to save the file
    By raj.mscking@gmail.com in forum New To Java
    Replies: 3
    Last Post: 06-04-2012, 11:23 AM
  2. Replies: 0
    Last Post: 11-17-2011, 10:33 AM
  3. How to save the results for checkbox
    By pink123 in forum AWT / Swing
    Replies: 1
    Last Post: 03-07-2011, 11:19 PM
  4. Java results in Excel
    By puk284 in forum New To Java
    Replies: 1
    Last Post: 04-21-2009, 05:13 AM
  5. Save data from Excel sheet to servlet
    By chandu.v in forum Java Servlet
    Replies: 2
    Last Post: 04-10-2008, 02:51 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •