Creating a hyperlink using apache poi - java

I am trying to create a hyperlink using the following code
CreationHelper createHelper = wb.getCreationHelper();
cell.setCellValue("Click Here");
Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_FILE);
File f = new File("C:\\Test\\1.pdf");
link.setAddress(f.getCanonicalPath());
cell.setHyperlink((org.apache.poi.ss.usermodel.Hyperlink) link);
It works fine and it adds a link Click Here to the cell
But how i can set a partial text and a link using same type of code, I mean the link need to be like your file is here, where only here is the link

As far as I know I don't think it is possible, as it is not supported by Excel as well. To achieve that in Excel too there is no straight way and you have to do some tricks to achieve that. Something that is not supported right from Excel cannot be supported by Apache POI too.

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

Pivot table refresh and save code in aspose cells for java corrupts excel file

I am using aspose-cells-8.7.2-java. When I refresh the pivot table and save it, the excel file is getting corrupted. When I try to open the excel file I am getting the alert message as below :
"Excel found unreadable content in 'Book1.xlsx'.Do you want to recover the contents of this workbook?If you trust the source file of this workbook, click yes."
The code is as below :
Workbook wb = new Workbook("Book1.xlsx");
PivotTable pt = wb.getWorksheets().get(1).getPivotTables().get(0);
pt.refreshData();
pt.calculateData();
wb.save("Book1.xlsx");
Any help ?
I found this thread where the same issue is logged as a ticket :
http://www.aspose.com/community/forums/thread/683715/aspose.cells-generates-a-corrupted-xlsx-file-excel-2007-fails-to-open.aspx.
Is this issue solved?
I'm afraid the logged issue is not resolved yet. By the way, do you use similar Excel file or yours template file "Book1.xlsx" is different. Moreover, your issue can be template specific (if you are using different file) and might have different scenarios, so we need your template "Book1.xlsx" file to properly evaluate your issue on our end. We recommend you to kindly create a separate thread in Aspose.Cells forum with your template Excel file, we will evaluate your issue and help you better there.
I am working as Support developer/ Evangelist at Aspose.

hyperlink to a different sheet within the same workbook using apache poi

I am using Apache POI to generate an excel workbook containing multiple sheets. I want to create a hyperlink from one sheet to another. How to accomplish this ? There are solutions I found for establishing hyperlinks to websites, even other excel files but not to other sheets within the same workbook. Does Apache POI allow us to do this ?
Yes, Apache POI allows you to create a hyperlink to another sheet in the same workbook. According to the Apache POI Quick Guide:
cell = sheet.createRow(3).createCell((short)0);
cell.setCellValue("Worksheet Link");
Hyperlink link2 = createHelper.createHyperlink(Hyperlink.LINK_DOCUMENT);
link2.setAddress("'Target Sheet'!A1");
cell.setHyperlink(link2);
cell.setCellStyle(hlink_style);
This creates a cell with a hyperlink in it of type LINK_DOCUMENT, with an address of a cell reference, which can be in the same sheet or another sheet. Then it sets the cell style to an existing cell style (created earlier in the code sample), so that it looks like a hyperlink.
You can also link to a cell address in another worksheet using POI.
Create your link as type Hyperlink.LINK_DOCUMENT. Then send setHyperlink() a string in this format :
file:/E:/PROJECTS/SomePrj/ExcelFileName.xlsx#'ExcelSheetName'!B5
that will work - at least for XSSF.
Use aFile.toURI().toString() to build the filepart before the '#' character. Don't forget to add both # , ' and ! characters (exactly as in my example) or it will not work.
I do not see this technique demonstrated in the (otherwise very good) POI documentation.
I found the right way. Tested on poi 3.16 and solved the annoying problem of not allowing space in the link(sheet name can have space). you will get error if you try
link.setAddress("ExcelWrite.xlsx#'Sheet 1'!A2");
RIGHT CODE:
cell = sheet.createRow(1).createCell(0);
cell.setCellValue("File Link");
XSSFHyperlink link = (XSSFHyperlink) createHelper.createHyperlink(HyperlinkType.FILE);
link.setAddress("ExcelWrite.xlsx");
link.setLocation("'Sheet 1'!A2");
cell.setHyperlink(link);
cell.setCellStyle(hlink_style);

Set page view mode in excel file using apache poi

Excel has different modes for viewing a sheet: Normal, Page Layout, Page Break Preview. (In excel 2010: in the view tab). The view mode is saved seperately for each sheet in a workbook and is restored when opened again.
I am trying to find a way to set a view mode using Either HSSF or XSSF.
In the old binary format, finding the answer seems quite impossible unfortunately.
In 2007+ OOXML format diffing does give the basic answer, looking at xl/worksheets/sheet1.xml
In normal view:
<sheetViews>
<sheetView rightToLeft="1" tabSelected="1" zoomScaleNormal="100" workbookViewId="0">
</sheetViews>
In page layout view:
<sheetViews>
<sheetView rightToLeft="1" tabSelected="1" view="pageLayout" zoomScaleNormal="100" workbookViewId="0"/>
</sheetViews>
That is the second tag in each sheet. Is there any XSSF API option to edit that attribute? (or the only solution to the problem would be unpacking the file, editing it and repacking)
Thanks!
XSSF doesn't expose this directly, but you can get at it if you want
From the XSSFSheet object, call getCTWorksheet to get the low level XML object backing the sheet. CTWorksheet provides a getSheetViews method. You'll like want something like:
CTSheetView view = sheet.getCTWorksheet().getSheetViews().getSheetViewArray(0);
view.setView(STSheetViewType.PAGE_LAYOUT);

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