Create automation to remove workbook password from .xlsx files - java

I have a scheduler sending me files in an FTP every day. The files are in an excel format and they have a password (workbook password) which is the same for every file.
I want to create an automation that will remove the password from the files. I had the idea of just opening the file using the password and then saving it. I am guessing this will not keep the password in the new file.
I tried (I had tickets here for this purpose) to replicate that using PHPExcel and MACRO but with no success. There is no command in PHP for that and in MACRO, I need to have the file open to run the MACRO.
So, from what I have found the only solution is to use JExcel API.
This script should work :
Workbook workbook = Workbook.getWorkbook(new File("/path/to/protected.xls"));
workbook.setProtected(false);
WritableWorkbook copy = Workbook.createWorkbook(new File("/path/to/unprotected.xls"), workbook);
WritableSheet[] sheets = copy.getSheets();
for (WritableSheet sheet : sheets){
sheet.getSettings().setProtected(false);
}
copy.write();
copy.close();
But I don't know how to make it run. Can I just create a PHP file where I will include this script? In PHP file I was able to include SQL code as well. Is that possible with Java?
I have not used Java for years and I don't remember if I can make this run by creating one file. I will have to download the software and make it run using classes I guess. But I want only a file to run in my server that will take the protected file and save it (with no password).
Any ideas are welcome and I can update the question accordingly.
Cheers

You can make the program executable on its own by creating an executable jar file.
Are you using an IDE to write the program? There are various methods to generate them for you.

Related

Possible to automate an excel process using java?

My current program is trying to download files and then combine them into one large excel file. The issue that I'm struggling with is that the website I'm downloading them from is for some reason making them .html files, but appending the .xls extension to them. This allows them to be opened by Excel manually but does not allow me to use Apache POI in order to read them as it sees a file format/extension difference. My process is as follows:
1 - Run part of my program which downloads a file through my web browser using Selenium - This works fine
2 - Manually open each downloaded file and Save-As xlsx files (Note: When I open them in Excel manually is when I'm told there is a file format/extension difference just to be clear)
3 - Run the rest of my program which combs through each new file (the ones created in step 2) and appends all the data to the ultimate output file - This works fine
Is there any way to automate the process or am I going to have to continue to do it manually?
you said in the comment that you opened the file in text editor and saw that it is HTML5.
I would use HTML parser like jsoup to get the data that you need and create a new file using Apache POI.
You can use EasyXLS library. It allows to read HTML files and save as XLSX.
ExcelDocument workbookForXLSX = new ExcelDocument();
for (int i=0; i<fileCount; i++){
ExcelDocument workbookForHTML = new ExcelDocument();
workbookForHTML.easy_LoadHTMLFile(filePath[i]);//or stream to the file
workbookForXLSX.easy_addWorksheet((ExcelWorksheet)workbookForHTML.easy_getSheetAt(0));
workbookForHTML.Dispose();
}
workbookForXLSX.easy_WriteXLSXFile(filePathXLSX);
workbookForXLSX.Dispose();
You can download the Excel library for Java from:
https://www.easyxls.com/java-excel-library
More details about reading HTML files and what HTML tags are supported at:
https://www.easyxls.com/manual/basics/import-from-html-file-format.html

Java save xls file as PDF

I have a simple java program that creates .xls file (open office excel file), and I want to save it as pdf. I saw some answers here but none of them worked for me. Is there a simple and free way to do so?
The file contains only 1 page of .xls
Thanks
Not sure, please provide more details , share code so that the answer can be more precise, but for code perspective it can be achieved via Apache POI.something like below ....
//Instantiate a new workbook with excel file path
Workbook workbook = new Workbook("F:\\FileTemp\\Book1.xls");
//Save the document in Pdf format
workbook.save("F:\\FileTemp\\MyPdfFile.pdf", FileFormatType.PDF);
I have published a library that saves files, and handles everything with one line of code only, you can find it here along with its documentation
Github repository
and the answer to your question is so easy
String path = FileSaver
.get()
.save(fileXls,"file.pdf");

Adding data in Excel file without closing it

I am currently using Apache POI to enter data into Excel file. The only problem is I can not keep the file open if I have to append data to the same file. Are there any specific sample codes which would allow me to do so?
My basic requirement is to fetch Runtime data from a place (this I am able to do) and add it to the Excel sheet while the file is still open.
Any suggestions?
I don't think that this is possible without a C# addon or some kind of macro. You could write a simple C# addon for Excel that connects to your java programm and recieves the realtime data. The addon will write it to the spreadsheat then.

Using java to create excel file with a web query link

I want to create excel file from java (for example with Apache POI) that contains web query with link to my application but I couldn't find any reference for it. Is that possible?
I'll even settle for updating the link of a web query in an existing excel file.
Thank you.
What I finally did is to create an xlsx file with a temp connection. Then I opened the file using a zip reader and modified the connections.xml file inside and then I zipped the file again to xlsx.
Works like a charm.

Automatically update database when new excel sheet is added in a folder

I need to write one java program which monitors a folder containing excel sheets (.xls format) . Once a new excel sheet is added I have to update the database in db2 and move the excel sheet to other place.
Please suggest
It sounds like you need to get notified if files appear in a directory. Java 7 has good support for this, see this article about the Watch Service API. The db2 part, well it isn't clear what you mean by "update", but likely you want to parse the content of the xls file and make some database updates. Apache POI is a good starting place for reading the content of the file into a Java process.

Categories