From 1c1bd6e5854c14ab6650e50c9d4540a360ac34a5 Mon Sep 17 00:00:00 2001 From: Hassan Ahmad Date: Fri, 9 Jul 2021 17:31:10 +0200 Subject: [PATCH] Implement specificWorkbookSheet method --- .../hssf/converter/ExcelToHtmlConverter.java | 22 +++++++++++++++++-- .../converter/TestExcelConverterSuite.java | 10 +++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java b/poi-scratchpad/src/main/java/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java index 7c0fd298019..515e7649669 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java @@ -136,7 +136,7 @@ public static Document process(InputStream xlsStream) throws IOException, Parser public static Document process(HSSFWorkbook workbook) throws IOException, ParserConfigurationException { ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter( XMLHelper.newDocumentBuilder().newDocument()); - excelToHtmlConverter.processWorkbook(workbook); + excelToHtmlConverter.processWorkbookSheet(workbook); return excelToHtmlConverter.getDocument(); } @@ -668,7 +668,7 @@ protected void processSheetHeader(Element htmlBody, HSSFSheet sheet) { htmlBody.appendChild(h2); } - public void processWorkbook(HSSFWorkbook workbook) { + public void processWorkbookSheet(HSSFWorkbook workbook) { final SummaryInformation summaryInformation = workbook .getSummaryInformation(); if (summaryInformation != null) { @@ -692,6 +692,24 @@ public void processWorkbook(HSSFWorkbook workbook) { htmlDocumentFacade.updateStylesheet(); } + public void processWorkbookSheet(HSSFWorkbook workbook, int sheetNumber) { + SummaryInformation summaryInformation = workbook.getSummaryInformation(); + + if (summaryInformation != null) { + this.processDocumentInformation(summaryInformation); + } + + if (workbook.getNumberOfSheets() < sheetNumber) { + throw new IllegalArgumentException("invalid sheet number: " + sheetNumber); + } + + HSSFSheet sheet = workbook.getSheetAt(sheetNumber); + + this.processSheet(sheet); + + htmlDocumentFacade.updateStylesheet(); + } + public void setCssClassPrefixCell(String cssClassPrefixCell) { this.cssClassPrefixCell = cssClassPrefixCell; } diff --git a/poi-scratchpad/src/test/java/org/apache/poi/hssf/converter/TestExcelConverterSuite.java b/poi-scratchpad/src/test/java/org/apache/poi/hssf/converter/TestExcelConverterSuite.java index 927d54e88ec..d6169612a9e 100644 --- a/poi-scratchpad/src/test/java/org/apache/poi/hssf/converter/TestExcelConverterSuite.java +++ b/poi-scratchpad/src/test/java/org/apache/poi/hssf/converter/TestExcelConverterSuite.java @@ -32,6 +32,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import javax.xml.transform.stream.StreamResult; import org.apache.poi.POIDataSamples; +import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.util.XMLHelper; import org.junit.jupiter.params.ParameterizedTest; @@ -64,6 +65,7 @@ public static Stream files() { void testFo(File child) throws Exception { HSSFWorkbook workbook; + int sheetNumber = 0; try { workbook = AbstractExcelUtils.loadXls( child ); } catch ( Exception exc ) { @@ -73,7 +75,8 @@ void testFo(File child) throws Exception ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter( XMLHelper.newDocumentBuilder().newDocument() ); - excelToHtmlConverter.processWorkbook( workbook ); + excelToHtmlConverter.processWorkbookSheet( workbook); + excelToHtmlConverter.processWorkbookSheet( workbook, sheetNumber); StringWriter stringWriter = new StringWriter(); @@ -90,6 +93,8 @@ void testFo(File child) throws Exception void testHtml(File child) throws Exception { HSSFWorkbook workbook; + int sheetNumber = 0; // + try { workbook = AbstractExcelUtils.loadXls( child ); } catch ( Exception exc ) { @@ -99,7 +104,8 @@ void testHtml(File child) throws Exception ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter( XMLHelper.newDocumentBuilder().newDocument() ); - excelToHtmlConverter.processWorkbook( workbook ); + excelToHtmlConverter.processWorkbookSheet( workbook); + excelToHtmlConverter.processWorkbookSheet( workbook, sheetNumber); StringWriter stringWriter = new StringWriter();