Author: centic
Date: Mon Apr 22 06:43:20 2024
New Revision: 1917257

URL: http://svn.apache.org/viewvc?rev=1917257&view=rev
Log:
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

Added:
    poi/trunk/poi-examples/src/test/java/org/apache/poi/examples/
    poi/trunk/poi-examples/src/test/java/org/apache/poi/examples/hssf/
    
poi/trunk/poi-examples/src/test/java/org/apache/poi/examples/hssf/eventusermodel/
    
poi/trunk/poi-examples/src/test/java/org/apache/poi/examples/hssf/eventusermodel/TestXLS2CSVmra.java
Modified:
    
poi/trunk/poi-examples/src/main/java/org/apache/poi/examples/hssf/eventusermodel/XLS2CSVmra.java

Modified: 
poi/trunk/poi-examples/src/main/java/org/apache/poi/examples/hssf/eventusermodel/XLS2CSVmra.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-examples/src/main/java/org/apache/poi/examples/hssf/eventusermodel/XLS2CSVmra.java?rev=1917257&r1=1917256&r2=1917257&view=diff
==============================================================================
--- 
poi/trunk/poi-examples/src/main/java/org/apache/poi/examples/hssf/eventusermodel/XLS2CSVmra.java
 (original)
+++ 
poi/trunk/poi-examples/src/main/java/org/apache/poi/examples/hssf/eventusermodel/XLS2CSVmra.java
 Mon Apr 22 06:43:20 2024
@@ -253,6 +253,10 @@ public class XLS2CSVmra implements HSSFL
 
             // Format
             thisStr = formatListener.formatNumberDateCell(numrec);
+            if (thisStr.contains(",")) {
+                thisStr = '"' + thisStr + '"';
+            }
+
             break;
         case RKRecord.sid:
             RKRecord rkrec = (RKRecord) record;

Added: 
poi/trunk/poi-examples/src/test/java/org/apache/poi/examples/hssf/eventusermodel/TestXLS2CSVmra.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-examples/src/test/java/org/apache/poi/examples/hssf/eventusermodel/TestXLS2CSVmra.java?rev=1917257&view=auto
==============================================================================
--- 
poi/trunk/poi-examples/src/test/java/org/apache/poi/examples/hssf/eventusermodel/TestXLS2CSVmra.java
 (added)
+++ 
poi/trunk/poi-examples/src/test/java/org/apache/poi/examples/hssf/eventusermodel/TestXLS2CSVmra.java
 Mon Apr 22 06:43:20 2024
@@ -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



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@poi.apache.org
For additional commands, e-mail: commits-h...@poi.apache.org

Reply via email to