Results 1 to 6 of 6
  1. #1
    Irish Rayray is offline Member
    Join Date
    Apr 2012
    Posts
    21
    Rep Power
    0

    Default Reading in From a text then opening a Excel File and Modifying it.

    Hi im pretty new to java and I am trying to create a program that Reads in data from a Excel file looks for aaaaa then pulls out the integers after it and prints that to a excel file.

    Say this is the .txt
    ___________________________________
    Hello World

    P1 = aaaaa= 5623, bbbbb= 7892
    P2 = aaaaa= , bbbbb= 5454

    Goodbye World
    More Text
    ______________________________________

    and this is the excel
    ______________________________________
    A B C
    1 Project -> P1 P2
    2 aaaaa= 5623 5434
    3 bbbbb= 7892 5454
    4
    _____________________________________

    tghis is the code i have but i cant get it to work at the moment it prints like
    A B C
    1 Project -> P1 P2
    2 aaaaa=5623,
    bbbbb=7892
    3
    4
    so it all goes into one cell.

    I was wondering if anyone could help also I am not sure if this is in the right place if its Advanced java or not.

    Java Code:
    import java.io.*;
    import java.util.*;
    
    import jxl.CellView;
    import jxl.Workbook;
    import jxl.WorkbookSettings;
    import jxl.format.UnderlineStyle;
    //import jxl.write.Formula;
    import jxl.write.Label;
    //import jxl.write.Number;
    import jxl.write.WritableCellFormat;
    import jxl.write.WritableFont;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    import jxl.write.WriteException;
    import jxl.write.biff.RowsExceededException;
    
    
    public class WriteExcel 
    {
    
    	private WritableCellFormat timesBoldUnderline;
    	private WritableCellFormat times;
    	private String inputFile;
    	
    	public void setOutputFile(String inputFile) 
    		{
    		this.inputFile = inputFile;
    		}
    		public void write() throws Exception 
    		{
    			File file = new File(inputFile);
    			WorkbookSettings wbSettings = new WorkbookSettings();
    	
    			wbSettings.setLocale(new Locale("en", "EN"));
    	
    			WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
    			workbook.createSheet("Lines of Code", 0);
    			WritableSheet excelSheet = workbook.getSheet(0);
    			createLabel(excelSheet);
    			createContent(excelSheet);
    			
    			
    			
    			workbook.write();
    			workbook.close();
    		}
    	
    		private void createLabel(WritableSheet sheet)throws WriteException 
    		{
    			WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
    			times = new WritableCellFormat(times10pt);
    			times.setWrap(true);
    	
    			WritableFont times10ptBoldUnderline = new WritableFont(
    					WritableFont.TIMES, 10, WritableFont.BOLD, false,
    					UnderlineStyle.SINGLE);
    			timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
    	
    			timesBoldUnderline.setWrap(true);
    	
    			CellView cv = new CellView();
    			cv.setFormat(times);
    			cv.setFormat(timesBoldUnderline);
    			cv.setAutosize(true);
    			
    			addCaption(sheet, 0, 0, "Project");
    			addCaption(sheet, 0, 1, "Ansic");
    			addCaption(sheet, 0, 2, "Cpp");
    			
    	
    		}
    	
    		private void createContent(WritableSheet sheet) throws Exception, WriteException,
    				RowsExceededException 
    		{
    			FileReader fr=new FileReader("F:../HelloWorld.txt");
    			BufferedReader br=new BufferedReader(fr);
    			   
    			   String s;
    			   String k;
    			   while ((s=br.readLine())!=null)
    			   {
    			       int indexfound=s.indexOf("aaaaa");
    			       if (indexfound > -1)
    			       {
    						for (int i = 0; i < 1; i++)
    						{   
    							addLabel(sheet, i + 1, 1, s);
    							System.out.println("Label Printed");
    						}
    						for (int j = 0; j < 1; j++){
    				    		   addLabel(sheet, j + 2, 1, s);
    				    		   System.out.println("Label Printed");
    				    	   }
    			       }
    			   }
    		}
    	
    		private void addCaption(WritableSheet sheet, int column, int row, String s)
    				throws RowsExceededException, WriteException 
    		{
    			Label label;
    			label = new Label(column, row, s, timesBoldUnderline);
    			sheet.addCell(label);
    		}
    				
    		private void addLabel(WritableSheet sheet, int column, int row, String s)
    				throws WriteException, RowsExceededException 
    		{
    			Label label;
    			label = new Label(column, row, s, times);
    			sheet.addCell(label);
    		}
    		
    		public static void main(String args[]) throws Exception
    		{
    			WriteExcel LoC = new WriteExcel();
    			LoC.setOutputFile("F:/Workspace/HelloWorld123.xls");
    			LoC.write();
    			System.out.println("Please check the result file under: F:/Workspace/HelloWorld.txt" );
    		}
    }

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

    Default Re: Reading in From a text then opening a Excel File and Modifying it.

    Where are you breaking out the individual bits of the data?
    I see you getting the index for aaaaa, but I don't see where you substring the line?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Irish Rayray is offline Member
    Join Date
    Apr 2012
    Posts
    21
    Rep Power
    0

    Default Re: Reading in From a text then opening a Excel File and Modifying it.

    I am not subtracting the line there mainly because i was having trouble doing so and cant figure out how to do it right.

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

    Default Re: Reading in From a text then opening a Excel File and Modifying it.

    Just break it up.
    If the format is consistent then just substring based on the index for aaaa and bbbb.
    Write a simple test method by itself that you can pass in test Strings representing one of those lines.
    Have that method break up the String into an Object, consisting of the identifier ('P1' etc) and the two numbers, aaaa and bbbb.

    Once you have that then worry about the rest of it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Irish Rayray is offline Member
    Join Date
    Apr 2012
    Posts
    21
    Rep Power
    0

    Default Re: Reading in From a text then opening a Excel File and Modifying it.

    That's the Major prob with this as for now is consistent but it wont be once i start using it the only consistent part will be that after aaaaa there be = so it'll be aaaaa= or could be ffffff=. That why i'm having so much trouble trying to figure out away of doing this.
    And i haven't had time to work on this till today.
    There a line that will be above the information that will always be consistent so i think ill have to use that.

    ____________________________
    dadasda
    asdasda
    asdadsasd
    adasd
    897
    84sd
    s6sa8d


    Hello World

    P1 aaaaa= 123455, bbbbb= 543433
    P2 ggggg= 546456, ddddd= 8975

    End of the World


    ajish
    sdajda
    asdjais
    asdao
    (Just stuff thst will be in document which can be ignored)
    __________________________________________________ __

    the line Hello World and End of the world will always be there(well there not gonna be named that but for test perposes they will be)
    only the data inbetween them is important

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

    Default Re: Reading in From a text then opening a Excel File and Modifying it.

    You need to define the rules that apply to that format.

    From here it looks like -
    up to first space is identifier.
    after that, comma separated key/value pairs with the key before an '=' and the value after it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Having trouble opening and reading a txt file
    By DBaskov in forum New To Java
    Replies: 14
    Last Post: 01-29-2011, 02:55 AM
  2. Help opening and reading a file
    By viperlasson in forum New To Java
    Replies: 0
    Last Post: 09-21-2010, 07:32 AM
  3. Opening mp3 file ( not reading )
    By Char in forum New To Java
    Replies: 15
    Last Post: 08-29-2010, 07:09 AM
  4. Modifying text file
    By right2001 in forum New To Java
    Replies: 7
    Last Post: 04-13-2009, 08:41 AM
  5. Modifying text file
    By right2001 in forum New To Java
    Replies: 7
    Last Post: 04-08-2009, 01:18 AM

Posting Permissions

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