You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ce...@apache.org on 2021/04/06 06:11:01 UTC

svn commit: r1888418 - in /poi/trunk: examples/build.gradle examples/src/main/java/org/apache/poi/examples/xssf/eventusermodel/XLSX2CSV.java ooxml/build.gradle

Author: centic
Date: Tue Apr  6 06:11:01 2021
New Revision: 1888418

URL: http://svn.apache.org/viewvc?rev=1888418&view=rev
Log:
XSLX2CSV: Do not double-encode if the value is already having quotes and escape double-quotes

Most CSV formats use "" (two quotes) to escape a "-character, we should do this in this
example as well to produce files that can be parsed by other CSV processors correctly.

Also cases where the value is already enclosed in quotes should not lead to additional quotes

Add a simple initial test to module "examples" verify basic functionality of XSLX2CSV
as I often rely on it for converting some very large xlsx-files to csv

Modified:
    poi/trunk/examples/build.gradle
    poi/trunk/examples/src/main/java/org/apache/poi/examples/xssf/eventusermodel/XLSX2CSV.java
    poi/trunk/ooxml/build.gradle

Modified: poi/trunk/examples/build.gradle
URL: http://svn.apache.org/viewvc/poi/trunk/examples/build.gradle?rev=1888418&r1=1888417&r2=1888418&view=diff
==============================================================================
--- poi/trunk/examples/build.gradle (original)
+++ poi/trunk/examples/build.gradle Tue Apr  6 06:11:01 2021
@@ -20,4 +20,7 @@ dependencies {
     implementation project(':scratchpad')
 
     implementation "org.apache.logging.log4j:log4j-core:${log4jVersion}"
+
+	testImplementation project(path: ':ooxml', configuration: 'tests')
+	testImplementation project(path: ':main', configuration: 'tests')
 }

Modified: poi/trunk/examples/src/main/java/org/apache/poi/examples/xssf/eventusermodel/XLSX2CSV.java
URL: http://svn.apache.org/viewvc/poi/trunk/examples/src/main/java/org/apache/poi/examples/xssf/eventusermodel/XLSX2CSV.java?rev=1888418&r1=1888417&r2=1888418&view=diff
==============================================================================
--- poi/trunk/examples/src/main/java/org/apache/poi/examples/xssf/eventusermodel/XLSX2CSV.java (original)
+++ poi/trunk/examples/src/main/java/org/apache/poi/examples/xssf/eventusermodel/XLSX2CSV.java Tue Apr  6 06:11:01 2021
@@ -128,6 +128,12 @@ public class XLSX2CSV {
             for (int i=0; i<missedCols; i++) {
                 output.append(',');
             }
+
+            // no need to append anything if we do not have a value
+            if (formattedValue == null) {
+                return;
+            }
+
             currentCol = thisCol;
 
             // Number or string?
@@ -136,8 +142,14 @@ public class XLSX2CSV {
                 Double.parseDouble(formattedValue);
                 output.append(formattedValue);
             } catch (Exception e) {
+                // let's remove quotes if they are already there
+                if (formattedValue.startsWith("\"") && formattedValue.endsWith("\"")) {
+                    formattedValue = formattedValue.substring(1, formattedValue.length()-1);
+                }
+
                 output.append('"');
-                output.append(formattedValue);
+                // encode double-quote with two double-quotes to produce a valid CSV format
+                output.append(formattedValue.replace("\"", "\"\""));
                 output.append('"');
             }
         }

Modified: poi/trunk/ooxml/build.gradle
URL: http://svn.apache.org/viewvc/poi/trunk/ooxml/build.gradle?rev=1888418&r1=1888417&r2=1888418&view=diff
==============================================================================
--- poi/trunk/ooxml/build.gradle (original)
+++ poi/trunk/ooxml/build.gradle Tue Apr  6 06:11:01 2021
@@ -50,7 +50,17 @@ jar {
     }
 }
 
-test {
-    // for some reason catching the OOM does not work when run from Gradle
-    exclude '**/MemoryUsage.class'
-}
\ No newline at end of file
+// Create a separate jar for test-code to depend on it in other projects
+// See http://stackoverflow.com/questions/5144325/gradle-test-dependency
+task testJar(type: Jar, dependsOn: testClasses) {
+	baseName = "test-${project.archivesBaseName}"
+	from sourceSets.test.output
+}
+
+configurations {
+	tests
+}
+
+artifacts {
+	tests testJar
+}



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