diff --git a/example/data/input/product_list.xlsx b/example/data/input/product_list.xlsx new file mode 100644 index 0000000..a8e746c Binary files /dev/null and b/example/data/input/product_list.xlsx differ diff --git a/src/main/java/com/northconcepts/datapipeline/examples/cookbook/ReadHyperlinksFromExcel.java b/src/main/java/com/northconcepts/datapipeline/examples/cookbook/ReadHyperlinksFromExcel.java new file mode 100644 index 0000000..df1fa05 --- /dev/null +++ b/src/main/java/com/northconcepts/datapipeline/examples/cookbook/ReadHyperlinksFromExcel.java @@ -0,0 +1,58 @@ +package com.northconcepts.datapipeline.examples.cookbook; + +import java.io.File; + +import com.northconcepts.datapipeline.core.DataReader; +import com.northconcepts.datapipeline.core.DataWriter; +import com.northconcepts.datapipeline.core.Field; +import com.northconcepts.datapipeline.core.ProxyReader; +import com.northconcepts.datapipeline.core.Record; +import com.northconcepts.datapipeline.csv.CSVWriter; +import com.northconcepts.datapipeline.excel.ExcelDocument; +import com.northconcepts.datapipeline.excel.ExcelDocument.ProviderType; +import com.northconcepts.datapipeline.excel.ExcelFieldMetadata; +import com.northconcepts.datapipeline.excel.ExcelHyperlink; +import com.northconcepts.datapipeline.excel.ExcelReader; +import com.northconcepts.datapipeline.job.Job; + +public class ReadHyperlinksFromExcel { + + private static File INPUT_FILE = new File("example/data/input", "product_list.xlsx"); + private static File OUTPUT_FILE = new File("example/data/output", "product_list.csv"); + + public static void main(String[] args) { + ExcelDocument document = new ExcelDocument(ProviderType.POI_XSSF_SAX) + .open(INPUT_FILE); + DataReader reader = new ExcelReader(document) + .setFieldNamesInFirstRow(true) + .setAutoCloseDocument(true) + .setReadMetadata(true); + + reader = new ExcelHyperlinkReader(reader); + + DataWriter writer = new CSVWriter(OUTPUT_FILE); + + Job.run(reader, writer); + } +} + +class ExcelHyperlinkReader extends ProxyReader { + + private ExcelFieldMetadata metadata = new ExcelFieldMetadata(); + + public ExcelHyperlinkReader(DataReader dataReader) { + super(dataReader); + } + + @Override + protected Record interceptRecord(Record record) throws Throwable { + Field field = record.getField("product_name"); + metadata.setField(field); + + ExcelHyperlink excelHyperlink = metadata.getExcelHyperlink(); + record.addField("product_link", excelHyperlink == null ? null : excelHyperlink.getLocation()); + + return super.interceptRecord(record); + } + +}