Java save xls file as PDF - java

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");

Related

Create automation to remove workbook password from .xlsx files

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.

How to mantain formatting in Excel with JExcel

I'm using JExcel API to open and edit an existing .xls file.
First problem was that I can't directly edit the opened xls file. I need to make a copy of that first. (Question1: It is possible to edit the opened workbook or not?)
My solution to that based on some internet information was:
Workbook workbook=Workbook.getWorkbook(inputfile);
...
WritableWorkbook wb=Workbook.createWorkbook(inputfile, workbook);
wb.write();
wb.close();
//Edit cells in wb
This works but the updated version of the xls has the wrong formatting.
So, Question 2: Is there any way to keep the format when I copy workbooks?
Well, I solved it by using Apache API.
Even though it is not as well documented as JExcel it seems to do its job better.

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.

Read excel file extension from struts control <s:file>

I want read contents of excel file selected using
. I am using POI 3.8. The problem is I am not able to identify
if this file is xls or xlsx. Please help
Why do you need to know?
Just use WorkbookFactory to load the excel file, and it'll autodetect for you. You then don't need to know, and your code remains completely generic between the two file formats.

Generate and return docx file to client

I have an app that generates a docx file base on user input. It uses Apache POI to generate the docx file and I can get the FileOutputStream from that, the document opens perfectly on a local machine when I write it to a file.
The webapp is using Dojo xhrPost to send the necessary data to the server to generate the document. What I am wondering is how I get the docx file to the client.
I know I can do it be creating a temp file and passing the location of that file to the client to download, but I would think there would be a way to do it by piping the FileOutputStream straight to the client, which would be much cleaner.
Any suggestions?
The answer from Mr Shiny in this SO question has an example streaming an excel file, should be very similar for a docx:
How can I get an Input Stream from HSSFWorkbook Object
Except that a docx content type should, probably, be application/vnd.ms-word

Categories