Skip to content

Commit d36e2f3

Browse files
author
rzymek
committed
fixes #528
1 parent b0d22d1 commit d36e2f3

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

fastexcel-writer/src/main/java/org/dhatim/fastexcel/HyperLink.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.dhatim.fastexcel;
22

3+
import java.util.Objects;
4+
35
public class HyperLink {
46
private final String displayStr;
57

@@ -53,14 +55,23 @@ public HyperLink(String linkStr, String displayStr) {
5355
this.hyperLinkType = hyperLinkType;
5456
}
5557

58+
@Override
59+
public boolean equals(Object o) {
60+
if (o == null || getClass() != o.getClass()) return false;
61+
HyperLink hyperLink = (HyperLink) o;
62+
return Objects.equals(displayStr, hyperLink.displayStr)
63+
&& Objects.equals(linkStr, hyperLink.linkStr)
64+
&& hyperLinkType == hyperLink.hyperLinkType;
65+
}
66+
5667
@Override
5768
public int hashCode() {
58-
return super.hashCode();
69+
return Objects.hash(displayStr, linkStr, hyperLinkType);
5970
}
6071

6172
@Override
62-
public boolean equals(Object obj) {
63-
return super.equals(obj);
73+
public String toString() {
74+
return linkStr;
6475
}
6576

6677
public String getDisplayStr() {

fastexcel-writer/src/main/java/org/dhatim/fastexcel/Relationships.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.ArrayList;
55
import java.util.concurrent.atomic.AtomicInteger;
66

7+
import static org.dhatim.fastexcel.XmlEscapeHelper.escape;
8+
79
public class Relationships {
810

911
private static final String TYPE_OF_HYPERLINK= "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink";
@@ -66,7 +68,7 @@ void write( Writer relsWr) throws IOException {
6668
relsWr.append("<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">");
6769
for (Relationship rs : relationship) {
6870
relsWr.append("<Relationship Id=\"" + rs.id + "\" ");
69-
relsWr.append("Target=\"" + rs.target + "\" ");
71+
relsWr.append("Target=\"" + escape(rs.target) + "\" ");
7072
if (rs.targetMode!=null) {
7173
relsWr.append("TargetMode=\""+rs.targetMode+"\" " );
7274
}

fastexcel-writer/src/main/java/org/dhatim/fastexcel/Worksheet.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.util.*;
2424
import java.util.stream.IntStream;
2525

26+
import static org.dhatim.fastexcel.XmlEscapeHelper.escape;
27+
2628
/**
2729
* A worksheet is a set of cells.
2830
*/
@@ -930,7 +932,7 @@ public void finish() throws IOException {
930932
String rId = relationships.setHyperLinkRels(hyperLink.getLinkStr(), "External");
931933
writer.append("r:id=\"" + rId +"\" ");
932934
}else{
933-
writer.append("location=\"").append(hyperLink.getLinkStr()).append("\"");
935+
writer.append("location=\"").append(escape(hyperLink.getLinkStr())).append("\"");
934936
}
935937
writer.append("/>");
936938
}

fastexcel-writer/src/test/java/org/dhatim/fastexcel/PoiCompatibilityTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ void singleWorksheet() throws Exception {
4444
int intValue = 2_016;
4545
long longValue = 2_016_000_000_000L;
4646
BigDecimal bigDecimalValue = BigDecimal.TEN;
47+
HyperLink hyperlinkValue = new HyperLink("https://github.com/dhatim/fastexcel?a=1&b=2");
4748
byte[] data = writeWorkbook(wb -> {
4849
Worksheet ws = wb.newWorksheet(sheetName);
4950
ws.paperSize(PaperSize.A4_PAPER);
@@ -63,6 +64,7 @@ void singleWorksheet() throws Exception {
6364
ws.value(i, i++, bigDecimalValue);
6465
ws.value(i, i++, Boolean.TRUE);
6566
ws.value(i, i++, Boolean.FALSE);
67+
ws.hyperlink(i,i++, hyperlinkValue);
6668
try {
6769
ws.close();
6870
} catch (IOException ex) {
@@ -101,6 +103,7 @@ void singleWorksheet() throws Exception {
101103
assertThat(new BigDecimal(xws.getRow(i).getCell(i++).getRawValue())).isEqualTo(bigDecimalValue);
102104
assertThat(xws.getRow(i).getCell(i++).getBooleanCellValue()).isTrue();
103105
assertThat(xws.getRow(i).getCell(i++).getBooleanCellValue()).isFalse();
106+
assertThat(xws.getRow(i).getCell(i++).getHyperlink().getAddress()).isEqualTo(hyperlinkValue.getLinkStr());
104107
}
105108

106109

0 commit comments

Comments
 (0)