-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[plugin-excel] Add ability to specify cells type during data creation
- Loading branch information
Showing
8 changed files
with
356 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
== Excel cell types | ||
|
||
[cols="1,3,3", options="header"] | ||
|=== | ||
|
||
|Type | ||
|Usage example | ||
|Description | ||
|
||
|STRING | ||
|`String value` | ||
|
||
`#{withCellType(String, String value)}` | ||
|Default cells type. Used if the cell type is not specified explicitly | ||
|
||
|FORMULA | ||
|`\#{withCellType(Formula, DOLLAR(A2))}` | ||
|
||
`#{withCellType(Formula, A2+A3)}` | ||
|https://support.microsoft.com/en-us/office/data-types-in-data-models-e2388f62-6122-4e2b-bcad-053e3da9ba90[Excel formulas] | ||
|
||
|DATE | ||
|`\#{withCellType(Date, 03/31/2024)}` - date with default date input format `MM/dd/yyyy` and default cell format `m/d/yy`. Output `3/31/24` | ||
|
||
`#{withCellType(Date, 31-Mar-2024 input dd-MMM-yyyy)}` - date with custom date input format `dd-MMM-yyyy` and default cell format `m/d/yy`. Output `3/31/24` | ||
|
||
`\#{withCellType(Date, 03/31/2024 format m.d.yyyy h:mm)}` - date with default date input format `MM/dd/yyyy` and custom cell format `m.d.yyyy h:mm`. Output `3.31.2024 0:00` | ||
|
||
`#{withCellType(Date, 2024/03/31 13:04 input yyyy/MM/dd HH:mm format m.d.yy h:mm)}` - date with custom date input and cell formats. Output `3.31.24 13:04` | ||
|
||
|Excel date. | ||
|
||
Optional parameters: | ||
|
||
`input` - the input date format based on the https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/time/format/DateTimeFormatter.html#patterns[date/time format]. Default value is `MM/dd/yyyy` | ||
|
||
`format` - the output https://support.microsoft.com/en-us/office/format-numbers-as-dates-or-times-418bd3fe-0577-47c8-8caa-b4d30c528309[cell date format] in Excel. Default value is `m/d/yy` | ||
|
||
|BOOLEAN | ||
|`#{withCellType(Boolean, false)}` | ||
|Boolean cell type | ||
|
||
|NUMERIC | ||
|`\#{withCellType(Numeric, 128)}` - output `128.0` | ||
|
||
`#{withCellType(Numeric, 32768 format \#,##0.00)}` - the number with `,` thousands separator and two decimal places. Output `32,768.00` | ||
|Number cell type | ||
|
||
Optional parameters: | ||
|
||
`format` - the current https://support.microsoft.com/en-us/office/number-format-codes-5026bbd6-04bc-48cd-bf33-80f18b4eae68[cell number format]. Default value `#.0` | ||
|
||
|=== | ||
|
||
=== Cells type for column | ||
|
||
Cells type can be specified for all values in column using the next column name format: `#{withColumnCellsType(<values-type>, <column-name>)`. The types specified in individual cells will have priority. | ||
|
||
.Create Excel file with typed column | ||
---- | ||
When I create temporary excel file with content: | ||
|#{withColumnCellsType(date, multi-type column)} | | ||
|31-Mar-2024 input dd-MMM-yyyy | | ||
|#{withCellType(Numeric, 100500 format #,##0.00)} | | ||
|#{withCellType(Boolean, true)} | | ||
|2024/03/31 13:04 input yyyy/MM/dd HH:mm format m.d.yy h:mm | | ||
and put path to scenario variable `path` | ||
---- | ||
|
||
Data from output Excel file: | ||
---- | ||
|multi-type column | | ||
|3/31/24 | | ||
|100,500.00 | | ||
|TRUE | | ||
|3.31.24 13:04 | | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
vividus-plugin-excel/src/main/java/org/vividus/model/CellType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright 2019-2024 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. | ||
*/ | ||
|
||
package org.vividus.model; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.Locale; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.apache.poi.ss.usermodel.Cell; | ||
import org.apache.poi.ss.usermodel.CellStyle; | ||
import org.apache.poi.ss.usermodel.CreationHelper; | ||
import org.apache.poi.xssf.usermodel.XSSFWorkbook; | ||
import org.vividus.util.DateUtils; | ||
|
||
public enum CellType | ||
{ | ||
STRING | ||
{ | ||
@Override | ||
public void setCellValue(XSSFWorkbook workbook, Cell cell, String value, DateUtils dateUtils) | ||
{ | ||
cell.setCellValue(value); | ||
} | ||
}, | ||
FORMULA | ||
{ | ||
@Override | ||
public void setCellValue(XSSFWorkbook workbook, Cell cell, String value, DateUtils dateUtils) | ||
{ | ||
cell.setCellFormula(value); | ||
} | ||
}, | ||
DATE | ||
{ | ||
@Override | ||
public void setCellValue(XSSFWorkbook workbook, Cell cell, String value, DateUtils dateUtils) | ||
{ | ||
Matcher valueMatcher = INCOMING_VALUE_PATTERN.matcher(value); | ||
if (!valueMatcher.matches()) | ||
{ | ||
throw new IllegalArgumentException("The date value isn't specified"); | ||
} | ||
Matcher inputFormatMatcher = INPUT_FORMAT_PATTERN.matcher(value); | ||
Matcher sellFormatMatcher = SELL_FORMAT_PATTERN.matcher(value); | ||
|
||
String inputFormat = inputFormatMatcher.matches() ? inputFormatMatcher.group(1).trim() : "MM/dd/yyyy"; | ||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(inputFormat, Locale.ENGLISH); | ||
ZonedDateTime zonedDateTime = dateUtils.parseDateTime(valueMatcher.group(1).trim(), formatter); | ||
LocalDateTime localDateTime = zonedDateTime.toLocalDateTime(); | ||
cell.setCellValue(localDateTime); | ||
|
||
CreationHelper createHelper = workbook.getCreationHelper(); | ||
CellStyle cellStyle = workbook.createCellStyle(); | ||
String sellFormat = sellFormatMatcher.matches() ? sellFormatMatcher.group(1).trim() : "m/d/yy"; | ||
cellStyle.setDataFormat(createHelper.createDataFormat().getFormat(sellFormat)); | ||
cell.setCellStyle(cellStyle); | ||
} | ||
}, | ||
BOOLEAN | ||
{ | ||
@Override | ||
public void setCellValue(XSSFWorkbook workbook, Cell cell, String value, DateUtils dateUtils) | ||
{ | ||
cell.setCellValue(Boolean.parseBoolean(value)); | ||
} | ||
}, | ||
NUMERIC | ||
{ | ||
@Override | ||
public void setCellValue(XSSFWorkbook workbook, Cell cell, String value, DateUtils dateUtils) | ||
{ | ||
Matcher valueMatcher = INCOMING_VALUE_PATTERN.matcher(value); | ||
if (!valueMatcher.matches()) | ||
{ | ||
throw new IllegalArgumentException("The numeric value isn't specified"); | ||
} | ||
Matcher sellFormatMatcher = SELL_FORMAT_PATTERN.matcher(value); | ||
|
||
cell.setCellValue(Double.parseDouble(valueMatcher.group(1).trim())); | ||
|
||
CreationHelper createHelper = workbook.getCreationHelper(); | ||
CellStyle cellStyle = workbook.createCellStyle(); | ||
String sellFormat = sellFormatMatcher.matches() ? sellFormatMatcher.group(1).trim() : "#.0"; | ||
cellStyle.setDataFormat(createHelper.createDataFormat().getFormat(sellFormat)); | ||
cell.setCellStyle(cellStyle); | ||
} | ||
}; | ||
|
||
private static final Pattern INCOMING_VALUE_PATTERN = Pattern.compile("^(.+?)(?=format|input|$).*"); | ||
private static final Pattern INPUT_FORMAT_PATTERN = Pattern.compile(".+input\\h+(.*?)(?=format|$).*"); | ||
private static final Pattern SELL_FORMAT_PATTERN = Pattern.compile(".+format\\h+(.*?)(?=input|$).*"); | ||
|
||
public abstract void setCellValue(XSSFWorkbook workbook, Cell cell, String value, DateUtils dateUtils); | ||
} |
Oops, something went wrong.