]> source.dussan.org Git - poi.git/commitdiff
XLS(X) -> CSV: Wrap formatted numbers in quotes if necessary
authorDominik Stadler <centic@apache.org>
Mon, 22 Apr 2024 06:43:20 +0000 (06:43 +0000)
committerDominik Stadler <centic@apache.org>
Mon, 22 Apr 2024 06:43:20 +0000 (06:43 +0000)
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

poi-examples/src/main/java/org/apache/poi/examples/hssf/eventusermodel/XLS2CSVmra.java
poi-examples/src/test/java/org/apache/poi/examples/hssf/eventusermodel/TestXLS2CSVmra.java [new file with mode: 0644]

index 55dc5789152bf640a290bb2ead9fdf7e7ec923f1..d3b879c9f8aadabdb8fec166437935fc23b40999 100644 (file)
@@ -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 (file)
index 0000000..f6cbbc4
--- /dev/null
@@ -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