Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions docs/modules/ROOT/pages/dataformats.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,113 @@ local params = {
}
------------------------

## XLSX Format

### MIME types and identifiers
* `application/xlsx`
### `write`

Creates a XLSX out of an array of JSON objects. The keys of JSON object values are used as headers.

*Example*

.Payload
------------------------
[
{
"First Name": "William",
"Last Name": "Shakespeare",
"Phone": "(123)456-7890"
},
{
"First Name": "Christopher",
"Last Name": "Marlow",
"Phone": "(987)654-3210"
}
]
------------------------
.DataSonnet map:
------------------------
ds.write(payload, "application/xlsx")
------------------------

.Result
[%header,cols=3*a]
|===
|First Name
|Last Name
|Phone

|William
|Shakespeare
|(123)456-7890

|Christopher
|Marlow
|(987)654-3210
|===

Providing an optional `params` object allows for more control over the format of the output XLSX. The XLSX can be created without headers - in this case the input can be an array of arrays. In addition, a list of columns can be specified to override the JSON object names. In addition to the parameters described in the `read` section, the following XLSX output-only parameters are supported:

[%header, cols=3*a]
|===
|Parameter
|Description
|Default value

|`UseHeader`
|If set to `true`, the first row of XLSX will be a list of column headers and will mapped from the JSON object property names or from `Headers`
|`true`

|`Headers`
|an array of strings to use as column names (has no effect if `UseHeader` is set to `false`)
|`"`

|`UseTempFile`
|If set to `true`, a temp file will be created hold the output instead of an byte[]
|`false`
|===



*Example*

.Payload
------------------------
[
[
"William",
"Shakespeare",
"(123)456-7890"
],
[
"Christopher",
"Marlow",
"(987)654-3210"
]
]
------------------------
.DataSonnet map:
------------------------
local params = {
"UseHeader": false
};

ds.write(payload, "application/xlsx", params)

------------------------
.Result
|===
|William
|Shakespeare
|(123)456-7890

|Christopher
|Marlow
|(987)654-3210
|===


## Java Objects

### `read`
Expand Down
14 changes: 13 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<jackson.version>2.11.2</jackson.version>
<jackson.version>2.13.0</jackson.version>
<junit.version>5.6.2</junit.version>
<bouncycastle.version>1.68</bouncycastle.version>
<quickcheck.version>0.9.4</quickcheck.version>
Expand Down Expand Up @@ -123,6 +123,11 @@
<artifactId>jackson-dataformat-properties</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.github.sett4</groupId>
<artifactId>jackson-dataformat-xlsx-lite</artifactId>
<version>2.13.0</version>
</dependency>
<!-- plugin deps: end -->

<dependency>
Expand Down Expand Up @@ -512,4 +517,11 @@
</build>
</profile>
</profiles>

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
</project>
25 changes: 8 additions & 17 deletions src/main/java/com/datasonnet/document/MediaTypes.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.datasonnet.document;

/*-
* Copyright 2019-2020 the original author or authors.
* Copyright 2019-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,22 +16,6 @@
* limitations under the License.
*/

/*
* Copyright 2002-2020 the original author or authors.
*
* Licensed 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
*
* https://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.
*/

import java.util.Optional;

/**
Expand Down Expand Up @@ -348,6 +332,10 @@ public class MediaTypes {

public static final String APPLICATION_CSV_VALUE = "application/csv";

public static final MediaType APPLICATION_XLSX;

public static final String APPLICATION_XLSX_VALUE = "application/xlsx";

// See Null Object pattern
/**
* Public constant media type for representing an unknown content type. This is meant to used to signal to Datasonnet
Expand Down Expand Up @@ -390,6 +378,7 @@ public class MediaTypes {
TEXT_XML = new MediaType("text", "xml");
APPLICATION_JAVA = new MediaType("application", "x-java-object");
APPLICATION_CSV = new MediaType("application", "csv");
APPLICATION_XLSX = new MediaType("application", "xlsx");
UNKNOWN = new MediaType("unknown", "unknown");
}

Expand All @@ -402,6 +391,8 @@ public static Optional<MediaType> forExtension(String ext) {
return Optional.of(APPLICATION_XML);
case "csv":
return Optional.of(APPLICATION_CSV);
case "xlsx":
return Optional.of(APPLICATION_XLSX);
case "txt":
return Optional.of(TEXT_PLAIN);
default:
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/com/datasonnet/io/AutoDeleteFileInputStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.datasonnet.io;
/*-
* Copyright 2019-2021 the original author or authors.
*
* Licensed 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.
*/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

public class AutoDeleteFileInputStream extends FileInputStream {
private File file;

public AutoDeleteFileInputStream(File file) throws FileNotFoundException {
super(file);
this.file = file;
}

@Override
public void close() throws IOException {
super.close();
FileUtils.deleteQuietly(file);
}

}
Loading