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