How to print excel file from java? - java

First Question on the community!
Let me explain a little the question.
I have a user interface, that allows me to create an excel file. And I want to add at that ui the a "print Document" button. This button is supposed to allow the user to print that excel document. So what I need is a way to call the windows printing dialog for that file, so the user can print the file.
I have read http://docs.oracle.com/javase/tutorial/2d/printing/printable.html, But I can not find the way of telling the printing dialog what file to print.
Thanks in advance!

I think Apache POI is the best thing for you. Code syntax is like
POIFSFileSystem filesystem = new POIFSFileSystem(new FileInputStream(file));
HSSFWorkbook wrkbook = new HSSFWorkbook(filesystem );
HSSFSheet sheet = wrkbook.getSheetAt(0);
HSSFRow row;
HSSFCell cell;
//Keep on doing your manipulation.
More about Apache POI.

Related

Selenium - store web element id in a spreadsheet, call the id from the spreadsheet in test?

So I'm trying to store a web element such as id in an excel spreadsheet, I then want to call the id from the sheet and place it in a test.
Would this work? The theory is then that others with less tech knowledge can modify tests within my workplace without editing code.
I am using the following code to read from the spreadsheet and place the cell data into a string.
File src=new File("C:\\Users\\Admin\\Documents\\ExcelData\\TestData.xlsx");
FileInputStream fis=new FileInputStream(src);
XSSFWorkbook wb=new XSSFWorkbook(fis);
XSSFSheet sheet1=wb.getSheetAt(0);
String data0=sheet1.getRow(0).getCell(0).getStringCellValue();
The difficulty I am having is when testing for example.
driver.findElement(By.name("q")).sendKeys("Java");
if I replace this with
driver.findElement(By.name(data0).sendKeys("Java");
this will not work.
Yes, you can get the input data from the external sheet as you asked.
This can be done using Apache POI Jars support.
Hope you are coding in Java.
The below link gives you enough code to your requirement. ->
Apache POI Examples
Code to read the data from Excel.
File file= new File(PATH);
FileInputStream inputStream = new FileInputStream(file);
Workbook workbook =new XSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheet(SHEETNAME);
String[][] number=new String[10][10]; // temp array
for(int i=0;i<rowCount+1;i++)
{
Row row=s.getRow(i);
for(int j=0;j<=row.getLastCellNum();j++)
{
number[i][j]= row.getCell(j).toString(); // works only for cells of string type
}
}
Then you fetch data from number array.

POI overriding the old excel sheet with new excel sheet

if i create a first sheet it is working fine. if i call the same method to create new sheet it will over ride the previous sheet.
After couple of method calls, it is showing only one sheet(last sheet).
In my code every time i am creating sheet only, not workbook every time.
could any body please explain.how to code it.
FileOutputStream fos = new FileOutputStream(outputString)
I am assuming you are using this. When you add the boolean "true" to this syntax like this:
FileOutputStream fos = new FileOutputStream(outputString, true);
Data will get added to the existing data. You should give it a try.

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.

Text Object not supported. Java excel API

I have this piece of code to read from an xls file:
fileName = "...."
WorkbookSettings settings = new WorkbookSettings();
settings.setEncoding("Cp1252");
System.out.println("BEFORE");
Workbook w = Workbook.getWorkbook(new File(fileName), settings);
Sheet sheet = w.getSheet(1);
System.out.println("AFTER");
This is what I get in the console:
BEFORE
Warning: Text Object on sheet "Detalle" not supported - omitting
jxl.common.AssertionFailed
at jxl.common.Assert.verify(Assert.java:37)
at jxl.read.biff.SheetReader.handleObjectRecord(SheetReader.java:1811)
at jxl.read.biff.SheetReader.read(SheetReader.java:1059)
at jxl.read.biff.SheetImpl.readSheet(SheetImpl.java:716)
at jxl.read.biff.WorkbookParser.getSheet(WorkbookParser.java:257)
at MapMovInfoResource.postService(MapMovInfoResource.java:77)
The problem comes when I try to open the second sheet in that file. When I use the first sheet (w.getSheet(0)), it works fine.
Any ideas on how to solve this?
enter image description here
Its seem you have some value in excel which are not text. Please check excel value like in image, and if and special quotes are available then remove that and try again.

Edit existing excel files using jxl api / Apache POI

I am interested and would like to learn more about java , how to write into existing excel sheets / manipulating the existing data. I was wondering if you could give me an idea on how to edit an existing excel file and save it using the jxl api / Apache POI
or perhaps give me a sample program on how to edit some data in an existing excel file and then save it
Thanks in advance !!
The tutorials here are very helpful and well-written. They use an external JAR developed by the Apache POI project.
Here's an simple example of editing one cell:
InputStream inp = new FileInputStream("wb.xls");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt([sheet index]);
Row row = sheet.getRow([row index]);
Cell cell = row.getCell([cell index]);
String cellContents = cell.getStringCellValue();
//Modify the cellContents here
// Write the output to a file
cell.setCellValue(cellContents);
FileOutputStream fileOut = new FileOutputStream("wb.xls");
wb.write(fileOut);
fileOut.close();
Hope it helps
One very important tip that I learned the hard way.
Open the OutputStream only after you have completed writing to your excel workbook. Zabbala's example is spot on and shows this correctly. If you open the OutputStream any earlier, your changes would not be written to the file after your program exits and you would be scratching your head as I did.
I refresh the formulas with another tab for this I use the next sentence
HSSFSheet worksheetse = workbook.getSheetAt(0);
worksheetse.setForceFormulaRecalculation(true);
but it's necesary that you apply the method setForceFormulaRecalculation for all the tabs that have the formulas.
Sorry for my English
Hello i have the same problem than neXGen. But strangely if i open the file with openoffice, it works!
Edit: perhaps i found a solution, put this after changing the values:
HSSFFormulaEvaluator.evaluateAllFormulaCells(workbook);

Categories

Resources