aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDominik Stadler <centic@apache.org>2024-04-22 06:43:20 +0000
committerDominik Stadler <centic@apache.org>2024-04-22 06:43:20 +0000
commit601c5e2671243db42306de8fe94c3ebfb6024408 (patch)
tree044b8c72c575dc0456a442ce264351979c753ef3
parent51242877e59ec528a99ba66903a7c3b5cb524b6c (diff)
downloadpoi-601c5e2671243db42306de8fe94c3ebfb6024408.tar.gz
poi-601c5e2671243db42306de8fe94c3ebfb6024408.zip
XLS(X) -> CSV: Wrap formatted numbers in quotes if necessary
e.g. German locale uses "comma" instead of point, e.g. 1,23 instead of 1.23 so we may need to quote formatted numbers git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1917257 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--poi-examples/src/main/java/org/apache/poi/examples/hssf/eventusermodel/XLS2CSVmra.java4
-rw-r--r--poi-examples/src/test/java/org/apache/poi/examples/hssf/eventusermodel/TestXLS2CSVmra.java111
2 files changed, 115 insertions, 0 deletions
diff --git a/poi-examples/src/main/java/org/apache/poi/examples/hssf/eventusermodel/XLS2CSVmra.java b/poi-examples/src/main/java/org/apache/poi/examples/hssf/eventusermodel/XLS2CSVmra.java
index 55dc578915..d3b879c9f8 100644
--- a/poi-examples/src/main/java/org/apache/poi/examples/hssf/eventusermodel/XLS2CSVmra.java
+++ b/poi-examples/src/main/java/org/apache/poi/examples/hssf/eventusermodel/XLS2CSVmra.java
@@ -253,6 +253,10 @@ public class XLS2CSVmra implements HSSFListener {
// Format
thisStr = formatListener.formatNumberDateCell(numrec);
+ if (thisStr.contains(",")) {
+ thisStr = '"' + thisStr + '"';
+ }
+
break;
case RKRecord.sid:
RKRecord rkrec = (RKRecord) record;
diff --git a/poi-examples/src/test/java/org/apache/poi/examples/hssf/eventusermodel/TestXLS2CSVmra.java b/poi-examples/src/test/java/org/apache/poi/examples/hssf/eventusermodel/TestXLS2CSVmra.java
new file mode 100644
index 0000000000..f6cbbc4d67
--- /dev/null
+++ b/poi-examples/src/test/java/org/apache/poi/examples/hssf/eventusermodel/TestXLS2CSVmra.java
@@ -0,0 +1,111 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.examples.hssf.eventusermodel;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.poi.hssf.HSSFTestDataSamples;
+import org.apache.poi.hssf.record.NumberRecord;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.junit.jupiter.api.Test;
+
+class TestXLS2CSVmra {
+ @Test
+ void test() throws Exception {
+ XLS2CSVmra.main(new String[] { HSSFTestDataSamples.getSampleFile("SampleSS.xls").getAbsolutePath() });
+ }
+
+ @Test
+ void testWithMinCols() throws Exception {
+ XLS2CSVmra.main(new String[] { HSSFTestDataSamples.getSampleFile("SampleSS.xls").getAbsolutePath(), "100" });
+ }
+
+ @Test
+ void testProcess() throws IOException {
+ ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+ PrintStream out = new PrintStream(outStream);
+ XLS2CSVmra cvs = new XLS2CSVmra(
+ new POIFSFileSystem(new FileInputStream(HSSFTestDataSamples.getSampleFile("SampleSS.xls").getAbsolutePath())),
+ out, -1);
+
+ cvs.process();
+
+ outStream.flush();
+
+ assertEquals("\n"
+ + "First Sheet [1]:\n"
+ + "\"Test spreadsheet\"\n"
+ + "\"2nd row\",\"2nd row 2nd column\"\n"
+ + "\n"
+ + "\"This one is red\"\n"
+ + "\n"
+ + "Sheet Number 2 [2]:\n"
+ + "\"Start of 2nd sheet\"\n"
+ + "\"Sheet 2 row 2\"\n"
+ + "\n"
+ + "\"I'm in bold blue, on a yellow background\"\n"
+ + "\n"
+ + "\"cb=1\",\"cb=10\",\"cb=2\",\"cb=sum\"\n"
+ + "1,10,2,13\n"
+ + "\n"
+ + "Sheet3 [3]:\n", new String(outStream.toByteArray(), StandardCharsets.UTF_8));
+ }
+
+ @Test
+ void testProcessNumberRecord() throws IOException {
+ ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+ PrintStream out = new PrintStream(outStream);
+ XLS2CSVmra cvs = new XLS2CSVmra(
+ new POIFSFileSystem(new FileInputStream(HSSFTestDataSamples.getSampleFile("empty.xls").getAbsolutePath())),
+ out, -1);
+
+ // need to call process() first to initialize members
+ cvs.process();
+
+ outStream.flush();
+
+ assertEquals("\n"
+ + "Лист1 [1]:\n"
+ + "\n"
+ + "Лист2 [2]:\n"
+ + "\n"
+ + "Лист3 [3]:\n", new String(outStream.toByteArray(), StandardCharsets.UTF_8));
+
+
+ NumberRecord record = new NumberRecord();
+ record.setValue(1.243);
+
+ cvs.processRecord(record);
+
+ outStream.flush();
+
+ assertEquals("\n"
+ + "Лист1 [1]:\n"
+ + "\n"
+ + "Лист2 [2]:\n"
+ + "\n"
+ + "Лист3 [3]:\n"
+ + "1.243", new String(outStream.toByteArray(), StandardCharsets.UTF_8));
+ }
+} \ No newline at end of file