You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by ProgramadorJunior <ne...@gmail.com> on 2013/09/03 12:55:57 UTC
Apache POI Relative file hyperlink
I'm trying to create an hyperlink to a file using APACHE POI XSSF and a
relative path. When opening the .xlsx file created with *Microsft Excel* the
relative path is modified and it is not well linked. Microsoft Excel adds
some '../../' in front of the path.
I tried opening it with OpenOffice and it works OK. Also I tried it with
HSSF and it works both in Microsoft Excel and OpenOffice. Any idea why is
this happening? This is an example code:
import java.io.File;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.apache.poi.hssf.usermodel.HSSFHyperlink;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelHyperlinks {
public static void main(String[] args) throws Exception{
/* Create Workbook and Worksheet */
XSSFWorkbook my_workbook = new XSSFWorkbook();
XSSFSheet my_sheet = my_workbook.createSheet("Cell Hyperlink");
CreationHelper createHelper = my_workbook.getCreationHelper();
Hyperlink
url_link=createHelper.createHyperlink(HSSFHyperlink.LINK_URL);
Hyperlink
file_link=createHelper.createHyperlink(HSSFHyperlink.LINK_FILE);
Hyperlink
email_link=createHelper.createHyperlink(HSSFHyperlink.LINK_EMAIL);
/* Define the data for these hyperlinks */
url_link.setAddress("http://www.google.com");
try {
file_link.setAddress((URLEncoder.encode("Encuesta de
bienvenida","UTF-8")+"/"+"E18").replace("\\", "/").replace("+","%20"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("addres--> " + file_link.getAddress());
email_link.setAddress("mailto:test@gmail.com");
/* Attach these links to cells */
Row row = my_sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Take me to Google");
cell.setHyperlink(url_link);
row = my_sheet.createRow(1);
cell = row.createCell(1);
cell.setCellValue("Click to Open the file");
cell.setHyperlink(file_link);
row = my_sheet.createRow(2);
cell = row.createCell(2);
cell.setCellValue("Send an Email");
cell.setHyperlink(email_link);
/* Write changes to the workbook */
FileOutputStream out = new FileOutputStream(new
File("C:/cell_hyperlink_example.xlsx"));
my_workbook.write(out);
out.close();
}
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Apache-POI-Relative-file-hyperlink-tp5713703.html
Sent from the POI - Dev mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org
Re: Apache POI Relative file hyperlink
Posted by ProgramadorJunior <ne...@gmail.com>.
I found another XML that I think is the one you wanted to look at.
Inside the .xlsx in xl/worksheets/_rels there's the file *sheet1.xml.rels*
and one of its relationships is
/<Relationship Id="rId2"
Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
TargetMode="External" Target="Encuesta%20de%20bienvenida/E18"/>/
Where the path indicated in the Target attrobute is OK.
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Apache-POI-Relative-file-hyperlink-tp5713703p5713706.html
Sent from the POI - Dev mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org
Re: Apache POI Relative file hyperlink
Posted by ProgramadorJunior <ne...@gmail.com>.
When I consult the link with Microsoft Excel the path is
'../../../../../Encuesta%20de%20bienvenida/E18'. It has added all the dots
before the path I wrote in the code. If I open the file with OpenOffice the
path appears just as I wrote it, without the dots.
Sorry this is my first time working with Apache POI and Excel and I really
don't understand anything from the XML file you mentioned.
I unzipped the .xlsx and this is the whole content of the file inside the
_rels folder:
*.rels*
<?xml version="1.0" encoding="UTF-8"?>
<Relationships
xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1"
Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
Target="xl/workbook.xml"/>
<Relationship Id="rId2"
Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties"
Target="docProps/app.xml"/>
<Relationship Id="rId3"
Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties"
Target="docProps/core.xml"/>
</Relationships>
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Apache-POI-Relative-file-hyperlink-tp5713703p5713705.html
Sent from the POI - Dev mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org
Re: Apache POI Relative file hyperlink
Posted by Nick Burch <ap...@gagravarr.org>.
On Tue, 3 Sep 2013, ProgramadorJunior wrote:
> I'm trying to create an hyperlink to a file using APACHE POI XSSF and a
> relative path. When opening the .xlsx file created with *Microsft Excel* the
> relative path is modified and it is not well linked. Microsoft Excel adds
> some '../../' in front of the path.
What does Excel write for such a file? What does POI write? How do they
differ in how both Excel and POI see each other's ones? If you unzip the
.xlsx and look at the raw XML in the _rels file, what really got written?
Nick
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org