how to generate the POI excel file in a specific location? - java

I have created a java maven programme that generate an excel file using Apache POI API, all is working correctly but when i try to put the report to a specific location doing like this
: FileInputStream inputStream = new FileInputStream(new File("C:\\Users\\C5292600\\Desktop\\report1.xlsx"));
i get the following error :
Exception in thread "main" java.lang.IllegalArgumentException: Invalid char (:) found at index (1) in sheet name 'C:\Users\C5292600\Desktop\repor'
at org.apache.poi.ss.util.WorkbookUtil.validateSheetName(WorkbookUtil.java:151)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.createSheet(XSSFWorkbook.java:873)
at com.occCheckNoData.report.Create.newSheet(Create.java:25)
at com.occCheckNoData.report.mainReport.main(mainReport.java:34)

Is it an existing path? If not use something like File file = new File(yourpath); file.mkdirs(); to create a new path. And to generate the file FileOutputStream fileOut = new FileOutputStream(new File(yourpath + filetype));

Related

Where do find a File in the Eclipse Project

I know this question has been asked before, but I have a slightly different problem.
I want to read and write to a text file which will be included in my build(.jar file) . its a Gui application java.
Where Should I place the file?
I have placed the file by creating a new Resources folder under the project. This enables me to read the file but when I attempt to write to it, it throws a FileNotFound Exception. What should I do?
I am using this code
File file = new File(this.getClass().getClassLoader().getResource("score.txt").getFile());
FileOutputStream stream = new FileOutputStream(file);
DataOutputStream write = new DataOutputStream(stream);
write.writeInt(max);
And for Input i use this :
File file = new File(this.getClass().getClassLoader().getResource("Files/score.txt").getFile());
FileInputStream input = new FileInputStream(file);
DataInputStream read = new DataInputStream(input);
System.out.println(read.readInt());
For reading this code works perfectly.

Reading Excel from src folder using JXL

I am a beginner and having a hard time to get my program run correctly after exporting it as runnable JAR.
I need to read an Excel called "bstn.xls", which is in the dDefault package such as all other classes.
Here is how I did it.
get the file path with: URL excelURL = this.getClass().getClassLoader().getResource("bstn.xls");
Create a File with the URL File file = new File(new ResourceLoader().loader());
Create a workbook with the file wbook = Workbook.getWorkbook(file);
I got the error in german here, but it says: the System cannot find the given path.
Is JXL not able to read with the full qualified path? I can't find anything on the JXL site.
Since the file is in the classpath, you may use the getWorkbook method taking an InputStream parameter :
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("bstn.xls");
wbook = Workbook.getWorkbook(stream);

Using Java to open excel then to read xml and then save as xls

My response from server is in xml format, I am able to open this file using excel and excel will auto generate the table for me. After that I can save this file as xls, and my java program is capable of retreiving those information through loops. My question is how do I use java code to replace the step " open xml using excel, wait for auto generated table, then save this file as xls". I tried several ways, it is not going to work, if I open the xml file using java code, it will give me invalid header error. Any help would be greatly appreciated. My code is as follow:
FileInputStream input = new FileInputStream("Y:\\xx\\xx .xml");
POIFSFileSystem fs = new POIFSFileSystem( input );
HSSFWorkbook wb = new HSSFWorkbook(fs);
FileOutputStream fos = new FileOutputStream("Y:\\xx\\xx.xls") ;
wb.write(fos);
fose.close();
This will give me invalid header error, please help.

Writing to Files : Printwriter Converting Forward Slash to Backslash

Why is Printwriter doing this?
File file = new File("/files/KA.txt");
writer = new PrintWriter(file);
writer.write("HELLO");
In the above code I keep getting an error that says :
java.io.FileNotFoundException: \files\KA.txt (The network path was not found)
Except this was not my specified path? How do I then specify a file to write - usually create a new file and write to this? It also throws errors if KA.txt is not present - I ideally want to create a new file and writer to it.
Thanks
I ideally want to create a new file and writer to it.
You can simply create a file ,
PrintWriter writer = new PrintWriter("name.txt", "UTF-8");
writer.println("text");
where UTF-8 is the file encoding. and write to the file , remember it overrides if the file exists with the same name
The problem is that the parent /files directory doesn't already exist, so you must create it beforehand, using File.mkdirs.
File file = new File("/files/KA.txt");
File parentFile = file.getParentFile();
parentFile.mkdirs();
PrintWriter writer = new PrintWriter(file);
writer.write("HELLO");
writer.close();

FileInputStream vs ClassPathResource vs getResourceAsStream and file integrity

I have a weird problem :
in src/main/resources i have a "template.xlsx" file.
If i do this :
InputStream is = new ClassPathResource("template.xlsx").getInputStream();
Or this :
InputStream is = ClassLoader.getSystemResourceAsStream("template.xlsx");
Or this :
InputStream is = getClass().getResourceAsStream("/template.xlsx");
When i try to create a workbook :
Workbook wb = new XSSFWorkbook(is);
I get this error :
java.util.zip.ZipException: invalid block type
BUT, when i get my file like this :
InputStream is = new FileInputStream("C:/.../src/main/resources/template.xlsx");
It works !
What is wrong ? I can't hardcode the fullpath to the file.
Can someone help me with this ?
Thanks
I had the same issue, you probably have a problem with maven filtering.
This code load the file from source, unfiltered
InputStream is = new FileInputStream("C:/.../src/main/resources/template.xlsx");
This code load the file from the target directory, after maven has filtered the content
InputStream is = getClass().getResourceAsStream("/template.xlsx");
You should not filter binary files like excel and use two mutually exclusive resource sets as described at the bottom of this page maven resources plugin
haven't you try accessing it like
InputStream is = new FileInputStream("/main/resources/template.xlsx");
?

Categories

Resources