XML/XSLT trasnsformed data export to excel in Java - java

I have a JSP in which I want to:
make an HTTP request;
get back the XML response;
transform the response using XSL; and
export the transformed data to excel.
But i do not get the entire data in a single HTTP call. Also I have to use two stylesheets for exporting to Excel. The first is for the first page of records, and the other is for all the other pages.
How can one do this in Java?
Thanks

Or use Apache POI, its a powerful API to read and create MS Office documents in Java.

Could you parse the XML into Java objects, then when you have the complete object list simply write the Excel file using JExcelAPI? I've used this for Excel files and it's very simple & useful.

Related

Create an excel worksheet based on a predefined excel template

I have an .xls file which I call it model. Now my probleme is that I want to create over 50 .xls file that have that same format of the model. So properly my question is : how do I create a worksheet (excel file) based on an excel template using java? Is there is any way to do it without copying the style between sheets ??
This example does not use a pre defined template but you can go check out my code and morph it into something worth for you.
https://github.com/ernst223/spread-sheet-exporter
So you can maybe code the style in my class and then use my class for all of your excel files
SpreadSheetExporter spreadSheetExporter = new SpreadSheetExporter(List<Object>, "Filename");
File fileExcel = spreadSheetExporter.getExcel();
as you didn't make any apparent effort before asking i will do the same
excel api

Read data from Web Page and feed it in Excel

Is there any way wherein I can read data from an Excel file(from a Column) change the I/P of the Column Value in a HTTPS URL and then fetch the value from the Web Page and feed the I/P corresponding to the Row in another column which can be used in Shell.
I have googled and found that we can use Selenium and Apache POI to read and write into the Excel however how do I read the data from the Web Page.
You can use node.js to fetch data on the web.
then you use node module
https://www.npmjs.com/package/excel-export
to make it as an excel sheet or
https://www.npmjs.com/package/node_spreadsheet

Converting java file to excel spread sheet

First of all thanks for the previous help.
What I want to do is the following.
Is to collect information through a small program that can capture in Java and writes it to a file which I have done.
Then to have this information passed to a preformatted excel spread sheet.
In the spread sheet it reads this data that is written and displays it in different tables and graphs.
Is this possible to automatically code or do I have to write to a csv file, from the csv file manually copy to an excel template?
If anybody has come across a solution to my problem I would appreciate a steer in the right direction
Hey try ths code in servlet or in JSP.
This will convert simple excel sheet on which you can do further operations..
response.setContentType("application/vnd.ms-excel");
PrintWriter out = response.getWriter();
try {
out.println("\tId \tName");
out.println("\t1\tabc");
out.println("\t2\txyz");
out.println("\t3\tpqr");
} finally {
out.close();
}
here ..
response.setContentType("application/vnd.ms-excel");
specify the excel sheet to be generated..
all "\t" separates new cell in the excel sheet.
On your requirement basis you can also change this code with database or any...
It's a duplicate of How to read and write excel file in java
you can use this API: http://poi.apache.org/
Or if you write one text file with tab separation between fields and save with (.XLS) extension, excel should read it. But off course the better solution is to use Apache POI HSSF.

Passing HTML table from JSP to Java request object

I wanted to know if there is a way in which I can pass an HTML table from my JSP page using the Java request object.
More precisely, this is what my table looks like..
table id="tableid"
thead
tr
th...column1 header.../td
th...column2 header.../td
/tr
/thead
tbody
tr
td...column1 data../td
td..column2 data../td
/tr
/tbody
/table
I've a method in my Java code getExcelVersion(), for which I wanted to pass the above table as an argument.
I actually wanted to pass data from the table(column1 data, column2 data) as an List, ArrayList or anything data type, and not the actual HTML. What I'm basically trying to achieve is something as follows:
I've an HTML table, that I need to export to excel on a button click. For this purpose, I'm using libraries from Apache-POI, and the foll. code block.
HSSFWorkbook workbook = getExcelVersion();
resp.setContentType("application/vnd.ms-excel");
resp.setHeader("Content-Disposition", "attachment; filename=\"contacts.xls\""); workbook.write(resp.getOutputStream());
resp.flushBuffer();
Now, I wanted to know how would I pass table data(column1 data, column2 data) to my getExcelVersion() method.
Thanks,
Pritish.
Apache POI doesn't take a HTML table as input, but just fullworthy Java objects. Just reload the same data in the servlet as you loaded for the initial display in JSP and then feed it to Apache POI instead of forwarding to JSP. If you want to avoid reloading the same data, then you can consider to store it in the session scope, but this has a negative impact on server memory usage and client experience.
At least, I'm assuming that your JSP/Servlet/database code is well written, else it's going to be a lot of refactoring/rewriting work.
Related questions:
How to avoid Java code in JSP files?
Hidden features of JSP/Servlet
Export to Excel (CSV) using JSP/Servlet

Running a JavaScript command from MATLAB to fetch a PDF file

I'm currently writing some MATLAB code to interact with my company's internal reports database. So far I can access the HTML abstract page using code which looks like this:
import com.mathworks.mde.desk.*;
wb=com.mathworks.mde.webbrowser.WebBrowser.createBrowser;
wb.setCurrentLocation(ReportURL(8:end));
pause(1);
s={};
while isempty(s)
s=char(wb.getHtmlText);
pause(.1);
end
desk=MLDesktop.getInstance;
desk.removeClient(wb);
I can extract out various bits of information from the HTML text which ends up in the variable s, however the PDF of the report is accessed via what I believe is a JavaScript command (onClick="gotoFulltext('','[Report Number]')").
Any ideas as to how I execute this JavaScript command and get the contents of the PDF file into a MATLAB variable?
(MATLAB sits on top of Java, so I believe a Java solution would work...)
I think you should take a look at the JavaScript that is being called and see what the final request to the webserver looks like.
You can do this quite easily in Firefox using the FireBug plugin.
https://addons.mozilla.org/en-US/firefox/addon/1843
Once you have found the real server request then you can just request this URL or post to this URL instead of trying to run the JavaScript.
Once you have gotten the correct URL (a la the answer from pjp), your next problem is to "get the contents of the PDF file into a MATLAB variable". Whether or not this is possible may depend on what you mean by "contents"...
If you want to get the raw data in the PDF file, I don't think there is a way currently to do this in MATLAB. The URLREAD function was the first thing I thought of to read content from a URL into a string, but it has this note in the documentation:
s = urlread('url') reads the content
at a URL into the string s. If the
server returns binary data, s will
be unreadable.
Indeed, if you try to read a PDF as in the following example, s contains some text intermingled with mostly garbage:
s = urlread('http://samplepdf.com/sample.pdf');
If you want to get the text from the PDF file, you have some options. First, you can use URLWRITE to save the contents of the URL to a file:
urlwrite('http://samplepdf.com/sample.pdf','temp.pdf');
Then you should be able to use one of two submissions on The MathWorks File Exchange to extract the text from the PDF:
Extract text from a PDF document by Dimitri Shvorob
PDF Reader by Tom Gaudette
If you simply want to view the PDF, you can just open it in Adobe Acrobat with the OPEN function:
open('temp.pdf');
wb=com.mathworks.mde.webbrowser.WebBrowser.createBrowser;
wb.executeScript('javascript:alert(''Some code from a link'')');
desk=com.mathworks.mde.desk.MLDesktop.getInstance;
desk.removeClient(wb);

Categories

Resources