├── .gitignore
├── .idea
├── .name
├── ant.xml
├── compiler.xml
├── copyright
│ ├── Apache.xml
│ ├── Apache_2_0.xml
│ └── profiles_settings.xml
├── encodings.xml
├── libraries
│ ├── Maven__cglib_cglib_nodep_2_1_3.xml
│ ├── Maven__junit_junit_4_11.xml
│ ├── Maven__junit_junit_dep_4_4.xml
│ ├── Maven__org_apache_commons_commons_lang3_3_1.xml
│ ├── Maven__org_hamcrest_hamcrest_core_1_3.xml
│ ├── Maven__org_hamcrest_hamcrest_library_1_3.xml
│ ├── Maven__org_jmock_jmock_2_6_0.xml
│ ├── Maven__org_jmock_jmock_junit4_2_6_0.xml
│ ├── Maven__org_jmock_jmock_legacy_2_6_0.xml
│ └── Maven__org_objenesis_objenesis_1_0.xml
├── misc.xml
├── modules.xml
├── runConfigurations
│ └── All_in_simple_excel.xml
├── scopes
│ └── scope_settings.xml
├── uiDesigner.xml
└── vcs.xml
├── .travis.yml
├── LICENCE.md
├── README.md
├── RELEASE.md
├── pom.xml
├── simple-excel.iml
└── src
├── main
└── java
│ └── bad
│ └── robot
│ └── excel
│ ├── AbstractValueType.java
│ ├── Assertions.java
│ ├── OutputWorkbook.java
│ ├── PoiToExcelCoercions.java
│ ├── ValueType.java
│ ├── cell
│ ├── BlankCell.java
│ ├── BooleanCell.java
│ ├── Cell.java
│ ├── DataFormat.java
│ ├── DateCell.java
│ ├── DoubleCell.java
│ ├── ErrorCell.java
│ ├── Formula.java
│ ├── FormulaCell.java
│ ├── Hyperlink.java
│ ├── HyperlinkCell.java
│ ├── StringCell.java
│ └── StyledCell.java
│ ├── column
│ ├── ColumnIndex.java
│ └── ExcelColumnIndex.java
│ ├── matchers
│ ├── CellAdapter.java
│ ├── CellInRowMatcher.java
│ ├── CellMatcher.java
│ ├── CellNumberMatcher.java
│ ├── CellType.java
│ ├── CellsMatcher.java
│ ├── CompositeMatcher.java
│ ├── Matchers.java
│ ├── MismatchDetector.java
│ ├── Mismatches.java
│ ├── RowInSheetMatcher.java
│ ├── RowMissingMatcher.java
│ ├── RowNumberMatcher.java
│ ├── RowsMatcher.java
│ ├── SheetMatcher.java
│ ├── SheetNameMatcher.java
│ ├── SheetNumberMatcher.java
│ ├── SheetsMatcher.java
│ └── WorkbookMatcher.java
│ ├── row
│ ├── CopyRow.java
│ ├── DefaultRowBuilder.java
│ ├── DefaultStyledRowBuilder.java
│ ├── ExcelRowIndex.java
│ ├── NullSkippingRowBuilder.java
│ ├── Row.java
│ ├── RowBuilder.java
│ ├── RowIndex.java
│ └── StyledRowBuilder.java
│ ├── sheet
│ ├── Coordinate.java
│ ├── ExcelCoordinate.java
│ ├── SheetIndex.java
│ ├── SheetIterable.java
│ ├── SheetIterator.java
│ ├── SheetNameIterable.java
│ └── SheetNameIterator.java
│ ├── style
│ ├── Alignment.java
│ ├── AlignmentStyle.java
│ ├── Border.java
│ ├── BorderStyle.java
│ ├── BottomBorder.java
│ ├── CellStyleFactory.java
│ ├── ClonedStyleFactory.java
│ ├── Colour.java
│ ├── Fill.java
│ ├── FontColour.java
│ ├── FontSize.java
│ ├── ForegroundColour.java
│ ├── LeftBorder.java
│ ├── NoStyle.java
│ ├── ReplaceExistingStyle.java
│ ├── RightBorder.java
│ ├── Style.java
│ ├── StyleBuilder.java
│ └── TopBorder.java
│ └── workbook
│ ├── Editable.java
│ ├── ExcelWorkbook.java
│ ├── Navigable.java
│ ├── NavigablePoiWorkbook.java
│ ├── PoiWorkbook.java
│ ├── PoiWorkbookReader.java
│ ├── PoiWorkbookWriter.java
│ ├── Readable.java
│ ├── WorkbookType.java
│ └── Writable.java
└── test
├── java
└── bad
│ └── robot
│ └── excel
│ ├── DateCellTest.java
│ ├── DateUtil.java
│ ├── PoiToExcelCoercionsTest.java
│ ├── WorkbookResource.java
│ ├── cell
│ └── StringCellTest.java
│ ├── column
│ └── ExcelColumnIndexTest.java
│ ├── matchers
│ ├── Assertions.java
│ ├── CellInRowMatcherTest.java
│ ├── CellMatcherTest.java
│ ├── CellNumberMatcherTest.java
│ ├── CellTypeTest.java
│ ├── CellsMatcherTest.java
│ ├── MismatchesTest.java
│ ├── RowInSheetMatcherTest.java
│ ├── RowMissingMatcherTest.java
│ ├── RowNumberMatcherTest.java
│ ├── RowsMatcherTest.java
│ ├── SheetMatcherTest.java
│ ├── SheetNameMatcherTest.java
│ ├── SheetNumberMatcherTest.java
│ └── StubCell.java
│ ├── sheet
│ └── ExcelCoordinateTest.java
│ ├── style
│ ├── ColourTest.java
│ └── StyleBuilderTest.java
│ └── workbook
│ └── PoiWorkbookTest.java
└── resource
└── bad
└── robot
└── excel
├── cellTypes.xls
├── emptySheet.xls
├── emptySheet.xlsx
├── replaceCellWithSameRowExpected.xls
├── rowWithThreeCells.xls
├── rowWithThreeCellsAlternativeValues.xls
├── rowWithThreeCellsButOnlyTwoPhysicalCells.xls
├── rowWithThreePhysicalCells.xls
├── rowWithTwoCells.xls
├── rowWithVariousCells.xls
├── sheetWithSingleCell.xls
├── sheetWithThreeRows.xls
├── sheetWithThreeRowsWithAlternativeValues.xls
├── sheetWithTwoRows.xls
├── shouldAppendRowTemplate.xls
├── shouldAppendRowTemplateExpected.xls
├── shouldReplaceCellTemplate.xls
├── shouldReplaceCellTemplateExpected.xls
├── shouldReplaceCellsInComplicatedExampleTemplate.xls
├── shouldReplaceCellsInComplicatedExampleTemplateExpected.xls
├── shouldReplaceDateCellTemplate.xls
├── shouldReplaceDateCellTemplateExpected.xls
├── workbookTemplate.xls
├── workbookWithMultipleAlternativelyNamedSheets.xls
├── workbookWithMultipleNamedSheets.xls
├── workbookWithOneSheet.xls
└── workbookWithTwoSheets.xls
/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | .idea/workspace.xml
3 | pmip/
4 | .idea/libraries/
5 | .idea/
6 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | simple-excel
--------------------------------------------------------------------------------
/.idea/ant.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/.idea/copyright/Apache.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/copyright/Apache_2_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__cglib_cglib_nodep_2_1_3.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__junit_junit_4_11.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__junit_junit_dep_4_4.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__org_apache_commons_commons_lang3_3_1.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__org_hamcrest_hamcrest_library_1_3.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__org_jmock_jmock_2_6_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__org_jmock_jmock_junit4_2_6_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__org_jmock_jmock_legacy_2_6_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__org_objenesis_objenesis_1_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 | http://www.w3.org/1999/xhtml
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/runConfigurations/All_in_simple_excel.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/.idea/scopes/scope_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 | jdk:
3 | - openjdk8
--------------------------------------------------------------------------------
/RELEASE.md:
--------------------------------------------------------------------------------
1 | # Releasing
2 |
3 | ## Prepare
4 |
5 | In the `pom`, using HTTPS developer connection
6 |
7 | ``` xml
8 |
9 | scm:git:https://github.com/tobyweston/simple-excel.git
10 |
11 | ```
12 |
13 | and running,
14 |
15 | ```
16 | mvn release:prepare -Dusername=bub -Dpassword=secret
17 | ```
18 |
19 | will update the `pom` and create a tag in SCM.
20 |
21 | Where `password` is the personal access token generated from [GitHub](https://github.com/settings/tokens) as opposed to your regular password used in the web UI.
22 |
23 | ## Deploy
24 |
25 | After running the `prepare`, run the `perform` step
26 |
27 | ```
28 | mvn release:perform
29 | ```
30 |
31 | to checkout the newly created tag, build to release and deploy it (using the `mvn deploy`). This will use the `distributionManagement` from the `pom`.
32 |
33 | For local deployment to `robotooling.com`'s Maven repository, clone the repository before running this step.
34 |
35 | ```
36 | git clone git@github.com:tobyweston/robotooling.git
37 | git checkout gh-pages
38 | ...
39 | perform the release process (inc deploy)
40 | ...
41 | ./update.sh
42 | ```
43 |
44 |
45 | ## Rollback
46 |
47 | When things go wrong with the `prepare` step, rollback using
48 |
49 | `mvn release:rollback -Dusername=bub -Dpassword=secret`
50 |
51 | although you'll have to delete any tags manually
52 |
53 | ```
54 | git tag -d simple-excel-1.x
55 | git push origin :simple-excel-1.x
56 | ```
--------------------------------------------------------------------------------
/simple-excel.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/AbstractValueType.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel;
18 |
19 | import org.apache.commons.lang3.builder.EqualsBuilder;
20 | import org.apache.commons.lang3.builder.HashCodeBuilder;
21 | import org.apache.commons.lang3.builder.ToStringBuilder;
22 |
23 | import java.io.Serializable;
24 |
25 | import static bad.robot.excel.Assertions.assertNotNull;
26 | import static org.apache.commons.lang3.builder.CompareToBuilder.reflectionCompare;
27 | import static org.apache.commons.lang3.builder.ToStringStyle.SHORT_PREFIX_STYLE;
28 |
29 | public abstract class AbstractValueType implements ValueType, Serializable, Comparable> {
30 |
31 | private final T value;
32 |
33 | public AbstractValueType(T value) {
34 | assertNotNull(value);
35 | this.value = value;
36 | }
37 |
38 | @Override
39 | public T value() {
40 | return value;
41 | }
42 |
43 | @Override
44 | public int hashCode() {
45 | return new HashCodeBuilder().append(value()).toHashCode();
46 | }
47 |
48 | @Override
49 | public boolean equals(Object other) {
50 | if (other == null)
51 | return false;
52 | if (other == this)
53 | return true;
54 | if (this.getClass().equals(other.getClass()))
55 | return new EqualsBuilder().append(value(), ((AbstractValueType) other).value()).isEquals();
56 | return false;
57 | }
58 |
59 | @Override
60 | public int compareTo(AbstractValueType> other) {
61 | return reflectionCompare(value(), other.value(), true);
62 | }
63 |
64 | @Override
65 | public String toString() {
66 | return new ToStringBuilder(this, SHORT_PREFIX_STYLE).append(value()).toString();
67 | }
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/Assertions.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel;
18 |
19 | public class Assertions {
20 |
21 | public static T assertNotNull(T object) {
22 | if (object == null)
23 | throw new IllegalArgumentException("object can not be null");
24 | return object;
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/OutputWorkbook.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel;
18 |
19 | import bad.robot.excel.workbook.PoiWorkbookWriter;
20 | import bad.robot.excel.workbook.Writable;
21 | import org.apache.poi.ss.usermodel.Workbook;
22 |
23 | import java.io.IOException;
24 |
25 | public class OutputWorkbook {
26 |
27 | @Deprecated
28 | /** @deprecated use {@link Writable} instead*/
29 | public static void writeWorkbookToTemporaryFile(Workbook workbook, String filename) throws IOException {
30 | new PoiWorkbookWriter(workbook).saveAsTemporaryFile(filename);
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/PoiToExcelCoercions.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel;
18 |
19 | import bad.robot.excel.column.ExcelColumnIndex;
20 | import org.apache.poi.ss.usermodel.Cell;
21 | import org.apache.poi.ss.usermodel.Row;
22 |
23 | public class PoiToExcelCoercions {
24 |
25 | public static String asExcelCoordinate(Cell cell) {
26 | return asExcelColumn(cell) + asExcelRow(cell);
27 | }
28 |
29 | public static String asExcelColumn(Cell cell) {
30 | return ExcelColumnIndex.from(cell.getColumnIndex()).name();
31 | }
32 |
33 | public static int asExcelRow(Cell cell) {
34 | return cell.getRowIndex() + 1;
35 | }
36 |
37 | public static int asExcelRow(Row row) {
38 | return row.getRowNum() + 1;
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/ValueType.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel;
18 |
19 | import java.io.Serializable;
20 |
21 | public interface ValueType extends Serializable {
22 |
23 | T value();
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/cell/BlankCell.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.cell;
18 |
19 | import bad.robot.excel.column.ColumnIndex;
20 | import bad.robot.excel.column.ExcelColumnIndex;
21 | import bad.robot.excel.style.NoStyle;
22 | import bad.robot.excel.style.Style;
23 | import org.apache.poi.ss.usermodel.Cell;
24 | import org.apache.poi.ss.usermodel.Row;
25 | import org.apache.poi.ss.usermodel.Workbook;
26 |
27 | import static bad.robot.excel.column.ColumnIndex.column;
28 | import static org.apache.poi.ss.usermodel.CellType.BLANK;
29 |
30 | public class BlankCell extends StyledCell {
31 |
32 | public BlankCell() {
33 | this(new NoStyle());
34 | }
35 |
36 | public BlankCell(Style style) {
37 | super(style);
38 | }
39 |
40 | @Override
41 | public void addTo(Row row, ColumnIndex column, Workbook workbook) {
42 | Cell cell = row.createCell(column.value(), BLANK);
43 | this.getStyle().applyTo(cell, workbook);
44 | }
45 |
46 | @Override
47 | public void update(Cell cell, Workbook workbook) {
48 | ColumnIndex column = column(ExcelColumnIndex.from(cell.getColumnIndex()));
49 | this.getStyle().applyTo(cell, workbook);
50 | addTo(cell.getRow(), column, workbook);
51 | }
52 |
53 | @Override
54 | public String toString() {
55 | return "nothing";
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/cell/BooleanCell.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.cell;
18 |
19 | import bad.robot.excel.column.ColumnIndex;
20 | import bad.robot.excel.style.NoStyle;
21 | import bad.robot.excel.style.Style;
22 | import org.apache.poi.ss.usermodel.Row;
23 | import org.apache.poi.ss.usermodel.Workbook;
24 |
25 | import static org.apache.poi.ss.usermodel.CellType.BOOLEAN;
26 |
27 | public class BooleanCell extends StyledCell {
28 |
29 | private final Boolean value;
30 |
31 | public BooleanCell(Boolean value) {
32 | this(value, new NoStyle());
33 | }
34 |
35 | public BooleanCell(Boolean value, Style style) {
36 | super(style);
37 | this.value = value;
38 | }
39 |
40 | @Override
41 | public void addTo(Row row, ColumnIndex column, Workbook workbook) {
42 | org.apache.poi.ss.usermodel.Cell cell = row.createCell(column.value(), BOOLEAN);
43 | update(cell, workbook);
44 | }
45 |
46 | @Override
47 | public void update(org.apache.poi.ss.usermodel.Cell cell, Workbook workbook) {
48 | this.getStyle().applyTo(cell, workbook);
49 | cell.setCellValue(value);
50 | }
51 |
52 | @Override
53 | public String toString() {
54 | return value.toString().toUpperCase();
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/cell/Cell.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.cell;
18 |
19 | import bad.robot.excel.column.ColumnIndex;
20 | import org.apache.poi.ss.usermodel.Workbook;
21 |
22 | public interface Cell {
23 |
24 | void addTo(org.apache.poi.ss.usermodel.Row row, ColumnIndex column, Workbook workbook);
25 |
26 | void update(org.apache.poi.ss.usermodel.Cell cell, Workbook workbook);
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/cell/DataFormat.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.cell;
18 |
19 | import bad.robot.excel.AbstractValueType;
20 | import bad.robot.excel.style.Style;
21 | import org.apache.poi.ss.usermodel.Cell;
22 | import org.apache.poi.ss.usermodel.CellStyle;
23 | import org.apache.poi.ss.usermodel.Workbook;
24 |
25 | import static bad.robot.excel.style.ClonedStyleFactory.newStyleBasedOn;
26 |
27 | public class DataFormat extends AbstractValueType implements Style {
28 |
29 | public static DataFormat asDayMonthYear() {
30 | return dataFormat("dd-MMM-yyyy");
31 | }
32 |
33 | public static DataFormat asTwoDecimalPlacesNumber() {
34 | return dataFormat("#,##0.00");
35 | }
36 |
37 | public static DataFormat dataFormat(String value) {
38 | return new DataFormat(value);
39 | }
40 |
41 | private DataFormat(String value) {
42 | super(value);
43 | }
44 |
45 | @Override
46 | public void applyTo(Cell cell, Workbook workbook) {
47 | updateDataFormat(cell, workbook);
48 | }
49 |
50 | private void updateDataFormat(Cell cell, Workbook workbook) {
51 | CellStyle style = newStyleBasedOn(cell).create(workbook);
52 | style.setDataFormat(workbook.createDataFormat().getFormat(value()));
53 | cell.setCellStyle(style);
54 | }
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/cell/DateCell.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.cell;
18 |
19 | import bad.robot.excel.column.ColumnIndex;
20 | import bad.robot.excel.style.NoStyle;
21 | import bad.robot.excel.style.Style;
22 | import org.apache.poi.ss.usermodel.DateUtil;
23 | import org.apache.poi.ss.usermodel.Row;
24 | import org.apache.poi.ss.usermodel.Workbook;
25 |
26 | import java.util.Date;
27 |
28 | import static bad.robot.excel.cell.DataFormat.asDayMonthYear;
29 | import static org.apache.poi.ss.usermodel.CellType.NUMERIC;
30 |
31 | public class DateCell extends StyledCell {
32 |
33 | private final Date date;
34 |
35 | public DateCell(Date date) {
36 | this(date, new NoStyle());
37 | }
38 |
39 | public DateCell(Date date, Style style) {
40 | super(style);
41 | this.date = date;
42 | }
43 |
44 | @Override
45 | public void addTo(Row row, ColumnIndex column, Workbook workbook) {
46 | org.apache.poi.ss.usermodel.Cell cell = row.createCell(column.value(), NUMERIC);
47 | update(cell, workbook);
48 | }
49 |
50 | @Override
51 | public void update(org.apache.poi.ss.usermodel.Cell cell, Workbook workbook) {
52 | this.getStyle().applyTo(cell, workbook);
53 | if (!isCellDateFormatted(cell))
54 | overrideAsDateFormatting(workbook, cell);
55 | cell.setCellValue(date);
56 | }
57 |
58 | private void overrideAsDateFormatting(Workbook workbook, org.apache.poi.ss.usermodel.Cell cell) {
59 | asDayMonthYear().applyTo(cell, workbook);
60 | }
61 |
62 | @Override
63 | public String toString() {
64 | return date.toString();
65 | }
66 |
67 | private static boolean isCellDateFormatted(org.apache.poi.ss.usermodel.Cell cell) {
68 | return cell.getCellType() == NUMERIC && DateUtil.isCellDateFormatted(cell);
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/cell/DoubleCell.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.cell;
18 |
19 | import bad.robot.excel.column.ColumnIndex;
20 | import bad.robot.excel.style.NoStyle;
21 | import bad.robot.excel.style.Style;
22 | import org.apache.poi.ss.usermodel.Row;
23 | import org.apache.poi.ss.usermodel.Workbook;
24 |
25 | import static java.lang.String.format;
26 | import static org.apache.poi.ss.usermodel.CellType.NUMERIC;
27 |
28 | public class DoubleCell extends StyledCell {
29 |
30 | private final Double number;
31 |
32 | public DoubleCell(Double number) {
33 | this(number, new NoStyle());
34 | }
35 |
36 | public DoubleCell(Double number, Style style) {
37 | super(style);
38 | this.number = number;
39 | }
40 |
41 | @Override
42 | public void addTo(Row row, ColumnIndex column, Workbook workbook) {
43 | org.apache.poi.ss.usermodel.Cell cell = row.createCell(column.value(), NUMERIC);
44 | update(cell, workbook);
45 | }
46 |
47 | @Override
48 | public void update(org.apache.poi.ss.usermodel.Cell cell, Workbook workbook) {
49 | this.getStyle().applyTo(cell, workbook);
50 | cell.setCellValue(number);
51 | }
52 |
53 | @Override
54 | public String toString() {
55 | return format("%sD", number.toString());
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/cell/ErrorCell.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.cell;
18 |
19 | import bad.robot.excel.column.ColumnIndex;
20 | import bad.robot.excel.style.NoStyle;
21 | import bad.robot.excel.style.Style;
22 | import org.apache.poi.ss.usermodel.Row;
23 | import org.apache.poi.ss.usermodel.Workbook;
24 |
25 | import static java.lang.String.format;
26 | import static org.apache.poi.ss.usermodel.CellType.ERROR;
27 |
28 | public class ErrorCell extends StyledCell {
29 |
30 | private final Byte errorCode;
31 |
32 | public ErrorCell(Byte text) {
33 | this(text, new NoStyle());
34 | }
35 |
36 | public ErrorCell(Byte text, Style style) {
37 | super(style);
38 | this.errorCode = text;
39 | }
40 |
41 | @Override
42 | public void addTo(Row row, ColumnIndex column, Workbook workbook) {
43 | org.apache.poi.ss.usermodel.Cell cell = row.createCell(column.value(), ERROR);
44 | update(cell, workbook);
45 | }
46 |
47 | @Override
48 | public void update(org.apache.poi.ss.usermodel.Cell cell, Workbook workbook) {
49 | this.getStyle().applyTo(cell, workbook);
50 | cell.setCellErrorValue(errorCode);
51 | }
52 |
53 | @Override
54 | public String toString() {
55 | return format("Error:%s", errorCode);
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/cell/Formula.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.cell;
18 |
19 | import bad.robot.excel.AbstractValueType;
20 |
21 | public class Formula extends AbstractValueType {
22 |
23 | public static Formula formula(String formula) {
24 | return new Formula(formula);
25 | }
26 |
27 | private Formula(String formula) {
28 | super(formula);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/cell/FormulaCell.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.cell;
18 |
19 | import bad.robot.excel.column.ColumnIndex;
20 | import bad.robot.excel.style.NoStyle;
21 | import bad.robot.excel.style.Style;
22 | import org.apache.poi.ss.usermodel.Row;
23 | import org.apache.poi.ss.usermodel.Workbook;
24 |
25 | import static java.lang.String.format;
26 | import static org.apache.poi.ss.usermodel.CellType.FORMULA;
27 |
28 | public class FormulaCell extends StyledCell {
29 |
30 | private final String formula;
31 |
32 | public FormulaCell(String formula) {
33 | this(formula, new NoStyle());
34 | }
35 |
36 | public FormulaCell(Formula formula) {
37 | this(formula.value());
38 | }
39 |
40 | public FormulaCell(String text, Style style) {
41 | super(style);
42 | this.formula = text;
43 | }
44 |
45 | @Override
46 | public void addTo(Row row, ColumnIndex column, Workbook workbook) {
47 | org.apache.poi.ss.usermodel.Cell cell = row.createCell(column.value(), FORMULA);
48 | update(cell, workbook);
49 | }
50 |
51 | @Override
52 | public void update(org.apache.poi.ss.usermodel.Cell cell, Workbook workbook) {
53 | this.getStyle().applyTo(cell, workbook);
54 | cell.setCellFormula(formula);
55 | }
56 |
57 | @Override
58 | public String toString() {
59 | return format("Formula:%s", formula);
60 | }
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/cell/Hyperlink.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.cell;
18 |
19 | import bad.robot.excel.AbstractValueType;
20 |
21 | import java.net.MalformedURLException;
22 | import java.net.URL;
23 |
24 | import static java.lang.String.format;
25 |
26 | public class Hyperlink extends AbstractValueType {
27 |
28 | private final String text;
29 |
30 | public static Hyperlink hyperlink(String text, URL link) {
31 | return new Hyperlink(text, link);
32 | }
33 |
34 | public static Hyperlink hyperlink(String text, String url) {
35 | try {
36 | return new Hyperlink(text, new URL(url));
37 | } catch (MalformedURLException e) {
38 | throw new IllegalArgumentException(format("%s is not a valid URL", url), e);
39 | }
40 | }
41 |
42 | private Hyperlink(String text, URL url) {
43 | super(url);
44 | this.text = text;
45 | }
46 |
47 | public String text() {
48 | return text;
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/cell/HyperlinkCell.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.cell;
18 |
19 | import bad.robot.excel.column.ColumnIndex;
20 | import bad.robot.excel.style.NoStyle;
21 | import bad.robot.excel.style.Style;
22 | import org.apache.poi.common.usermodel.HyperlinkType;
23 | import org.apache.poi.ss.usermodel.Hyperlink;
24 | import org.apache.poi.ss.usermodel.Row;
25 | import org.apache.poi.ss.usermodel.Workbook;
26 |
27 | import java.net.URL;
28 |
29 | import static java.lang.String.format;
30 | import static org.apache.poi.ss.usermodel.CellType.STRING;
31 |
32 | /**
33 | * Only supports URL hyperlinks.
34 | */
35 | public class HyperlinkCell extends StyledCell {
36 |
37 | private final String text;
38 | private final URL link;
39 |
40 | public HyperlinkCell(String text, URL link) {
41 | this(text, link, new NoStyle());
42 | }
43 |
44 | public HyperlinkCell(bad.robot.excel.cell.Hyperlink link) {
45 | this(link.text(), link.value());
46 | }
47 |
48 | public HyperlinkCell(String text, URL link, Style style) {
49 | super(style);
50 | this.text = text;
51 | this.link = link;
52 | }
53 |
54 | @Override
55 | public void addTo(Row row, ColumnIndex column, Workbook workbook) {
56 | org.apache.poi.ss.usermodel.Cell cell = row.createCell(column.value(), STRING);
57 | update(cell, workbook);
58 | }
59 |
60 | @Override
61 | public void update(org.apache.poi.ss.usermodel.Cell cell, Workbook workbook) {
62 | this.getStyle().applyTo(cell, workbook);
63 | cell.setCellValue(text);
64 | cell.setHyperlink(createHyperlink(workbook));
65 | }
66 |
67 | @Override
68 | public String toString() {
69 | return format("%s", link, text);
70 | }
71 |
72 | private Hyperlink createHyperlink(Workbook workbook) {
73 | Hyperlink hyperlink = workbook.getCreationHelper().createHyperlink(HyperlinkType.URL);
74 | hyperlink.setAddress(link.toExternalForm());
75 | return hyperlink;
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/cell/StringCell.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.cell;
18 |
19 | import bad.robot.excel.column.ColumnIndex;
20 | import bad.robot.excel.style.NoStyle;
21 | import bad.robot.excel.style.Style;
22 | import org.apache.poi.ss.usermodel.Row;
23 | import org.apache.poi.ss.usermodel.Workbook;
24 |
25 | import static org.apache.poi.ss.usermodel.CellType.STRING;
26 |
27 | public class StringCell extends StyledCell {
28 |
29 | private final String text;
30 |
31 | public StringCell(String text) {
32 | this(text, new NoStyle());
33 | }
34 |
35 | public StringCell(String text, Style style) {
36 | super(style);
37 | this.text = text;
38 | }
39 |
40 | @Override
41 | public void addTo(Row row, ColumnIndex column, Workbook workbook) {
42 | org.apache.poi.ss.usermodel.Cell cell = row.createCell(column.value(), STRING);
43 | update(cell, workbook);
44 | }
45 |
46 | @Override
47 | public void update(org.apache.poi.ss.usermodel.Cell cell, Workbook workbook) {
48 | this.getStyle().applyTo(cell, workbook);
49 | cell.setCellValue(text);
50 | }
51 |
52 | @Override
53 | public String toString() {
54 | return "\"" + text + "\"";
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/cell/StyledCell.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.cell;
18 |
19 | import bad.robot.excel.style.Style;
20 |
21 | import static org.apache.commons.lang3.builder.EqualsBuilder.reflectionEquals;
22 |
23 | public abstract class StyledCell implements Cell {
24 |
25 | private final Style style;
26 |
27 | protected StyledCell(Style style) {
28 | this.style = style;
29 | }
30 |
31 | public Style getStyle() {
32 | return style;
33 | }
34 |
35 | @Override
36 | public boolean equals(Object that) {
37 | return reflectionEquals(this, that, false, this.getClass());
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/column/ColumnIndex.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.column;
18 |
19 | import bad.robot.excel.AbstractValueType;
20 |
21 | public class ColumnIndex extends AbstractValueType {
22 |
23 | public static ColumnIndex column(ExcelColumnIndex index) {
24 | return new ColumnIndex(index.ordinal());
25 | }
26 |
27 | private ColumnIndex(Integer value) {
28 | super(value);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/column/ExcelColumnIndex.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.column;
18 |
19 | /**
20 | * Currently supporting only 702 columns. sorry.
21 | */
22 | public enum ExcelColumnIndex {
23 | A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, AA, AB, AC, AD, AE, AF, AG, AH, AI, AJ, AK, AL, AM, AN, AO, AP, AQ, AR, AS, AT, AU, AV, AW, AX, AY, AZ, BA, BB, BC, BD, BE, BF, BG, BH, BI, BJ, BK, BL, BM, BN, BO, BP, BQ, BR, BS, BT, BU, BV, BW, BX, BY, BZ, CA, CB, CC, CD, CE, CF, CG, CH, CI, CJ, CK, CL, CM, CN, CO, CP, CQ, CR, CS, CT, CU, CV, CW, CX, CY, CZ, DA, DB, DC, DD, DE, DF, DG, DH, DI, DJ, DK, DL, DM, DN, DO, DP, DQ, DR, DS, DT, DU, DV, DW, DX, DY, DZ, EA, EB, EC, ED, EE, EF, EG, EH, EI, EJ, EK, EL, EM, EN, EO, EP, EQ, ER, ES, ET, EU, EV, EW, EX, EY, EZ, FA, FB, FC, FD, FE, FF, FG, FH, FI, FJ, FK, FL, FM, FN, FO, FP, FQ, FR, FS, FT, FU, FV, FW, FX, FY, FZ, GA, GB, GC, GD, GE, GF, GG, GH, GI, GJ, GK, GL, GM, GN, GO, GP, GQ, GR, GS, GT, GU, GV, GW, GX, GY, GZ, HA, HB, HC, HD, HE, HF, HG, HH, HI, HJ, HK, HL, HM, HN, HO, HP, HQ, HR, HS, HT, HU, HV, HW, HX, HY, HZ, IA, IB, IC, ID, IE, IF, IG, IH, II, IJ, IK, IL, IM, IN, IO, IP, IQ, IR, IS, IT, IU, IV, IW, IX, IY, IZ, JA, JB, JC, JD, JE, JF, JG, JH, JI, JJ, JK, JL, JM, JN, JO, JP, JQ, JR, JS, JT, JU, JV, JW, JX, JY, JZ, KA, KB, KC, KD, KE, KF, KG, KH, KI, KJ, KK, KL, KM, KN, KO, KP, KQ, KR, KS, KT, KU, KV, KW, KX, KY, KZ, LA, LB, LC, LD, LE, LF, LG, LH, LI, LJ, LK, LL, LM, LN, LO, LP, LQ, LR, LS, LT, LU, LV, LW, LX, LY, LZ, MA, MB, MC, MD, ME, MF, MG, MH, MI, MJ, MK, ML, MM, MN, MO, MP, MQ, MR, MS, MT, MU, MV, MW, MX, MY, MZ, NA, NB, NC, ND, NE, NF, NG, NH, NI, NJ, NK, NL, NM, NN, NO, NP, NQ, NR, NS, NT, NU, NV, NW, NX, NY, NZ, OA, OB, OC, OD, OE, OF, OG, OH, OI, OJ, OK, OL, OM, ON, OO, OP, OQ, OR, OS, OT, OU, OV, OW, OX, OY, OZ, PA, PB, PC, PD, PE, PF, PG, PH, PI, PJ, PK, PL, PM, PN, PO, PP, PQ, PR, PS, PT, PU, PV, PW, PX, PY, PZ, QA, QB, QC, QD, QE, QF, QG, QH, QI, QJ, QK, QL, QM, QN, QO, QP, QQ, QR, QS, QT, QU, QV, QW, QX, QY, QZ, RA, RB, RC, RD, RE, RF, RG, RH, RI, RJ, RK, RL, RM, RN, RO, RP, RQ, RR, RS, RT, RU, RV, RW, RX, RY, RZ, SA, SB, SC, SD, SE, SF, SG, SH, SI, SJ, SK, SL, SM, SN, SO, SP, SQ, SR, SS, ST, SU, SV, SW, SX, SY, SZ, TA, TB, TC, TD, TE, TF, TG, TH, TI, TJ, TK, TL, TM, TN, TO, TP, TQ, TR, TS, TT, TU, TV, TW, TX, TY, TZ, UA, UB, UC, UD, UE, UF, UG, UH, UI, UJ, UK, UL, UM, UN, UO, UP, UQ, UR, US, UT, UU, UV, UW, UX, UY, UZ, VA, VB, VC, VD, VE, VF, VG, VH, VI, VJ, VK, VL, VM, VN, VO, VP, VQ, VR, VS, VT, VU, VV, VW, VX, VY, VZ, WA, WB, WC, WD, WE, WF, WG, WH, WI, WJ, WK, WL, WM, WN, WO, WP, WQ, WR, WS, WT, WU, WV, WW, WX, WY, WZ, XA, XB, XC, XD, XE, XF, XG, XH, XI, XJ, XK, XL, XM, XN, XO, XP, XQ, XR, XS, XT, XU, XV, XW, XX, XY, XZ, YA, YB, YC, YD, YE, YF, YG, YH, YI, YJ, YK, YL, YM, YN, YO, YP, YQ, YR, YS, YT, YU, YV, YW, YX, YY, YZ, ZA, ZB, ZC, ZD, ZE, ZF, ZG, ZH, ZI, ZJ, ZK, ZL, ZM, ZN, ZO, ZP, ZQ, ZR, ZS, ZT, ZU, ZV, ZW, ZX, ZY, ZZ;
24 |
25 | public static ExcelColumnIndex from(int index) {
26 | return ExcelColumnIndex.values()[index];
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/matchers/CellAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import bad.robot.excel.cell.Cell;
20 |
21 | public interface CellAdapter {
22 | Cell adapt(org.apache.poi.ss.usermodel.Cell cell);
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/matchers/CellInRowMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import bad.robot.excel.cell.Cell;
20 | import org.apache.poi.ss.usermodel.Row;
21 | import org.apache.poi.ss.usermodel.Sheet;
22 | import org.hamcrest.Description;
23 | import org.hamcrest.TypeSafeDiagnosingMatcher;
24 |
25 | import static bad.robot.excel.PoiToExcelCoercions.asExcelCoordinate;
26 | import static bad.robot.excel.matchers.CellType.adaptPoi;
27 |
28 | public class CellInRowMatcher extends TypeSafeDiagnosingMatcher {
29 |
30 | private final Sheet sheet;
31 | private final Cell expected;
32 | private final int columnIndex;
33 | private final String coordinate;
34 |
35 | private CellInRowMatcher(Sheet sheet, org.apache.poi.ss.usermodel.Cell expectedPoi) {
36 | this.sheet = sheet;
37 | this.expected = adaptPoi(expectedPoi);
38 | this.coordinate = asExcelCoordinate(expectedPoi);
39 | this.columnIndex = expectedPoi.getColumnIndex();
40 | }
41 |
42 | public static CellInRowMatcher hasSameCell(Sheet sheet, org.apache.poi.ss.usermodel.Cell expected) {
43 | return new CellInRowMatcher(sheet, expected);
44 | }
45 |
46 | @Override
47 | protected boolean matchesSafely(Row row, Description mismatch) {
48 | Cell actual = adaptPoi(row.getCell(columnIndex));
49 |
50 | if (!expected.equals(actual)) {
51 | mismatch.appendText("cell at ").appendValue(coordinate).appendText(" contained ").appendValue(actual).appendText(" expected ").appendValue(expected)
52 | .appendText(" sheet ").appendValue(sheet.getSheetName());
53 | return false;
54 | }
55 | return true;
56 | }
57 |
58 | @Override
59 | public void describeTo(Description description) {
60 | description.appendText("equality of cell ").appendValue(coordinate);
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/matchers/CellMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.apache.poi.ss.usermodel.Cell;
20 | import org.hamcrest.Description;
21 | import org.hamcrest.Matcher;
22 | import org.hamcrest.TypeSafeDiagnosingMatcher;
23 |
24 | import static bad.robot.excel.PoiToExcelCoercions.asExcelCoordinate;
25 | import static bad.robot.excel.matchers.CellType.adaptPoi;
26 |
27 | public class CellMatcher extends TypeSafeDiagnosingMatcher {
28 |
29 | private final bad.robot.excel.cell.Cell expected;
30 |
31 | private CellMatcher(bad.robot.excel.cell.Cell expected) {
32 | this.expected = expected;
33 | }
34 |
35 | public static Matcher equalTo(bad.robot.excel.cell.Cell expected) {
36 | return new CellMatcher(expected);
37 | }
38 |
39 | @Override
40 | protected boolean matchesSafely(Cell cell, Description mismatch) {
41 | if (!adaptPoi(cell).equals(expected)) {
42 | mismatch.appendText("cell at ").appendValue(asExcelCoordinate(cell)).appendText(" contained ").appendValue(adaptPoi(cell)).appendText(" expected ").appendValue(expected);
43 | return false;
44 | }
45 | return true;
46 | }
47 |
48 | @Override
49 | public void describeTo(Description description) {
50 | description.appendValue(expected);
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/matchers/CellNumberMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.apache.poi.ss.usermodel.Row;
20 | import org.hamcrest.Description;
21 | import org.hamcrest.TypeSafeDiagnosingMatcher;
22 |
23 | import static bad.robot.excel.PoiToExcelCoercions.asExcelRow;
24 |
25 | /**
26 | * Assert the number of cells in two workbooks are the same.
27 | */
28 | public class CellNumberMatcher extends TypeSafeDiagnosingMatcher {
29 |
30 | private final Row expected;
31 |
32 | public static CellNumberMatcher hasSameNumberOfCellsAs(Row expected) {
33 | return new CellNumberMatcher(expected);
34 | }
35 |
36 | private CellNumberMatcher(Row expected) {
37 | this.expected = expected;
38 | }
39 |
40 | @Override
41 | protected boolean matchesSafely(Row actual, Description mismatch) {
42 | if (numberOfCellsIn(expected) != numberOfCellsIn(actual)) {
43 | mismatch.appendText("got ")
44 | .appendValue(numberOfCellsIn(actual))
45 | .appendText(" cell(s) on row ")
46 | .appendValue(asExcelRow(expected))
47 | .appendText(" expected ")
48 | .appendValue(numberOfCellsIn(expected))
49 | .appendText(" sheet ")
50 | .appendValue(expected.getSheet().getSheetName());
51 | return false;
52 | }
53 | if (numberOfPhysicalCellsIn(expected) != numberOfPhysicalCellsIn(actual)) {
54 | mismatch.appendText("got ")
55 | .appendValue(numberOfPhysicalCellsIn(actual))
56 | .appendText(" cell(s) containing value on row ")
57 | .appendValue(asExcelRow(expected))
58 | .appendText(" expected ")
59 | .appendValue(numberOfPhysicalCellsIn(expected))
60 | .appendText(" sheet ")
61 | .appendValue(expected.getSheet().getSheetName());
62 | return false;
63 | }
64 |
65 | return true;
66 | }
67 |
68 | @Override
69 | public void describeTo(Description description) {
70 | description.appendValue(numberOfCellsIn(expected)).appendText(" cell(s) on row ").appendValue(asExcelRow(expected))
71 | .appendText(" sheet ").appendValue(expected.getSheet().getSheetName());
72 | }
73 |
74 | /** POI is zero-based */
75 | private static int numberOfCellsIn(Row row) {
76 | return row.getLastCellNum();
77 | }
78 |
79 | private static int numberOfPhysicalCellsIn(Row row) {
80 | return row.getPhysicalNumberOfCells();
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/matchers/CellsMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.apache.poi.ss.usermodel.Cell;
20 | import org.apache.poi.ss.usermodel.Row;
21 | import org.hamcrest.Description;
22 | import org.hamcrest.Matcher;
23 | import org.hamcrest.TypeSafeDiagnosingMatcher;
24 |
25 | import java.util.ArrayList;
26 | import java.util.List;
27 |
28 | import static bad.robot.excel.PoiToExcelCoercions.asExcelRow;
29 | import static bad.robot.excel.matchers.CellInRowMatcher.hasSameCell;
30 | import static bad.robot.excel.matchers.CompositeMatcher.allOf;
31 |
32 | public class CellsMatcher extends TypeSafeDiagnosingMatcher {
33 |
34 | private final Row expected;
35 | private final List> cellsOnRow;
36 |
37 | public static CellsMatcher hasSameCellsAs(Row expected) {
38 | return new CellsMatcher(expected);
39 | }
40 |
41 | private CellsMatcher(Row expected) {
42 | this.expected = expected;
43 | this.cellsOnRow = createCellMatchers(expected);
44 | }
45 |
46 | @Override
47 | protected boolean matchesSafely(Row actual, Description mismatch) {
48 | return allOf(cellsOnRow).matchesSafely(actual, mismatch);
49 | }
50 |
51 | @Override
52 | public void describeTo(Description description) {
53 | description.appendText("equality of all cells on row ").appendValue(asExcelRow(expected));
54 | }
55 |
56 | private static List> createCellMatchers(Row row) {
57 | List> matchers = new ArrayList>();
58 | for (Cell expected : row)
59 | matchers.add(hasSameCell(row.getSheet(), expected));
60 | return matchers;
61 | }
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/matchers/CompositeMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.hamcrest.Description;
20 | import org.hamcrest.Factory;
21 | import org.hamcrest.Matcher;
22 | import org.hamcrest.TypeSafeDiagnosingMatcher;
23 |
24 | import java.util.Arrays;
25 |
26 | class CompositeMatcher extends TypeSafeDiagnosingMatcher {
27 |
28 | private final Iterable> matchers;
29 |
30 | @Factory
31 | static CompositeMatcher allOf(Iterable> matchers) {
32 | return new CompositeMatcher(matchers);
33 | }
34 |
35 | @Factory
36 | static TypeSafeDiagnosingMatcher allOf(Matcher... matchers) {
37 | return allOf(Arrays.asList(matchers));
38 | }
39 |
40 | private CompositeMatcher(Iterable> matchers) {
41 | this.matchers = matchers;
42 | }
43 |
44 | @Override
45 | protected boolean matchesSafely(T actual, Description mismatch) {
46 | Mismatches mismatches = new Mismatches();
47 | if (mismatches.discover(actual, matchers))
48 | mismatches.describeTo(mismatch, actual);
49 | return !mismatches.found();
50 | }
51 |
52 | @Override
53 | public void describeTo(Description description) {
54 | description.appendList("", " ", "", matchers);
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/matchers/Matchers.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.apache.poi.ss.usermodel.Cell;
20 | import org.apache.poi.ss.usermodel.Workbook;
21 | import org.hamcrest.Matcher;
22 |
23 | public class Matchers {
24 |
25 | public static Matcher sameWorkbook(Workbook expected) {
26 | return WorkbookMatcher.sameWorkbook(expected);
27 | }
28 |
29 | public static Matcher equalTo(bad.robot.excel.cell.Cell expected) {
30 | return CellMatcher.equalTo(expected);
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/matchers/MismatchDetector.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.hamcrest.Matcher;
20 |
21 | public interface MismatchDetector {
22 | boolean discover(T actual, Iterable> matchers);
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/matchers/Mismatches.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.hamcrest.Description;
20 | import org.hamcrest.Matcher;
21 |
22 | import java.util.ArrayList;
23 | import java.util.Iterator;
24 | import java.util.List;
25 |
26 | import static java.lang.String.format;
27 |
28 | class Mismatches implements MismatchDetector {
29 |
30 | private final List> mismatches = new ArrayList>();
31 |
32 | @Override
33 | public boolean discover(T actual, Iterable> matchers) {
34 | for (Matcher matcher : matchers) {
35 | if (!matcher.matches(actual))
36 | mismatches.add(matcher);
37 | }
38 | return found();
39 | }
40 |
41 | public void describeTo(Description description, T actual) {
42 | Iterator> iterator = mismatches.iterator();
43 | while (iterator.hasNext()) {
44 | iterator.next().describeMismatch(actual, description);
45 | if (iterator.hasNext())
46 | description.appendText(format(",\n%1$10s", ""));
47 | }
48 | }
49 |
50 | public boolean found() {
51 | return !mismatches.isEmpty();
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/matchers/RowInSheetMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.apache.poi.ss.usermodel.Row;
20 | import org.apache.poi.ss.usermodel.Sheet;
21 | import org.hamcrest.Description;
22 | import org.hamcrest.TypeSafeDiagnosingMatcher;
23 |
24 | import static bad.robot.excel.PoiToExcelCoercions.asExcelRow;
25 | import static bad.robot.excel.matchers.CellNumberMatcher.hasSameNumberOfCellsAs;
26 | import static bad.robot.excel.matchers.CellsMatcher.hasSameCellsAs;
27 | import static bad.robot.excel.matchers.RowMissingMatcher.rowIsPresent;
28 |
29 | public class RowInSheetMatcher extends TypeSafeDiagnosingMatcher {
30 |
31 | private final Row expected;
32 | private int rowIndex;
33 |
34 | public static RowInSheetMatcher hasSameRow(Row expected) {
35 | return new RowInSheetMatcher(expected);
36 | }
37 |
38 | private RowInSheetMatcher(Row expected) {
39 | this.expected = expected;
40 | this.rowIndex = expected.getRowNum();
41 | }
42 |
43 | @Override
44 | protected boolean matchesSafely(Sheet actualSheet, Description mismatch) {
45 | Row actual = actualSheet.getRow(rowIndex);
46 |
47 | if (!rowIsPresent(expected).matchesSafely(actual, mismatch))
48 | return false;
49 |
50 | if (!hasSameNumberOfCellsAs(expected).matchesSafely(actual, mismatch))
51 | return false;
52 |
53 | if (!hasSameCellsAs(expected).matchesSafely(actual, mismatch))
54 | return false;
55 |
56 | return true;
57 | }
58 |
59 | @Override
60 | public void describeTo(Description description) {
61 | description.appendText("equality of row ").appendValue(asExcelRow(expected));
62 | }
63 |
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/matchers/RowMissingMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.apache.poi.ss.usermodel.Row;
20 | import org.hamcrest.Description;
21 | import org.hamcrest.TypeSafeDiagnosingMatcher;
22 |
23 | import static bad.robot.excel.PoiToExcelCoercions.asExcelRow;
24 |
25 | public class RowMissingMatcher extends TypeSafeDiagnosingMatcher {
26 |
27 | private final Row expected;
28 |
29 | public static RowMissingMatcher rowIsPresent(Row expected) {
30 | return new RowMissingMatcher(expected);
31 | }
32 |
33 | private RowMissingMatcher(Row expected) {
34 | this.expected = expected;
35 | }
36 |
37 | @Override
38 | protected boolean matchesSafely(Row actual, Description mismatch) {
39 | if (actual == null) {
40 | mismatch.appendText("row ").appendValue(asExcelRow(expected)).appendText(" is missing")
41 | .appendText(" in sheet ").appendValue(expected.getSheet().getSheetName());
42 | return false;
43 | }
44 | return true;
45 | }
46 |
47 | @Override
48 | public void describeTo(Description description) {
49 | description.appendText("row ").appendValue(asExcelRow(expected)).appendText(" to be present")
50 | .appendText(" in sheet ").appendValue(expected.getSheet().getSheetName());
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/matchers/RowNumberMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.apache.poi.ss.usermodel.Sheet;
20 | import org.hamcrest.Description;
21 | import org.hamcrest.TypeSafeDiagnosingMatcher;
22 |
23 | public class RowNumberMatcher extends TypeSafeDiagnosingMatcher {
24 |
25 | private final Sheet expected;
26 |
27 | public static RowNumberMatcher hasSameNumberOfRowAs(Sheet expected) {
28 | return new RowNumberMatcher(expected);
29 | }
30 |
31 | private RowNumberMatcher(Sheet expected) {
32 | this.expected = expected;
33 | }
34 |
35 | @Override
36 | protected boolean matchesSafely(Sheet actual, Description mismatch) {
37 | if (expected.getLastRowNum() != actual.getLastRowNum()) {
38 | mismatch.appendText("got ")
39 | .appendValue(numberOfRowsIn(actual))
40 | .appendText(" row(s) in sheet ")
41 | .appendValue(actual.getSheetName())
42 | .appendText(" expected ")
43 | .appendValue(numberOfRowsIn(expected));
44 | return false;
45 | }
46 | return true;
47 | }
48 |
49 | @Override
50 | public void describeTo(Description description) {
51 | description.appendValue(numberOfRowsIn(expected)).appendText(" row(s) in sheet ").appendValue(expected.getSheetName());
52 | }
53 |
54 | /* POI is zero-based */
55 | private static int numberOfRowsIn(Sheet sheet) {
56 | return sheet.getLastRowNum() + 1;
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/matchers/RowsMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.apache.poi.ss.usermodel.Row;
20 | import org.apache.poi.ss.usermodel.Sheet;
21 | import org.hamcrest.Description;
22 | import org.hamcrest.Matcher;
23 | import org.hamcrest.TypeSafeDiagnosingMatcher;
24 |
25 | import java.util.ArrayList;
26 | import java.util.List;
27 |
28 | import static bad.robot.excel.matchers.CompositeMatcher.allOf;
29 | import static bad.robot.excel.matchers.RowInSheetMatcher.hasSameRow;
30 |
31 | public class RowsMatcher extends TypeSafeDiagnosingMatcher {
32 |
33 | private final Sheet expected;
34 | private final List> rowsOnSheet;
35 |
36 | public static RowsMatcher hasSameRowsAs(Sheet expected) {
37 | return new RowsMatcher(expected);
38 | }
39 |
40 | private RowsMatcher(Sheet expected) {
41 | this.expected = expected;
42 | this.rowsOnSheet = createRowMatchers(expected);
43 | }
44 |
45 | @Override
46 | protected boolean matchesSafely(Sheet actual, Description mismatch) {
47 | return allOf(rowsOnSheet).matchesSafely(actual, mismatch);
48 | }
49 |
50 | @Override
51 | public void describeTo(Description description) {
52 | description.appendText("equality on all rows in ").appendValue(expected.getSheetName());
53 | }
54 |
55 | private static List> createRowMatchers(Sheet sheet) {
56 | List> matchers = new ArrayList>();
57 | for (Row expected : sheet)
58 | matchers.add(hasSameRow(expected));
59 | return matchers;
60 | }
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/matchers/SheetMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.apache.poi.ss.usermodel.Workbook;
20 | import org.hamcrest.Description;
21 | import org.hamcrest.Matcher;
22 | import org.hamcrest.TypeSafeDiagnosingMatcher;
23 |
24 | import static bad.robot.excel.matchers.CompositeMatcher.allOf;
25 | import static bad.robot.excel.matchers.SheetNameMatcher.containsSameNamedSheetsAs;
26 | import static bad.robot.excel.matchers.SheetNumberMatcher.hasSameNumberOfSheetsAs;
27 |
28 | public class SheetMatcher extends TypeSafeDiagnosingMatcher {
29 |
30 | private final Matcher matchers;
31 |
32 | public static SheetMatcher hasSameSheetsAs(Workbook expected) {
33 | return new SheetMatcher(expected);
34 | }
35 |
36 | private SheetMatcher(Workbook expected) {
37 | Matcher numberOfSheets = hasSameNumberOfSheetsAs(expected);
38 | Matcher namesOfSheets = containsSameNamedSheetsAs(expected);
39 | this.matchers = allOf(numberOfSheets, namesOfSheets);
40 | }
41 |
42 | @Override
43 | protected boolean matchesSafely(Workbook actual, Description mismatch) {
44 | boolean match = matchers.matches(actual);
45 | if (!match)
46 | matchers.describeMismatch(actual, mismatch);
47 | return match;
48 | }
49 |
50 | @Override
51 | public void describeTo(Description description) {
52 | matchers.describeTo(description);
53 | }
54 |
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/matchers/SheetNameMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.apache.poi.ss.usermodel.Sheet;
20 | import org.apache.poi.ss.usermodel.Workbook;
21 | import org.hamcrest.Description;
22 | import org.hamcrest.TypeSafeDiagnosingMatcher;
23 |
24 | import java.util.ArrayList;
25 | import java.util.List;
26 |
27 | import static bad.robot.excel.sheet.SheetIterable.sheetsOf;
28 | import static bad.robot.excel.sheet.SheetNameIterable.sheetNamesOf;
29 | import static java.lang.String.format;
30 |
31 | /**
32 | * For every sheet in the "expected", the matcher checks to see if a sheet of the same name is in the "actual".
33 | *
34 | * The "actual" can contain more sheets however.
35 | */
36 | public class SheetNameMatcher extends TypeSafeDiagnosingMatcher {
37 |
38 | private final Workbook expected;
39 |
40 | private SheetNameMatcher(Workbook expected) {
41 | this.expected = expected;
42 | }
43 |
44 | public static SheetNameMatcher containsSameNamedSheetsAs(Workbook expected) {
45 | return new SheetNameMatcher(expected);
46 | }
47 |
48 | @Override
49 | protected boolean matchesSafely(Workbook actual, Description mismatch) {
50 | List missingSheets = new ArrayList();
51 | for (Sheet sheet : sheetsOf(expected)) {
52 | if (actual.getSheet(sheet.getSheetName()) == null)
53 | missingSheets.add(sheet.getSheetName());
54 | }
55 | mismatch.appendValueList("sheet(s) ", ", ", notFound(missingSheets), missingSheets);
56 | return missingSheets.isEmpty();
57 | }
58 |
59 | @Override
60 | public void describeTo(Description description) {
61 | if (!anyPreviousDescriptionsIncludedIn(description))
62 | description.appendText("workbook to contain sheets ");
63 | description.appendText("named ").appendValueList("", ", ", "", sheetNamesOf(expected));
64 | }
65 |
66 | private static boolean anyPreviousDescriptionsIncludedIn(Description description) {
67 | return !description.toString().endsWith("Expected: ");
68 | }
69 |
70 | private static String notFound(List values) {
71 | return format(" %s missing", values.size() == 1 ? "was" : "were");
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/matchers/SheetNumberMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.apache.poi.ss.usermodel.Workbook;
20 | import org.hamcrest.Description;
21 | import org.hamcrest.TypeSafeDiagnosingMatcher;
22 |
23 | public class SheetNumberMatcher extends TypeSafeDiagnosingMatcher {
24 |
25 | private final Workbook expected;
26 |
27 | private SheetNumberMatcher(Workbook expected) {
28 | this.expected = expected;
29 | }
30 |
31 | public static SheetNumberMatcher hasSameNumberOfSheetsAs(Workbook expected) {
32 | return new SheetNumberMatcher(expected);
33 | }
34 |
35 | @Override
36 | protected boolean matchesSafely(Workbook actual, Description mismatch) {
37 | if (expected.getNumberOfSheets() != actual.getNumberOfSheets()) {
38 | mismatch.appendText("got " ).appendValue(actual.getNumberOfSheets()).appendText(" sheet(s) expected ").appendValue(expected.getNumberOfSheets());
39 | return false;
40 | }
41 | return true;
42 | }
43 |
44 | @Override
45 | public void describeTo(Description description) {
46 | description.appendValue(expected.getNumberOfSheets()).appendText(" sheet(s)");
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/matchers/SheetsMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.apache.poi.ss.usermodel.Sheet;
20 | import org.apache.poi.ss.usermodel.Workbook;
21 | import org.hamcrest.Description;
22 | import org.hamcrest.TypeSafeDiagnosingMatcher;
23 |
24 | import static bad.robot.excel.matchers.RowNumberMatcher.hasSameNumberOfRowAs;
25 | import static bad.robot.excel.matchers.RowsMatcher.hasSameRowsAs;
26 | import static bad.robot.excel.sheet.SheetIterable.sheetsOf;
27 |
28 | public class SheetsMatcher extends TypeSafeDiagnosingMatcher {
29 |
30 | private final Workbook expected;
31 |
32 | public SheetsMatcher(Workbook expected) {
33 | this.expected = expected;
34 | }
35 |
36 | @Override
37 | protected boolean matchesSafely(Workbook actual, Description mismatch) {
38 | for (Sheet expectedSheet : sheetsOf(expected)) {
39 | Sheet actualSheet = actual.getSheet(expectedSheet.getSheetName());
40 |
41 | if (!hasSameNumberOfRowAs(expectedSheet).matchesSafely(actualSheet, mismatch))
42 | return false;
43 |
44 | if (!hasSameRowsAs(expectedSheet).matchesSafely(actualSheet, mismatch))
45 | return false;
46 | }
47 | return true;
48 | }
49 |
50 | @Override
51 | public void describeTo(Description description) {
52 | description.appendText("equality on all sheets in workbook");
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/matchers/WorkbookMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.apache.poi.ss.usermodel.Workbook;
20 | import org.hamcrest.Description;
21 | import org.hamcrest.Matcher;
22 | import org.hamcrest.TypeSafeDiagnosingMatcher;
23 |
24 | import static bad.robot.excel.matchers.SheetMatcher.hasSameSheetsAs;
25 |
26 | public class WorkbookMatcher extends TypeSafeDiagnosingMatcher {
27 |
28 | private final Workbook expected;
29 |
30 | public static Matcher sameWorkbook(Workbook expectedWorkbook) {
31 | return new WorkbookMatcher(expectedWorkbook);
32 | }
33 |
34 | private WorkbookMatcher(Workbook expected) {
35 | this.expected = expected;
36 | }
37 |
38 | @Override
39 | protected boolean matchesSafely(Workbook actual, Description mismatch) {
40 | if (!hasSameSheetsAs(expected).matchesSafely(actual, mismatch))
41 | return false;
42 |
43 | if (!new SheetsMatcher(expected).matchesSafely(actual, mismatch))
44 | return false;
45 |
46 | return true;
47 | }
48 |
49 |
50 | @Override
51 | public void describeTo(Description description) {
52 | description.appendText("entire workbook to be equal");
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/row/DefaultRowBuilder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.row;
18 |
19 | import bad.robot.excel.cell.*;
20 | import bad.robot.excel.column.ColumnIndex;
21 |
22 | import java.util.Date;
23 | import java.util.HashMap;
24 | import java.util.Map;
25 |
26 | public class DefaultRowBuilder implements RowBuilder {
27 |
28 | private Map cells = new HashMap();
29 |
30 | private DefaultRowBuilder() {
31 | }
32 |
33 | public static RowBuilder aRow() {
34 | return new DefaultRowBuilder();
35 | }
36 |
37 | @Override
38 | public RowBuilder withBlank(ColumnIndex index) {
39 | this.cells.put(index, new BlankCell());
40 | return this;
41 | }
42 |
43 | @Override
44 | public RowBuilder withString(ColumnIndex index, String text) {
45 | this.cells.put(index, new StringCell(text));
46 | return this;
47 | }
48 |
49 | @Override
50 | public RowBuilder withDouble(ColumnIndex index, Double value) {
51 | this.cells.put(index, new DoubleCell(value));
52 | return this;
53 | }
54 |
55 | @Override
56 | public RowBuilder withInteger(ColumnIndex index, Integer value) {
57 | this.cells.put(index, new DoubleCell(new Double(Integer.toString(value))));
58 | return this;
59 | }
60 |
61 | @Override
62 | public RowBuilder withDate(ColumnIndex index, Date date) {
63 | this.cells.put(index, new DateCell(date));
64 | return this;
65 | }
66 |
67 | @Override
68 | public RowBuilder withFormula(ColumnIndex index, String formula) {
69 | this.cells.put(index, new FormulaCell(formula));
70 | return this;
71 | }
72 |
73 | @Override
74 | public Row build() {
75 | return new Row(cells);
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/row/ExcelRowIndex.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.row;
18 |
19 | import bad.robot.excel.AbstractValueType;
20 |
21 | public class ExcelRowIndex extends AbstractValueType {
22 |
23 | public static ExcelRowIndex row(Integer value) {
24 | return new ExcelRowIndex(value);
25 | }
26 |
27 | private ExcelRowIndex(Integer value) {
28 | super(value);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/row/NullSkippingRowBuilder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.row;
18 |
19 | import bad.robot.excel.cell.*;
20 | import bad.robot.excel.column.ColumnIndex;
21 | import bad.robot.excel.column.ExcelColumnIndex;
22 | import bad.robot.excel.style.Style;
23 |
24 | import java.util.Date;
25 | import java.util.HashMap;
26 | import java.util.Map;
27 |
28 | import static bad.robot.excel.column.ColumnIndex.column;
29 |
30 |
31 | public class NullSkippingRowBuilder implements RowBuilder {
32 |
33 | private final Map cells;
34 | private final Style defaultStyle;
35 |
36 | public NullSkippingRowBuilder(int initialCapacity, Style defaultStyle) {
37 | this.cells = new HashMap(initialCapacity);
38 | this.defaultStyle = defaultStyle;
39 | for (int i = 0; i < initialCapacity; i++)
40 | cells.put(column(ExcelColumnIndex.from(i)), new BlankCell(defaultStyle));
41 | }
42 |
43 | @Override
44 | public RowBuilder withBlank(ColumnIndex index) {
45 | this.cells.put(index, new BlankCell(defaultStyle));
46 | return this;
47 | }
48 |
49 | @Override
50 | public NullSkippingRowBuilder withString(ColumnIndex index, String text) {
51 | if (text != null)
52 | this.cells.put(index, new StringCell(text, defaultStyle));
53 | return this;
54 | }
55 |
56 | @Override
57 | public NullSkippingRowBuilder withDouble(ColumnIndex index, Double value) {
58 | if (value != null)
59 | this.cells.put(index, new DoubleCell(value, defaultStyle));
60 | return this;
61 | }
62 |
63 | @Override
64 | public NullSkippingRowBuilder withInteger(ColumnIndex index, Integer value) {
65 | if (value != null)
66 | this.cells.put(index, new DoubleCell(new Double(Integer.toString(value)), defaultStyle));
67 | return this;
68 | }
69 |
70 | @Override
71 | public NullSkippingRowBuilder withDate(ColumnIndex index, Date date) {
72 | if (date != null)
73 | this.cells.put(index, new DateCell(date, defaultStyle));
74 | return this;
75 | }
76 |
77 | @Override
78 | public RowBuilder withFormula(ColumnIndex index, String formula) {
79 | if (formula != null)
80 | this.cells.put(index, new StringCell(formula, defaultStyle));
81 | return this;
82 | }
83 |
84 | @Override
85 | public Row build() {
86 | return new Row(cells);
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/row/Row.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.row;
18 |
19 | import bad.robot.excel.cell.Cell;
20 | import bad.robot.excel.column.ColumnIndex;
21 | import bad.robot.excel.sheet.SheetIndex;
22 | import org.apache.poi.ss.usermodel.Sheet;
23 | import org.apache.poi.ss.usermodel.Workbook;
24 |
25 | import java.util.HashMap;
26 | import java.util.Map;
27 |
28 | public class Row {
29 |
30 | private static final int shiftDownAmount = 1;
31 |
32 | private final Map cells = new HashMap();
33 |
34 | public Row(Map cells) {
35 | this.cells.putAll(cells);
36 | }
37 |
38 | public void insertAt(Workbook workbook, SheetIndex sheetIndex, RowIndex rowIndex) {
39 | Sheet sheet = workbook.getSheetAt(sheetIndex.value());
40 | sheet.shiftRows(rowIndex.value(), sheet.getLastRowNum(), shiftDownAmount);
41 | org.apache.poi.ss.usermodel.Row row = sheet.createRow(rowIndex.value());
42 | copyCellsTo(row, workbook);
43 | }
44 |
45 | public void appendTo(Workbook workbook, SheetIndex index) {
46 | Sheet sheet = workbook.getSheetAt(index.value());
47 | org.apache.poi.ss.usermodel.Row row = createRow(sheet);
48 | copyCellsTo(row, workbook);
49 | }
50 |
51 | private void copyCellsTo(org.apache.poi.ss.usermodel.Row row, Workbook workbook) {
52 | for (ColumnIndex index : cells.keySet()) {
53 | Cell cellToInsert = cells.get(index);
54 | cellToInsert.addTo(row, index, workbook);
55 | }
56 | }
57 |
58 | private static org.apache.poi.ss.usermodel.Row createRow(Sheet sheet) {
59 | if (sheet.getPhysicalNumberOfRows() == 0)
60 | return sheet.createRow(0);
61 | return sheet.createRow(sheet.getLastRowNum() + 1);
62 | }
63 |
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/row/RowBuilder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.row;
18 |
19 | import bad.robot.excel.column.ColumnIndex;
20 |
21 | import java.util.Date;
22 |
23 | public interface RowBuilder {
24 |
25 | RowBuilder withBlank(ColumnIndex index);
26 |
27 | RowBuilder withString(ColumnIndex index, String text);
28 |
29 | RowBuilder withDouble(ColumnIndex index, Double value);
30 |
31 | RowBuilder withInteger(ColumnIndex index, Integer value);
32 |
33 | RowBuilder withDate(ColumnIndex index, Date date);
34 |
35 | RowBuilder withFormula(ColumnIndex index, String formula);
36 |
37 | Row build();
38 | }
39 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/row/RowIndex.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.row;
18 |
19 | import bad.robot.excel.AbstractValueType;
20 |
21 | public class RowIndex extends AbstractValueType {
22 |
23 | public static RowIndex row(Integer value) {
24 | if (value <= 0)
25 | throw new IllegalArgumentException("row indices start at 1");
26 | return new RowIndex(value - 1);
27 | }
28 |
29 | private RowIndex(Integer value) {
30 | super(value);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/row/StyledRowBuilder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.row;
18 |
19 | import bad.robot.excel.column.ColumnIndex;
20 | import bad.robot.excel.style.Style;
21 |
22 | import java.util.Date;
23 |
24 | public interface StyledRowBuilder {
25 |
26 | StyledRowBuilder withBlank(ColumnIndex index, Style style);
27 |
28 | StyledRowBuilder withString(ColumnIndex index, String text, Style style);
29 |
30 | StyledRowBuilder withDouble(ColumnIndex index, Double value, Style style);
31 |
32 | StyledRowBuilder withInteger(ColumnIndex index, Integer value, Style style);
33 |
34 | StyledRowBuilder withDate(ColumnIndex index, Date date, Style style);
35 |
36 | StyledRowBuilder withFormula(ColumnIndex index, String formula, Style style);
37 |
38 | Row build();
39 |
40 | }
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/sheet/Coordinate.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.sheet;
18 |
19 | import bad.robot.excel.column.ColumnIndex;
20 | import bad.robot.excel.column.ExcelColumnIndex;
21 | import bad.robot.excel.row.RowIndex;
22 |
23 | import static bad.robot.excel.sheet.SheetIndex.sheet;
24 |
25 | public class Coordinate {
26 |
27 | private final ColumnIndex column;
28 | private final RowIndex row;
29 | private final SheetIndex sheet;
30 |
31 | public static Coordinate coordinate(ExcelColumnIndex column, Integer row) {
32 | return new Coordinate(ColumnIndex.column(column), RowIndex.row(row), sheet(1));
33 | }
34 |
35 | public static Coordinate coordinate(ExcelColumnIndex column, Integer row, SheetIndex sheet) {
36 | return new Coordinate(ColumnIndex.column(column), RowIndex.row(row), sheet);
37 | }
38 |
39 | public static Coordinate coordinate(ColumnIndex column, RowIndex row) {
40 | return new Coordinate(column, row, sheet(1));
41 | }
42 |
43 | public static Coordinate coordinate(ColumnIndex column, RowIndex row, SheetIndex sheet) {
44 | return new Coordinate(column, row, sheet);
45 | }
46 |
47 | private Coordinate(ColumnIndex column, RowIndex row, SheetIndex sheet) {
48 | this.column = column;
49 | this.row = row;
50 | this.sheet = sheet;
51 | }
52 |
53 | public ColumnIndex getColumn() {
54 | return column;
55 | }
56 |
57 | public RowIndex getRow() {
58 | return row;
59 | }
60 |
61 | public SheetIndex getSheet() {
62 | return sheet;
63 | }
64 | }
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/sheet/ExcelCoordinate.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.sheet;
18 |
19 | import bad.robot.excel.column.ExcelColumnIndex;
20 | import bad.robot.excel.row.ExcelRowIndex;
21 |
22 | public class ExcelCoordinate {
23 |
24 | private final ExcelColumnIndex column;
25 | private final ExcelRowIndex row;
26 |
27 | public static ExcelCoordinate coordinate(ExcelColumnIndex column, int row) {
28 | return new ExcelCoordinate(column, ExcelRowIndex.row(row));
29 | }
30 |
31 | public static ExcelCoordinate coordinate(ExcelColumnIndex column, ExcelRowIndex row) {
32 | return new ExcelCoordinate(column, row);
33 | }
34 |
35 | public ExcelCoordinate(ExcelColumnIndex column, ExcelRowIndex row) {
36 | this.column = column;
37 | this.row = row;
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/sheet/SheetIndex.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.sheet;
18 |
19 | import bad.robot.excel.AbstractValueType;
20 |
21 | public class SheetIndex extends AbstractValueType {
22 |
23 | public static SheetIndex sheet(Integer value) {
24 | if (value <= 0)
25 | throw new IllegalArgumentException("sheet indices start at 1");
26 | return new SheetIndex(value - 1);
27 | }
28 |
29 | private SheetIndex(Integer value) {
30 | super(value);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/sheet/SheetIterable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.sheet;
18 |
19 | import org.apache.poi.ss.usermodel.Sheet;
20 | import org.apache.poi.ss.usermodel.Workbook;
21 |
22 | import java.util.Iterator;
23 |
24 | public class SheetIterable implements Iterable {
25 |
26 | private final Workbook workbook;
27 |
28 | private SheetIterable(Workbook workbook) {
29 | this.workbook = workbook;
30 | }
31 |
32 | public static SheetIterable sheetsOf(Workbook workbook) {
33 | return new SheetIterable(workbook);
34 | }
35 |
36 | @Override
37 | public Iterator iterator() {
38 | return new SheetIterator(workbook);
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/sheet/SheetIterator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.sheet;
18 |
19 | import org.apache.poi.ss.usermodel.Sheet;
20 | import org.apache.poi.ss.usermodel.Workbook;
21 |
22 | import java.util.Iterator;
23 |
24 | class SheetIterator implements Iterator {
25 |
26 | private final Workbook workbook;
27 | private int index;
28 |
29 | public SheetIterator(Workbook workbook) {
30 | this.workbook = workbook;
31 | }
32 |
33 | @Override
34 | public boolean hasNext() {
35 | return index < workbook.getNumberOfSheets();
36 | }
37 |
38 | @Override
39 | public Sheet next() {
40 | return workbook.getSheetAt(index++);
41 | }
42 |
43 | @Override
44 | public void remove() {
45 | throw new UnsupportedOperationException();
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/sheet/SheetNameIterable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.sheet;
18 |
19 | import org.apache.poi.ss.usermodel.Workbook;
20 |
21 | import java.util.Iterator;
22 |
23 | public class SheetNameIterable implements Iterable {
24 |
25 | private final Workbook workbook;
26 |
27 | private SheetNameIterable(Workbook workbook) {
28 | this.workbook = workbook;
29 | }
30 |
31 | public static SheetNameIterable sheetNamesOf(Workbook workbook) {
32 | return new SheetNameIterable(workbook);
33 | }
34 |
35 | @Override
36 | public Iterator iterator() {
37 | return new SheetNameIterator(workbook);
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/sheet/SheetNameIterator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.sheet;
18 |
19 | import org.apache.poi.ss.usermodel.Workbook;
20 |
21 | import java.util.Iterator;
22 |
23 | class SheetNameIterator implements Iterator {
24 |
25 | private final SheetIterator delegate;
26 |
27 | public SheetNameIterator(Workbook workbook) {
28 | delegate = new SheetIterator(workbook);
29 | }
30 |
31 | @Override
32 | public boolean hasNext() {
33 | return delegate.hasNext();
34 | }
35 |
36 | @Override
37 | public String next() {
38 | return delegate.next().getSheetName();
39 | }
40 |
41 | @Override
42 | public void remove() {
43 | delegate.remove();
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/style/Alignment.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.style;
18 |
19 | import bad.robot.excel.AbstractValueType;
20 |
21 | public class Alignment extends AbstractValueType {
22 |
23 | public static Alignment alignment(AlignmentStyle value) {
24 | return new Alignment(value);
25 | }
26 |
27 | public Alignment(AlignmentStyle value) {
28 | super(value);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/style/AlignmentStyle.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.style;
18 |
19 | import org.apache.poi.ss.usermodel.HorizontalAlignment;
20 |
21 | import static org.apache.poi.ss.usermodel.HorizontalAlignment.*;
22 |
23 |
24 | public enum AlignmentStyle {
25 |
26 | Left(LEFT),
27 | Centre(CENTER),
28 | Right(RIGHT),
29 | Justify(JUSTIFY);
30 |
31 | private HorizontalAlignment poiStyle;
32 |
33 | AlignmentStyle(HorizontalAlignment poiStyle) {
34 | this.poiStyle = poiStyle;
35 | }
36 |
37 | public HorizontalAlignment getPoiStyle() {
38 | return poiStyle;
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/style/Border.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.style;
18 |
19 | public class Border {
20 |
21 | private final TopBorder top;
22 | private final BottomBorder bottom;
23 | private final LeftBorder left;
24 | private final RightBorder right;
25 |
26 | private Border(TopBorder top, RightBorder right, BottomBorder bottom, LeftBorder left) {
27 | this.top = top;
28 | this.bottom = bottom;
29 | this.left = left;
30 | this.right = right;
31 | }
32 |
33 | public static Border border(TopBorder top, RightBorder right, BottomBorder bottom, LeftBorder left) {
34 | return new Border(top, right, bottom, left);
35 | }
36 |
37 | public BottomBorder getBottom() {
38 | return bottom;
39 | }
40 |
41 | public TopBorder getTop() {
42 | return top;
43 | }
44 |
45 | public LeftBorder getLeft() {
46 | return left;
47 | }
48 |
49 | public RightBorder getRight() {
50 | return right;
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/style/BorderStyle.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.style;
18 |
19 | import static org.apache.poi.ss.usermodel.BorderStyle.*;
20 |
21 | public enum BorderStyle {
22 |
23 | None(NONE),
24 | ThinSolid(THIN),
25 | MediumSolid(MEDIUM),
26 | ThickSolid(THICK);
27 |
28 | private org.apache.poi.ss.usermodel.BorderStyle poiStyle;
29 |
30 | BorderStyle(org.apache.poi.ss.usermodel.BorderStyle poiStyle) {
31 | this.poiStyle = poiStyle;
32 | }
33 |
34 | public org.apache.poi.ss.usermodel.BorderStyle getPoiStyle() {
35 | return poiStyle;
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/style/BottomBorder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.style;
18 |
19 | import bad.robot.excel.AbstractValueType;
20 |
21 | public class BottomBorder extends AbstractValueType {
22 |
23 | private BottomBorder(BorderStyle value) {
24 | super(value);
25 | }
26 |
27 | public static BottomBorder bottom(BorderStyle value) {
28 | return new BottomBorder(value);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/style/CellStyleFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.style;
18 |
19 | import org.apache.poi.ss.usermodel.CellStyle;
20 | import org.apache.poi.ss.usermodel.Workbook;
21 |
22 | public interface CellStyleFactory {
23 | CellStyle create(Workbook workbook);
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/style/ClonedStyleFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.style;
18 |
19 | import org.apache.poi.ss.usermodel.CellStyle;
20 | import org.apache.poi.ss.usermodel.Workbook;
21 |
22 | public class ClonedStyleFactory implements CellStyleFactory {
23 |
24 | private final org.apache.poi.ss.usermodel.Cell source;
25 |
26 | private ClonedStyleFactory(org.apache.poi.ss.usermodel.Cell source) {
27 | this.source = source;
28 | }
29 |
30 | public static ClonedStyleFactory newStyleBasedOn(org.apache.poi.ss.usermodel.Cell source) {
31 | return new ClonedStyleFactory(source);
32 | }
33 |
34 | @Override
35 | public CellStyle create(Workbook workbook) {
36 | CellStyle style = workbook.createCellStyle();
37 | style.cloneStyleFrom(source.getCellStyle());
38 | return style;
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/style/Colour.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.style;
18 |
19 | import org.apache.poi.ss.usermodel.IndexedColors;
20 |
21 | import static org.apache.poi.ss.usermodel.IndexedColors.*;
22 |
23 | public enum Colour {
24 |
25 | Brown(BROWN),
26 | Blue(LIGHT_BLUE),
27 | DarkRed(DARK_RED),
28 | DarkGrey(GREY_25_PERCENT),
29 | DarkYellow(YELLOW),
30 | Red(RED),
31 | Black(BLACK),
32 | Grey(GREY_25_PERCENT),
33 | White(WHITE),
34 | BrightGreen(BRIGHT_GREEN),
35 | Yellow(LIGHT_YELLOW),
36 | Pink(PINK),
37 | Turquoise(LIGHT_TURQUOISE),
38 | Green(LIGHT_GREEN),
39 | Violet(VIOLET),
40 | Teal(TEAL),
41 | Maroon(MAROON),
42 | Coral(CORAL),
43 | Rose(ROSE),
44 | Lavender(LAVENDER),
45 | Orange(LIGHT_ORANGE),
46 | Olive(OLIVE_GREEN),
47 | Plum(PLUM);
48 |
49 | private final IndexedColors color;
50 |
51 | Colour(IndexedColors color) {
52 | this.color = color;
53 | }
54 |
55 | public short getPoiStyle() {
56 | return color.getIndex();
57 | }
58 |
59 | }
60 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/style/Fill.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.style;
18 |
19 | public class Fill {
20 |
21 | private final ForegroundColour foregroundColour;
22 |
23 | public static Fill fill(ForegroundColour foregroundColour) {
24 | return new Fill(foregroundColour);
25 | }
26 |
27 | private Fill(ForegroundColour foregroundColour) {
28 | this.foregroundColour = foregroundColour;
29 | }
30 |
31 | public ForegroundColour getForegroundColour() {
32 | return foregroundColour;
33 | }
34 |
35 | }
36 |
37 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/style/FontColour.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.style;
18 |
19 | import bad.robot.excel.AbstractValueType;
20 |
21 | public class FontColour extends AbstractValueType {
22 |
23 | public static FontColour fontColour(Colour value) {
24 | return new FontColour(value);
25 | }
26 |
27 | private FontColour(Colour value) {
28 | super(value);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/style/FontSize.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.style;
18 |
19 | import bad.robot.excel.AbstractValueType;
20 |
21 | public class FontSize extends AbstractValueType {
22 |
23 | public static FontSize fontSize(String value) {
24 | return new FontSize(Short.valueOf(value).shortValue());
25 | }
26 |
27 | private FontSize(Short value) {
28 | super(value);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/style/ForegroundColour.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.style;
18 |
19 | import bad.robot.excel.AbstractValueType;
20 |
21 | public class ForegroundColour extends AbstractValueType {
22 |
23 | public static ForegroundColour foregroundColour(Colour value) {
24 | return new ForegroundColour(value);
25 | }
26 |
27 | private ForegroundColour(Colour value) {
28 | super(value);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/style/LeftBorder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.style;
18 |
19 | import bad.robot.excel.AbstractValueType;
20 |
21 | public class LeftBorder extends AbstractValueType {
22 |
23 | private LeftBorder(BorderStyle value) {
24 | super(value);
25 | }
26 |
27 | public static LeftBorder left(BorderStyle value) {
28 | return new LeftBorder(value);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/style/NoStyle.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.style;
18 |
19 | import org.apache.poi.ss.usermodel.Workbook;
20 |
21 | public class NoStyle implements Style {
22 |
23 | @Override
24 | public void applyTo(org.apache.poi.ss.usermodel.Cell cell, Workbook template) {
25 | // Do nothing
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/style/ReplaceExistingStyle.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.style;
18 |
19 | import bad.robot.excel.cell.DataFormat;
20 | import org.apache.poi.ss.usermodel.CellStyle;
21 | import org.apache.poi.ss.usermodel.Font;
22 | import org.apache.poi.ss.usermodel.Workbook;
23 |
24 | import static bad.robot.excel.style.ClonedStyleFactory.newStyleBasedOn;
25 | import static org.apache.poi.ss.usermodel.FillPatternType.SOLID_FOREGROUND;
26 |
27 | public class ReplaceExistingStyle implements Style {
28 |
29 | private final DataFormat format;
30 | private final Alignment alignment;
31 | private final FontSize fontSize;
32 | private final FontColour fontColour;
33 | private final Fill fill;
34 | private final Border border;
35 |
36 | /**
37 | * package protected. use {@link bad.robot.excel.style.StyleBuilder} instead
38 | */
39 | ReplaceExistingStyle(Border border, DataFormat format, Alignment alignment, FontSize fontSize, FontColour fontColour, Fill fill) {
40 | this.border = border;
41 | this.format = format;
42 | this.alignment = alignment;
43 | this.fontSize = fontSize;
44 | this.fontColour = fontColour;
45 | this.fill = fill;
46 | }
47 |
48 | @Override
49 | public void applyTo(org.apache.poi.ss.usermodel.Cell cell, Workbook workbook) {
50 | CellStyle style = newStyleBasedOn(cell).create(workbook);
51 | applyBorderTo(style);
52 | applyFillTo(style);
53 | applyAlignmentTo(style);
54 | applyDataFormatTo(style, workbook);
55 | applyFontTo(style, workbook);
56 | cell.setCellStyle(style);
57 | }
58 |
59 | private void applyBorderTo(CellStyle style) {
60 | if (border != null) {
61 | style.setBorderBottom(border.getBottom().value().getPoiStyle());
62 | style.setBorderTop(border.getTop().value().getPoiStyle());
63 | style.setBorderRight(border.getRight().value().getPoiStyle());
64 | style.setBorderLeft(border.getLeft().value().getPoiStyle());
65 | }
66 | }
67 |
68 | private void applyFillTo(CellStyle style) {
69 | if (fill != null) {
70 | style.setFillPattern(SOLID_FOREGROUND);
71 | style.setFillForegroundColor(fill.getForegroundColour().value().getPoiStyle());
72 | }
73 | }
74 |
75 | private void applyAlignmentTo(CellStyle style) {
76 | if (alignment != null)
77 | style.setAlignment(alignment.value().getPoiStyle());
78 | }
79 |
80 | private void applyDataFormatTo(CellStyle style, Workbook workbook) {
81 | if (format != null)
82 | style.setDataFormat(workbook.createDataFormat().getFormat(format.value()));
83 | }
84 |
85 | private void applyFontTo(CellStyle style, Workbook workbook) {
86 | if (fontSize != null) {
87 | Font font = workbook.createFont();
88 | font.setFontHeightInPoints(fontSize.value());
89 | font.setColor(fontColour.value().getPoiStyle());
90 | style.setFont(font);
91 | } else {
92 | // doesn't work
93 | Font existing = workbook.getFontAt(style.getFontIndex());
94 | existing.setColor(fontColour.value().getPoiStyle());
95 | style.setFont(existing);
96 | }
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/style/RightBorder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.style;
18 |
19 | import bad.robot.excel.AbstractValueType;
20 |
21 | public class RightBorder extends AbstractValueType {
22 |
23 | private RightBorder(BorderStyle value) {
24 | super(value);
25 | }
26 |
27 | public static RightBorder right(BorderStyle value) {
28 | return new RightBorder(value);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/style/Style.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.style;
18 |
19 | import org.apache.poi.ss.usermodel.Workbook;
20 |
21 | public interface Style {
22 |
23 | void applyTo(org.apache.poi.ss.usermodel.Cell cell, Workbook workbook);
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/style/StyleBuilder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.style;
18 |
19 | import bad.robot.excel.cell.DataFormat;
20 | import org.apache.poi.ss.usermodel.Cell;
21 | import org.apache.poi.ss.usermodel.Workbook;
22 |
23 | import static bad.robot.excel.style.Colour.Black;
24 | import static bad.robot.excel.style.FontColour.fontColour;
25 |
26 | public class StyleBuilder implements Style {
27 |
28 | private DataFormat format;
29 | private Alignment alignment;
30 | private FontSize fontSize;
31 | private Border border;
32 | private Fill fill;
33 | private FontColour fontColour = fontColour(Black);
34 |
35 | private StyleBuilder() {
36 | }
37 |
38 | public static StyleBuilder aStyle() {
39 | return new StyleBuilder();
40 | }
41 |
42 | public StyleBuilder with(DataFormat format) {
43 | this.format = format;
44 | return this;
45 | }
46 |
47 | public StyleBuilder with(Alignment alignment) {
48 | this.alignment = alignment;
49 | return this;
50 | }
51 |
52 | public StyleBuilder with(FontSize fontSize) {
53 | this.fontSize = fontSize;
54 | return this;
55 | }
56 |
57 | public StyleBuilder with(FontColour fontColour) {
58 | this.fontColour = fontColour;
59 | return this;
60 | }
61 |
62 | public StyleBuilder with(Border border) {
63 | this.border = border;
64 | return this;
65 | }
66 |
67 | public StyleBuilder with(Fill fill) {
68 | this.fill = fill;
69 | return this;
70 | }
71 |
72 | private ReplaceExistingStyle build() {
73 | return new ReplaceExistingStyle(border, format, alignment, fontSize, fontColour, fill);
74 | }
75 |
76 | @Override
77 | public void applyTo(Cell cell, Workbook workbook) {
78 | build().applyTo(cell, workbook);
79 | }
80 | }
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/style/TopBorder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.style;
18 |
19 | import bad.robot.excel.AbstractValueType;
20 |
21 | public class TopBorder extends AbstractValueType {
22 |
23 | private TopBorder(BorderStyle value) {
24 | super(value);
25 | }
26 |
27 | public static TopBorder top(BorderStyle value) {
28 | return new TopBorder(value);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/workbook/Editable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.workbook;
18 |
19 | import bad.robot.excel.cell.Cell;
20 | import bad.robot.excel.cell.Formula;
21 | import bad.robot.excel.cell.Hyperlink;
22 | import bad.robot.excel.row.Row;
23 | import bad.robot.excel.row.RowIndex;
24 | import bad.robot.excel.sheet.Coordinate;
25 | import bad.robot.excel.sheet.SheetIndex;
26 | import org.apache.poi.ss.usermodel.Sheet;
27 | import org.apache.poi.ss.usermodel.Workbook;
28 |
29 | import java.util.Date;
30 |
31 | public interface Editable {
32 |
33 | Editable blankCell(Coordinate coordinate);
34 |
35 | Editable replaceCell(Coordinate coordinate, Cell cell);
36 |
37 | Editable replaceCell(Coordinate coordinate, String value);
38 |
39 | Editable replaceCell(Coordinate coordinate, Formula formula);
40 |
41 | Editable replaceCell(Coordinate coordinate, Date date);
42 |
43 | Editable replaceCell(Coordinate coordinate, Double number);
44 |
45 | Editable replaceCell(Coordinate coordinate, Hyperlink hyperlink);
46 |
47 | Editable replaceCell(Coordinate coordinate, Boolean value);
48 |
49 | Editable copyRow(Workbook workbook, Sheet worksheet, RowIndex from, RowIndex to);
50 |
51 | Editable insertSheet();
52 |
53 | Editable insertSheet(String name);
54 |
55 | Editable insertRowToFirstSheet(Row row, RowIndex index);
56 |
57 | Editable insertRowToSheet(Row row, RowIndex index, SheetIndex sheet);
58 |
59 | Editable appendRowToFirstSheet(Row row);
60 |
61 | Editable appendRowToSheet(Row row, SheetIndex index);
62 |
63 | Editable refreshFormulas();
64 |
65 | }
66 |
67 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/workbook/ExcelWorkbook.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.workbook;
18 |
19 | import java.io.IOException;
20 | import java.io.InputStream;
21 |
22 | public interface ExcelWorkbook {
23 |
24 | InputStream getInputStream() throws IOException;
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/workbook/Navigable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.workbook;
18 |
19 | import bad.robot.excel.cell.Cell;
20 | import bad.robot.excel.row.Row;
21 | import bad.robot.excel.row.RowIndex;
22 | import bad.robot.excel.sheet.Coordinate;
23 | import bad.robot.excel.sheet.SheetIndex;
24 |
25 | /** @since 1.1 */
26 | public interface Navigable {
27 |
28 | Cell getCellAt(Coordinate coordinate);
29 |
30 | Row getRowAt(RowIndex rowIndex, SheetIndex sheetIndex);
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/workbook/PoiWorkbookReader.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.workbook;
18 |
19 | import org.apache.poi.ss.usermodel.Workbook;
20 | import org.apache.poi.ss.usermodel.WorkbookFactory;
21 |
22 | import java.io.File;
23 | import java.io.FileInputStream;
24 | import java.io.IOException;
25 | import java.io.InputStream;
26 |
27 | public class PoiWorkbookReader implements Readable {
28 |
29 | @Override
30 | public Workbook read(File file) throws IOException {
31 | InputStream stream = new FileInputStream(file);
32 | return read(stream);
33 | }
34 |
35 | @Override
36 | public Workbook read(InputStream stream) throws IOException {
37 | if (stream == null)
38 | throw new IllegalArgumentException("stream was null, could not load the workbook");
39 | try {
40 | return WorkbookFactory.create(stream);
41 | } finally {
42 | stream.close();
43 | }
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/workbook/PoiWorkbookWriter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.workbook;
18 |
19 | import org.apache.poi.hssf.usermodel.HSSFWorkbook;
20 | import org.apache.poi.ss.usermodel.Workbook;
21 |
22 | import java.io.File;
23 | import java.io.FileOutputStream;
24 | import java.io.IOException;
25 | import java.io.OutputStream;
26 |
27 | import static java.io.File.createTempFile;
28 |
29 | public class PoiWorkbookWriter implements Writable {
30 |
31 | private final Workbook workbook;
32 |
33 | public PoiWorkbookWriter(Workbook workbook) {
34 | this.workbook = workbook;
35 | }
36 |
37 | @Override
38 | public void writeTo(OutputStream stream) {
39 | try {
40 | workbook.write(stream);
41 | } catch (IOException e) {
42 | throw new RuntimeException(e);
43 | }
44 | }
45 |
46 | @Override
47 | public void saveAs(File file) {
48 | try {
49 | FileOutputStream stream = new FileOutputStream(file);
50 | workbook.write(stream);
51 | stream.close();
52 | } catch (IOException e) {
53 | throw new RuntimeException(e);
54 | }
55 | }
56 |
57 | @Override
58 | public void saveAsTemporaryFile(String filenameExcludingExtension) {
59 | String extension = workbook instanceof HSSFWorkbook ? ".xls" : ".xlsx";
60 | try {
61 | File file = createTempFile(filenameExcludingExtension, extension);
62 | saveAs(file);
63 | System.out.printf("saved %s: %s%n", extension, file.getAbsolutePath());
64 | } catch (IOException e) {
65 | throw new RuntimeException(String.format("failed to save %s: %s.%s", extension, filenameExcludingExtension, extension), e);
66 | }
67 |
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/workbook/Readable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.workbook;
18 |
19 | import org.apache.poi.ss.usermodel.Workbook;
20 |
21 | import java.io.File;
22 | import java.io.IOException;
23 | import java.io.InputStream;
24 |
25 | public interface Readable {
26 |
27 | Workbook read(File file) throws IOException;
28 |
29 | Workbook read(InputStream stream) throws IOException;
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/workbook/WorkbookType.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.workbook;
18 |
19 | import org.apache.poi.hssf.usermodel.HSSFWorkbook;
20 | import org.apache.poi.ss.usermodel.Workbook;
21 | import org.apache.poi.xssf.usermodel.XSSFWorkbook;
22 |
23 | public enum WorkbookType {
24 | XLS("Excel Binary File Format (pre 2007)") {
25 | @Override
26 | public Workbook create() {
27 | return new HSSFWorkbook();
28 | }
29 | },
30 | XML("Office Open XML") {
31 | @Override
32 | public Workbook create() {
33 | return new XSSFWorkbook();
34 | }
35 | };
36 |
37 | private final String description;
38 |
39 | WorkbookType(String description) {
40 | this.description = description;
41 | }
42 |
43 | public abstract Workbook create();
44 |
45 | public String getDescription() {
46 | return description;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/main/java/bad/robot/excel/workbook/Writable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.workbook;
18 |
19 | import java.io.File;
20 | import java.io.OutputStream;
21 |
22 | public interface Writable {
23 |
24 | void writeTo(OutputStream stream);
25 |
26 | void saveAs(File file);
27 |
28 | void saveAsTemporaryFile(String filenameExcludingExtension);
29 | }
30 |
--------------------------------------------------------------------------------
/src/test/java/bad/robot/excel/DateCellTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel;
18 |
19 | import bad.robot.excel.cell.DateCell;
20 | import bad.robot.excel.cell.DoubleCell;
21 | import bad.robot.excel.sheet.Coordinate;
22 | import bad.robot.excel.workbook.PoiWorkbook;
23 | import org.apache.poi.hssf.usermodel.HSSFCell;
24 | import org.apache.poi.hssf.usermodel.HSSFRow;
25 | import org.apache.poi.hssf.usermodel.HSSFSheet;
26 | import org.apache.poi.hssf.usermodel.HSSFWorkbook;
27 | import org.apache.poi.ss.usermodel.Workbook;
28 | import org.junit.Test;
29 |
30 | import java.io.IOException;
31 |
32 | import static bad.robot.excel.DateUtil.createDate;
33 | import static bad.robot.excel.WorkbookResource.*;
34 | import static bad.robot.excel.column.ColumnIndex.column;
35 | import static bad.robot.excel.column.ExcelColumnIndex.A;
36 | import static bad.robot.excel.column.ExcelColumnIndex.B;
37 | import static bad.robot.excel.matchers.CellMatcher.equalTo;
38 | import static bad.robot.excel.sheet.Coordinate.coordinate;
39 | import static java.util.Calendar.MARCH;
40 | import static java.util.Calendar.OCTOBER;
41 | import static org.hamcrest.MatcherAssert.assertThat;
42 | import static org.hamcrest.Matchers.is;
43 |
44 | public class DateCellTest {
45 |
46 | private final DateCell cell = new DateCell(createDate(12, OCTOBER, 2013));
47 |
48 | @Test
49 | public void shouldSetDataFormatWhenAddingACell() throws IOException {
50 | HSSFWorkbook workbook = new HSSFWorkbook();
51 | HSSFSheet sheet = workbook.createSheet();
52 | HSSFRow row = sheet.createRow(0);
53 | cell.addTo(row, column(A), workbook);
54 | assertThat(getCellDataFormatAtCoordinate(coordinate(A, 1), workbook), is("dd-MMM-yyyy"));
55 | }
56 |
57 | @Test
58 | public void shouldSetDataFormatWhenReplacingACell() throws IOException {
59 | HSSFWorkbook workbook = new HSSFWorkbook();
60 | HSSFSheet sheet = workbook.createSheet();
61 | HSSFRow row = sheet.createRow(0);
62 | HSSFCell original = row.createCell(0);
63 | cell.update(original, workbook);
64 | assertThat(getCellDataFormatAtCoordinate(coordinate(A, 1), workbook), is("dd-MMM-yyyy"));
65 | }
66 |
67 | @Test
68 | public void replaceNonDateCellWithACell() throws IOException {
69 | Workbook workbook = getWorkbook("cellTypes.xls");
70 | PoiWorkbook sheet = new PoiWorkbook(workbook);
71 | Coordinate coordinate = coordinate(B, 2);
72 |
73 | assertThat(getCellForCoordinate(coordinate, workbook), equalTo(new DoubleCell(1001d)));
74 | sheet.replaceCell(coordinate, createDate(15, MARCH, 2012));
75 |
76 | assertThat(getCellForCoordinate(coordinate, workbook), equalTo(new DateCell(createDate(15, MARCH, 2012))));
77 | assertThat(getCellDataFormatAtCoordinate(coordinate, workbook), is("dd-MMM-yyyy"));
78 | assertThat("should not have affected a shared data format", getCellDataFormatAtCoordinate(coordinate(B, 7), workbook), is("General"));
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/src/test/java/bad/robot/excel/DateUtil.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel;
18 |
19 | import java.util.Calendar;
20 | import java.util.Date;
21 |
22 | import static java.util.Calendar.*;
23 |
24 | public class DateUtil {
25 |
26 | public static Date createDate(int day, int month, int year) {
27 | Calendar calendar = getInstance();
28 | calendar.set(DAY_OF_MONTH, day);
29 | calendar.set(MONTH, month);
30 | calendar.set(YEAR, year);
31 | calendar.set(HOUR_OF_DAY, 0);
32 | calendar.set(MINUTE, 0);
33 | calendar.set(SECOND, 0);
34 | calendar.set(MILLISECOND, 0);
35 | return calendar.getTime();
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/test/java/bad/robot/excel/PoiToExcelCoercionsTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel;
18 |
19 | import org.apache.poi.ss.usermodel.Cell;
20 | import org.hamcrest.Matcher;
21 | import org.jmock.Expectations;
22 | import org.jmock.Mockery;
23 | import org.junit.Test;
24 |
25 | import static bad.robot.excel.PoiToExcelCoercions.asExcelCoordinate;
26 | import static bad.robot.excel.PoiToExcelCoercionsTest.PoiCoordinate.coordinate;
27 | import static org.hamcrest.Matchers.is;
28 | import static org.junit.Assert.assertThat;
29 |
30 | public class PoiToExcelCoercionsTest {
31 |
32 | private final Mockery context = new Mockery();
33 | private final Cell cell = context.mock(Cell.class);
34 |
35 | @Test
36 | public void shouldConvertFromPoiToExcelCoordinates() {
37 | verify(coordinate(0, 0), is("A1"));
38 | verify(coordinate(1, 1), is("B2"));
39 | verify(coordinate(10, 5), is("K6"));
40 | verify(coordinate(26, 11), is("AA12"));
41 | verify(coordinate(69, 15), is("BR16"));
42 | }
43 |
44 | private void verify(PoiCoordinate coordinate, Matcher matcher) {
45 | expectingCellAt(coordinate);
46 | assertThat(asExcelCoordinate(cell), matcher);
47 | context.assertIsSatisfied();
48 | }
49 |
50 | private void expectingCellAt(final PoiCoordinate coordinate) {
51 | context.checking(new Expectations() {{
52 | oneOf(cell).getColumnIndex(); will(returnValue(coordinate.column));
53 | oneOf(cell).getRowIndex(); will(returnValue(coordinate.row));
54 | }});
55 | }
56 |
57 | static class PoiCoordinate {
58 | private final int column;
59 | private final int row;
60 |
61 | private PoiCoordinate(int column, int row) {
62 | this.column = column;
63 | this.row = row;
64 | }
65 |
66 | static PoiCoordinate coordinate(int column, int row) {
67 | return new PoiCoordinate(column, row);
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/test/java/bad/robot/excel/WorkbookResource.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel;
18 |
19 | import bad.robot.excel.sheet.Coordinate;
20 | import bad.robot.excel.workbook.PoiWorkbookReader;
21 | import org.apache.poi.ss.usermodel.Sheet;
22 | import org.apache.poi.ss.usermodel.Workbook;
23 |
24 | import java.io.IOException;
25 | import java.io.InputStream;
26 |
27 | public class WorkbookResource {
28 |
29 | /** test only "loader" with a default location for loading workbooks **/
30 | public static Workbook getWorkbook(String file) throws IOException {
31 | InputStream stream = WorkbookResource.class.getResourceAsStream(file);
32 | return new PoiWorkbookReader().read(stream);
33 | }
34 |
35 | public static Sheet firstSheetOf(String file) throws IOException {
36 | return getWorkbook(file).getSheetAt(0);
37 | }
38 |
39 | public static org.apache.poi.ss.usermodel.Row firstRowOf(String file) throws IOException {
40 | return firstSheetOf(file).getRow(0);
41 | }
42 |
43 | public static org.apache.poi.ss.usermodel.Row secondRowOf(String file) throws IOException {
44 | return firstSheetOf(file).getRow(1);
45 | }
46 |
47 | public static org.apache.poi.ss.usermodel.Row thirdRowOf(String file) throws IOException {
48 | return firstSheetOf(file).getRow(2);
49 | }
50 |
51 | public static org.apache.poi.ss.usermodel.Row firstRowOf(Sheet sheet) {
52 | return sheet.getRow(0);
53 | }
54 |
55 | public static org.apache.poi.ss.usermodel.Cell getCellForCoordinate(Coordinate coordinate, Workbook workbook) throws IOException {
56 | org.apache.poi.ss.usermodel.Row row = getRowForCoordinate(coordinate, workbook);
57 | return row.getCell(coordinate.getColumn().value());
58 | }
59 |
60 | public static org.apache.poi.ss.usermodel.Row getRowForCoordinate(Coordinate coordinate, Workbook workbook) throws IOException {
61 | Sheet sheet = workbook.getSheetAt(coordinate.getSheet().value());
62 | org.apache.poi.ss.usermodel.Row row = sheet.getRow(coordinate.getRow().value());
63 | if (row == null)
64 | throw new IllegalStateException("expected to find a row");
65 | return row;
66 | }
67 |
68 | public static String getCellDataFormatAtCoordinate(Coordinate coordinate, Workbook workbook) throws IOException {
69 | return getCellForCoordinate(coordinate, workbook).getCellStyle().getDataFormatString();
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/src/test/java/bad/robot/excel/cell/StringCellTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.cell;
18 |
19 | import org.junit.Test;
20 |
21 | import static org.hamcrest.MatcherAssert.assertThat;
22 | import static org.hamcrest.Matchers.is;
23 |
24 | public class StringCellTest {
25 |
26 | @Test
27 | public void stringRepresentation() {
28 | assertThat(new StringCell("Value").toString(), is("\"Value\""));
29 | }
30 |
31 | @Test
32 | public void equalityOfDifferingTypes() {
33 | assertThat(new StringCell("Foo").equals(new BlankCell()), is(false));
34 | }
35 |
36 | @Test
37 | public void basicEquality() {
38 | assertThat(new StringCell("Bar").equals(new StringCell("Bar")), is(true));
39 | }
40 |
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/src/test/java/bad/robot/excel/column/ExcelColumnIndexTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.column;
18 |
19 | import org.junit.Test;
20 |
21 | import static bad.robot.excel.column.ExcelColumnIndex.*;
22 | import static org.hamcrest.Matchers.is;
23 | import static org.junit.Assert.assertThat;
24 |
25 | public class ExcelColumnIndexTest {
26 |
27 | @Test
28 | public void convertToIndex() {
29 | assertThat(A.ordinal(), is(0));
30 | assertThat(B.ordinal(), is(1));
31 | assertThat(C.ordinal(), is(2));
32 | assertThat(X.ordinal(), is(23));
33 | assertThat(Y.ordinal(), is(24));
34 | assertThat(Z.ordinal(), is(25));
35 | }
36 |
37 | @Test
38 | public void convertRowCoordinateToExcelRowForExtendedAlphabet() {
39 | assertThat(AA.ordinal(),is(26));
40 | assertThat(ZZ.ordinal(), is(701));
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/test/java/bad/robot/excel/matchers/Assertions.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.hamcrest.Matcher;
20 |
21 | import static org.hamcrest.MatcherAssert.assertThat;
22 |
23 | public class Assertions {
24 |
25 | public static void assertTimezone(Matcher matcher) {
26 | assertThat("this test is timezone sensitive, you may need to set the user.timezone system property", System.getProperty("user.timezone"), matcher);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/test/java/bad/robot/excel/matchers/CellMatcherTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import bad.robot.excel.cell.BooleanCell;
20 | import bad.robot.excel.cell.DoubleCell;
21 | import bad.robot.excel.cell.StringCell;
22 | import org.hamcrest.StringDescription;
23 | import org.junit.Test;
24 |
25 | import static bad.robot.excel.matchers.CellMatcher.equalTo;
26 | import static bad.robot.excel.matchers.StubCell.createCell;
27 | import static org.hamcrest.Matchers.is;
28 | import static org.junit.Assert.assertThat;
29 |
30 | public class CellMatcherTest {
31 |
32 | private final StringDescription description = new StringDescription();
33 |
34 | @Test
35 | public void usageExample() {
36 | assertThat(createCell("Text"), equalTo(new StringCell("Text")));
37 | }
38 |
39 | @Test
40 | public void matches() {
41 | assertThat(equalTo(new DoubleCell(0.44444d)).matches(createCell(0.44444d)), is(true));
42 | }
43 |
44 | @Test
45 | public void doesNotMatch() {
46 | assertThat(equalTo(new DoubleCell(0.44444d)).matches(createCell(0.4444d)), is(false));
47 | }
48 |
49 | @Test
50 | public void description() {
51 | equalTo(new DoubleCell(0.44444d)).describeTo(description);
52 | assertThat(description.toString(), is("<0.44444D>"));
53 | }
54 |
55 | @Test
56 | public void mismatch() {
57 | ((CellMatcher) equalTo(new BooleanCell(false))).matchesSafely(createCell(true), description);
58 | assertThat(description.toString(), is("cell at \"A1\" contained expected "));
59 | }
60 |
61 | }
62 |
--------------------------------------------------------------------------------
/src/test/java/bad/robot/excel/matchers/CellNumberMatcherTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.apache.poi.ss.usermodel.Row;
20 | import org.hamcrest.Description;
21 | import org.hamcrest.StringDescription;
22 | import org.junit.Before;
23 | import org.junit.Test;
24 |
25 | import java.io.IOException;
26 |
27 | import static bad.robot.excel.WorkbookResource.firstRowOf;
28 | import static bad.robot.excel.matchers.CellNumberMatcher.hasSameNumberOfCellsAs;
29 | import static org.hamcrest.MatcherAssert.assertThat;
30 | import static org.hamcrest.Matchers.is;
31 | import static org.hamcrest.Matchers.not;
32 |
33 | public class CellNumberMatcherTest {
34 |
35 | private Row rowWithThreeCells;
36 | private Row rowWithTwoCells;
37 |
38 | @Before
39 | public void loadWorkbookAndSheets() throws IOException {
40 | rowWithThreeCells = firstRowOf("rowWithThreeCells.xls");
41 | rowWithTwoCells = firstRowOf("rowWithTwoCells.xls");
42 | }
43 |
44 | @Test
45 | public void exampleUsages() throws Exception {
46 | assertThat(rowWithThreeCells, hasSameNumberOfCellsAs(rowWithThreeCells));
47 | assertThat(rowWithTwoCells, not(hasSameNumberOfCellsAs(rowWithThreeCells)));
48 | }
49 |
50 | @Test
51 | public void matches() {
52 | assertThat(hasSameNumberOfCellsAs(rowWithThreeCells).matches(rowWithThreeCells), is(true));
53 | }
54 |
55 | @Test
56 | public void doesNotMatch() {
57 | assertThat(hasSameNumberOfCellsAs(rowWithThreeCells).matches(rowWithTwoCells), is(false));
58 | }
59 |
60 | @Test
61 | public void description() {
62 | Description description = new StringDescription();
63 | hasSameNumberOfCellsAs(rowWithThreeCells).describeTo(description);
64 | assertThat(description.toString(), is("<3> cell(s) on row <1> sheet \"Sheet1\""));
65 | }
66 |
67 | @Test
68 | public void mismatch() {
69 | Description description = new StringDescription();
70 | hasSameNumberOfCellsAs(rowWithThreeCells).matchesSafely(rowWithTwoCells, description);
71 | assertThat(description.toString(), is("got <2> cell(s) on row <1> expected <3> sheet \"Sheet1\""));
72 | }
73 |
74 | @Test
75 | public void differentNumberOfPhysicalCells() throws IOException {
76 | Row rowWithThreePhysicalCells = firstRowOf("rowWithThreePhysicalCells.xls");
77 | Row rowWithThreeCellsButOnlyTwoPhysicalCells = firstRowOf("rowWithThreeCellsButOnlyTwoPhysicalCells.xls");
78 | Description description = new StringDescription();
79 | hasSameNumberOfCellsAs(rowWithThreeCellsButOnlyTwoPhysicalCells).matchesSafely(rowWithThreePhysicalCells, description);
80 | assertThat(description.toString(), is("got <3> cell(s) containing value on row <1> expected <2> sheet \"Sheet1\""));
81 | }
82 |
83 | }
84 |
--------------------------------------------------------------------------------
/src/test/java/bad/robot/excel/matchers/CellTypeTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import bad.robot.excel.cell.*;
20 | import org.apache.poi.ss.usermodel.Workbook;
21 | import org.junit.Before;
22 | import org.junit.Test;
23 |
24 | import java.io.IOException;
25 |
26 | import static bad.robot.excel.WorkbookResource.getCellForCoordinate;
27 | import static bad.robot.excel.WorkbookResource.getWorkbook;
28 | import static bad.robot.excel.column.ExcelColumnIndex.B;
29 | import static bad.robot.excel.column.ExcelColumnIndex.C;
30 | import static bad.robot.excel.matchers.CellType.adaptPoi;
31 | import static bad.robot.excel.sheet.Coordinate.coordinate;
32 | import static org.hamcrest.MatcherAssert.assertThat;
33 | import static org.hamcrest.Matchers.instanceOf;
34 | import static org.hamcrest.Matchers.is;
35 |
36 | public class CellTypeTest {
37 |
38 | private Workbook workbook;
39 |
40 | @Before
41 | public void loadWorkbook() throws IOException {
42 | workbook = getWorkbook("cellTypes.xls");
43 | }
44 |
45 | @Test
46 | public void adaptBooleanCell() throws IOException {
47 | assertThat(adaptPoi(getCellForCoordinate(coordinate(B, 6), workbook)), is(instanceOf(BooleanCell.class)));
48 | }
49 |
50 | @Test
51 | public void adaptFormulaCell() throws IOException {
52 | assertThat(adaptPoi(getCellForCoordinate(coordinate(B, 7), workbook)), is(instanceOf(FormulaCell.class)));
53 | }
54 |
55 | @Test
56 | public void adaptFormulaTypeToError() throws IOException {
57 | assertThat(adaptPoi(getCellForCoordinate(coordinate(B, 8), workbook)), is(instanceOf(ErrorCell.class)));
58 | }
59 |
60 | @Test
61 | public void adaptNumericTypeToDoubleCell() throws IOException {
62 | assertThat(adaptPoi(getCellForCoordinate(coordinate(B, 2), workbook)), is(instanceOf(DoubleCell.class)));
63 | assertThat(adaptPoi(getCellForCoordinate(coordinate(B, 3), workbook)), is(instanceOf(DoubleCell.class)));
64 | }
65 |
66 | @Test
67 | public void adaptNumericTypeToDateCell() throws IOException {
68 | assertThat(adaptPoi(getCellForCoordinate(coordinate(B, 1), workbook)), is(instanceOf(DateCell.class)));
69 | }
70 |
71 | @Test
72 | public void adaptStringCell() throws Exception {
73 | assertThat(adaptPoi(getCellForCoordinate(coordinate(B, 4), workbook)), is(instanceOf(StringCell.class)));
74 | }
75 |
76 | @Test
77 | public void adaptHyperlinkCell() throws Exception {
78 | assertThat(adaptPoi(getCellForCoordinate(coordinate(B, 5), workbook)), is(instanceOf(HyperlinkCell.class)));
79 | }
80 |
81 | @Test
82 | public void adaptInternalLinkCell() throws IOException {
83 | assertThat(adaptPoi(getCellForCoordinate(coordinate(B, 9), workbook)), is(instanceOf(StringCell.class)));
84 | }
85 |
86 | @Test
87 | public void adaptBlankCell() throws IOException {
88 | assertThat(adaptPoi(getCellForCoordinate(coordinate(C, 1), workbook)), is(instanceOf(BlankCell.class)));
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/src/test/java/bad/robot/excel/matchers/RowInSheetMatcherTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.apache.poi.ss.usermodel.Sheet;
20 | import org.hamcrest.StringDescription;
21 | import org.junit.Before;
22 | import org.junit.Test;
23 |
24 | import java.io.IOException;
25 |
26 | import static bad.robot.excel.WorkbookResource.firstRowOf;
27 | import static bad.robot.excel.WorkbookResource.firstSheetOf;
28 | import static bad.robot.excel.matchers.RowInSheetMatcher.hasSameRow;
29 | import static org.hamcrest.MatcherAssert.assertThat;
30 | import static org.hamcrest.Matchers.is;
31 | import static org.hamcrest.Matchers.not;
32 |
33 | public class RowInSheetMatcherTest {
34 |
35 | private final StringDescription description = new StringDescription();
36 |
37 | private Sheet sheetWithThreeCells;
38 | private Sheet sheetWithThreeCellsAlternativeValues;
39 | private Sheet sheetWithTwoCells;
40 |
41 | @Before
42 | public void loadWorkbooks() throws IOException {
43 | sheetWithThreeCells = firstSheetOf("rowWithThreeCells.xls");
44 | sheetWithThreeCellsAlternativeValues = firstSheetOf("rowWithThreeCellsAlternativeValues.xls");
45 | sheetWithTwoCells = firstSheetOf("rowWithTwoCells.xls");
46 | }
47 |
48 | @Test
49 | public void exampleUsage() throws IOException {
50 | assertThat(sheetWithTwoCells, hasSameRow(firstRowOf(sheetWithTwoCells)));
51 | assertThat(sheetWithThreeCells, not(hasSameRow(firstRowOf(sheetWithTwoCells))));
52 | }
53 |
54 | @Test
55 | public void matches() {
56 | assertThat(hasSameRow(firstRowOf(sheetWithThreeCells)).matches(sheetWithThreeCells), is(true));
57 | }
58 |
59 | @Test
60 | public void doesNotMatch() {
61 | assertThat(hasSameRow(firstRowOf(sheetWithThreeCells)).matches(sheetWithThreeCellsAlternativeValues), is(false));
62 | }
63 |
64 | @Test
65 | public void description() {
66 | hasSameRow(firstRowOf(sheetWithThreeCells)).describeTo(description);
67 | assertThat(description.toString(), is("equality of row <1>"));
68 | }
69 |
70 | @Test
71 | public void mismatch() {
72 | hasSameRow(firstRowOf(sheetWithThreeCells)).matchesSafely(sheetWithThreeCellsAlternativeValues, description);
73 | assertThat(description.toString(), is("cell at \"B1\" contained <3.14D> expected <\"C2, R1\"> sheet \"Sheet1\""));
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/src/test/java/bad/robot/excel/matchers/RowMissingMatcherTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.apache.poi.ss.usermodel.Row;
20 | import org.hamcrest.StringDescription;
21 | import org.junit.Before;
22 | import org.junit.Test;
23 |
24 | import java.io.IOException;
25 |
26 | import static bad.robot.excel.WorkbookResource.thirdRowOf;
27 | import static bad.robot.excel.matchers.RowMissingMatcher.rowIsPresent;
28 | import static org.hamcrest.MatcherAssert.assertThat;
29 | import static org.hamcrest.Matchers.is;
30 | import static org.hamcrest.Matchers.not;
31 |
32 | public class RowMissingMatcherTest {
33 |
34 | private final StringDescription description = new StringDescription();
35 |
36 | private Row thirdRow;
37 | private Row sheetWithThreeRows;
38 |
39 | @Before
40 | public void loadWorkbooks() throws IOException {
41 | thirdRow = thirdRowOf("sheetWithTwoRows.xls");
42 | sheetWithThreeRows = thirdRowOf("sheetWithThreeRows.xls");
43 | }
44 |
45 | @Test
46 | public void usageExample() throws Exception {
47 | assertThat(thirdRow, not(rowIsPresent(sheetWithThreeRows)));
48 | }
49 |
50 | @Test
51 | public void matches() {
52 | assertThat(rowIsPresent(sheetWithThreeRows).matches(sheetWithThreeRows), is(true));
53 | }
54 |
55 | @Test
56 | public void doesNotMatch() {
57 | assertThat(rowIsPresent(sheetWithThreeRows).matches(thirdRow), is(false));
58 | }
59 |
60 | @Test
61 | public void describe() {
62 | rowIsPresent(sheetWithThreeRows).describeTo(description);
63 | assertThat(description.toString(), is("row <3> to be present in sheet \"Sheet1\""));
64 | }
65 |
66 | @Test
67 | public void mismatch() {
68 | rowIsPresent(sheetWithThreeRows).matchesSafely(thirdRow, description);
69 | assertThat(description.toString(), is("row <3> is missing in sheet \"Sheet1\""));
70 | }
71 |
72 | }
73 |
--------------------------------------------------------------------------------
/src/test/java/bad/robot/excel/matchers/RowNumberMatcherTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.apache.poi.ss.usermodel.Sheet;
20 | import org.hamcrest.Description;
21 | import org.hamcrest.StringDescription;
22 | import org.junit.Before;
23 | import org.junit.Test;
24 |
25 | import java.io.IOException;
26 |
27 | import static bad.robot.excel.WorkbookResource.firstSheetOf;
28 | import static bad.robot.excel.matchers.RowNumberMatcher.hasSameNumberOfRowAs;
29 | import static org.hamcrest.MatcherAssert.assertThat;
30 | import static org.hamcrest.Matchers.is;
31 | import static org.hamcrest.Matchers.not;
32 |
33 | public class RowNumberMatcherTest {
34 |
35 | private Sheet sheetWithThreeRows;
36 | private Sheet sheetWithTwoRows;
37 |
38 | @Before
39 | public void loadWorkbookAndSheets() throws IOException {
40 | sheetWithThreeRows = firstSheetOf("sheetWithThreeRows.xls");
41 | sheetWithTwoRows = firstSheetOf("sheetWithTwoRows.xls");
42 | }
43 |
44 | @Test
45 | public void exampleUsages() throws Exception {
46 | assertThat(sheetWithThreeRows, hasSameNumberOfRowAs(sheetWithThreeRows));
47 | assertThat(sheetWithTwoRows, not(hasSameNumberOfRowAs(sheetWithThreeRows)));
48 | }
49 |
50 | @Test
51 | public void matches() {
52 | assertThat(hasSameNumberOfRowAs(sheetWithThreeRows).matches(sheetWithThreeRows), is(true));
53 | }
54 |
55 | @Test
56 | public void doesNotMatch() {
57 | assertThat(hasSameNumberOfRowAs(sheetWithThreeRows).matches(sheetWithTwoRows), is(false));
58 | }
59 |
60 | @Test
61 | public void description() {
62 | Description description = new StringDescription();
63 | hasSameNumberOfRowAs(sheetWithThreeRows).describeTo(description);
64 | assertThat(description.toString(), is("<3> row(s) in sheet \"Sheet1\""));
65 | }
66 |
67 | @Test
68 | public void mismatch() {
69 | Description description = new StringDescription();
70 | hasSameNumberOfRowAs(sheetWithThreeRows).matchesSafely(sheetWithTwoRows, description);
71 | assertThat(description.toString(), is("got <2> row(s) in sheet \"Sheet1\" expected <3>"));
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/test/java/bad/robot/excel/matchers/RowsMatcherTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.apache.poi.ss.usermodel.Sheet;
20 | import org.hamcrest.StringDescription;
21 | import org.junit.Before;
22 | import org.junit.Test;
23 |
24 | import java.io.IOException;
25 |
26 | import static bad.robot.excel.WorkbookResource.firstSheetOf;
27 | import static bad.robot.excel.matchers.RowsMatcher.hasSameRowsAs;
28 | import static org.hamcrest.MatcherAssert.assertThat;
29 | import static org.hamcrest.Matchers.*;
30 |
31 | public class RowsMatcherTest {
32 |
33 | private final StringDescription description = new StringDescription();
34 |
35 | private Sheet sheetWithDifferingValues;
36 | private Sheet sheetWithThreeRows;
37 | private Sheet sheetWithTwoRows;
38 |
39 | @Before
40 | public void loadWorkbooks() throws IOException {
41 | sheetWithThreeRows = firstSheetOf("sheetWithThreeRows.xls");
42 | sheetWithDifferingValues = firstSheetOf("sheetWithThreeRowsWithAlternativeValues.xls");
43 | sheetWithTwoRows = firstSheetOf("sheetWithTwoRows.xls");
44 | }
45 |
46 | @Test
47 | public void exampleUsage() throws IOException {
48 | assertThat(sheetWithDifferingValues, not(hasSameRowsAs(sheetWithThreeRows)));
49 | assertThat(sheetWithTwoRows, not(hasSameRowsAs(sheetWithThreeRows)));
50 | assertThat(sheetWithThreeRows, hasSameRowsAs(sheetWithTwoRows));
51 | }
52 |
53 | @Test
54 | public void matches() throws IOException {
55 | assertThat(hasSameRowsAs(sheetWithThreeRows).matches(sheetWithThreeRows), is(true));
56 | }
57 |
58 | @Test
59 | public void doesNotMatch() {
60 | assertThat(hasSameRowsAs(sheetWithThreeRows).matches(sheetWithDifferingValues), is(false));
61 | }
62 |
63 | @Test
64 | public void description() throws IOException {
65 | hasSameRowsAs(sheetWithThreeRows).describeTo(description);
66 | assertThat(description.toString(), containsString("equality on all rows in \"Sheet1\""));
67 | }
68 |
69 | @Test
70 | public void mismatch() throws IOException {
71 | hasSameRowsAs(sheetWithThreeRows).matchesSafely(sheetWithDifferingValues, description);
72 | assertThat(description.toString(), containsString("cell at \"A2\" contained <\"XXX\"> expected <\"Row 2\">"));
73 | }
74 |
75 | @Test
76 | public void mismatchOnMissingRow() {
77 | hasSameRowsAs(sheetWithThreeRows).matchesSafely(sheetWithTwoRows, description);
78 | assertThat(description.toString(), is("row <3> is missing in sheet \"Sheet1\""));
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/src/test/java/bad/robot/excel/matchers/SheetMatcherTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.apache.poi.ss.usermodel.Workbook;
20 | import org.hamcrest.Description;
21 | import org.hamcrest.StringDescription;
22 | import org.junit.Before;
23 | import org.junit.Test;
24 |
25 | import java.io.IOException;
26 |
27 | import static bad.robot.excel.WorkbookResource.getWorkbook;
28 | import static bad.robot.excel.matchers.SheetMatcher.hasSameSheetsAs;
29 | import static org.hamcrest.MatcherAssert.assertThat;
30 | import static org.hamcrest.Matchers.*;
31 |
32 | public class SheetMatcherTest {
33 |
34 | private final Description description = new StringDescription();
35 |
36 | private Workbook workbookWithOneNamedSheet;
37 | private Workbook workbookWithManyNamedSheets;
38 | private Workbook workbookWithMultipleAlternativelyNamedSheets;
39 |
40 | @Before
41 | public void loadWorkbooks() throws IOException {
42 | workbookWithOneNamedSheet = getWorkbook("workbookWithOneSheet.xls");
43 | workbookWithManyNamedSheets = getWorkbook("workbookWithMultipleNamedSheets.xls");
44 | workbookWithMultipleAlternativelyNamedSheets = getWorkbook("workbookWithMultipleAlternativelyNamedSheets.xls");
45 | }
46 |
47 | @Test
48 | public void exampleUsages() {
49 | assertThat(workbookWithOneNamedSheet, not(hasSameSheetsAs(workbookWithManyNamedSheets)));
50 | }
51 |
52 | @Test
53 | public void matches() {
54 | assertThat(hasSameSheetsAs(workbookWithManyNamedSheets).matches(workbookWithManyNamedSheets), is(true));
55 | }
56 |
57 | @Test
58 | public void doesNotMatch() {
59 | assertThat(hasSameSheetsAs(workbookWithManyNamedSheets).matches(workbookWithOneNamedSheet), is(false));
60 | }
61 |
62 | @Test
63 | public void description() {
64 | hasSameSheetsAs(workbookWithManyNamedSheets).describeTo(description);
65 | assertThat(description.toString(), is("<4> sheet(s) named \"Sheet1\", \"Another Sheet\", \"Yet Another Sheet\", \"Sheet5\""));
66 | }
67 |
68 | @Test
69 | public void mismatchOnNumberOfSheets() {
70 | hasSameSheetsAs(workbookWithManyNamedSheets).matchesSafely(workbookWithOneNamedSheet, description);
71 | assertThat(description.toString(), allOf(
72 | containsString("got <1> sheet(s) expected <4>,"),
73 | containsString("sheet(s) \"Another Sheet\", \"Yet Another Sheet\", \"Sheet5\" were missing")
74 | ));
75 | }
76 |
77 | @Test
78 | public void mismatchOnNameOfSheets() {
79 | hasSameSheetsAs(workbookWithManyNamedSheets).matchesSafely(workbookWithMultipleAlternativelyNamedSheets, description);
80 | assertThat(description.toString(), is("sheet(s) \"Yet Another Sheet\" was missing"));
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/src/test/java/bad/robot/excel/matchers/SheetNumberMatcherTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.apache.poi.ss.usermodel.Workbook;
20 | import org.hamcrest.Description;
21 | import org.hamcrest.StringDescription;
22 | import org.junit.Before;
23 | import org.junit.Test;
24 |
25 | import java.io.IOException;
26 |
27 | import static bad.robot.excel.WorkbookResource.getWorkbook;
28 | import static bad.robot.excel.matchers.SheetNumberMatcher.hasSameNumberOfSheetsAs;
29 | import static org.hamcrest.MatcherAssert.assertThat;
30 | import static org.hamcrest.Matchers.is;
31 | import static org.hamcrest.Matchers.not;
32 |
33 | public class SheetNumberMatcherTest {
34 |
35 | private final Description description = new StringDescription();
36 |
37 | private Workbook workbookWithOneSheet;
38 | private Workbook workbookWithTwoSheets;
39 |
40 | @Before
41 | public void loadWorkbooks() throws IOException {
42 | workbookWithOneSheet = getWorkbook("workbookWithOneSheet.xls");
43 | workbookWithTwoSheets = getWorkbook("workbookWithTwoSheets.xls");
44 | }
45 |
46 | @Test
47 | public void exampleUsages() throws Exception {
48 | assertThat(workbookWithOneSheet, hasSameNumberOfSheetsAs(workbookWithOneSheet));
49 | assertThat(workbookWithOneSheet, not(hasSameNumberOfSheetsAs(workbookWithTwoSheets)));
50 | }
51 |
52 | @Test
53 | public void matches() {
54 | assertThat(hasSameNumberOfSheetsAs(workbookWithOneSheet).matches(workbookWithOneSheet), is(true));
55 | }
56 |
57 | @Test
58 | public void doesNotMatch() {
59 | assertThat(hasSameNumberOfSheetsAs(workbookWithOneSheet).matches(workbookWithTwoSheets), is(false));
60 | }
61 |
62 | @Test
63 | public void description() {
64 | hasSameNumberOfSheetsAs(workbookWithOneSheet).describeTo(description);
65 | assertThat(description.toString(), is("<1> sheet(s)"));
66 | }
67 |
68 | @Test
69 | public void mismatch() {
70 | hasSameNumberOfSheetsAs(workbookWithOneSheet).matchesSafely(workbookWithTwoSheets, description);
71 | assertThat(description.toString(), is("got <2> sheet(s) expected <1>"));
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/test/java/bad/robot/excel/matchers/StubCell.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.matchers;
18 |
19 | import org.apache.poi.hssf.usermodel.HSSFWorkbook;
20 | import org.apache.poi.ss.usermodel.CellType;
21 | import org.apache.poi.ss.usermodel.*;
22 |
23 | import java.util.Date;
24 |
25 | import static org.apache.poi.ss.usermodel.CellType.*;
26 |
27 | class StubCell {
28 |
29 | static Cell createBlankCell(int row, int column) {
30 | return create(row, column, BLANK);
31 | }
32 |
33 | static Cell createCell(int row, int column, Boolean aBoolean) {
34 | Cell cell = create(row, column, BOOLEAN);
35 | cell.setCellValue(aBoolean);
36 | return cell;
37 | }
38 |
39 | static Cell createCell(Boolean aBoolean) {
40 | return createCell(0, 0, aBoolean);
41 | }
42 |
43 | static Cell createCell(int row, int column, Byte value) {
44 | Cell cell = create(row, column, ERROR);
45 | cell.setCellErrorValue(value);
46 | return cell;
47 | }
48 |
49 | static Cell createFormulaCell(int row, int column, String formula) {
50 | Cell cell = create(row, column, FORMULA);
51 | cell.setCellFormula(formula);
52 | return cell;
53 | }
54 |
55 | static Cell createCell(int row, int column, Double aDouble) {
56 | Cell cell = create(row, column, NUMERIC);
57 | cell.setCellValue(aDouble);
58 | return cell;
59 | }
60 |
61 | static Cell createCell(Double aDouble) {
62 | return createCell(0, 0, aDouble);
63 | }
64 |
65 | static Cell createCell(int row, int column, Date date) {
66 | Workbook workbook = new HSSFWorkbook();
67 | Sheet sheet = workbook.createSheet();
68 | Cell cell = sheet.createRow(row).createCell(column, NUMERIC);
69 | cell.setCellValue(date);
70 | CellStyle style = workbook.createCellStyle();
71 | style.setDataFormat(workbook.getCreationHelper().createDataFormat().getFormat("m/d/yy h:mm"));
72 | cell.setCellStyle(style);
73 | return cell;
74 | }
75 |
76 | static Cell createCell(int row, int column, String value) {
77 | Cell cell = create(row, column, STRING);
78 | cell.setCellValue(value);
79 | return cell;
80 | }
81 |
82 | static Cell createCell(String value) {
83 | return createCell(0, 0, value);
84 | }
85 |
86 | private static Cell create(int row, int column, CellType type) {
87 | Workbook workbook = new HSSFWorkbook();
88 | Sheet sheet = workbook.createSheet();
89 | Cell cell = sheet.createRow(row).createCell(column, type);
90 | return cell;
91 | }
92 |
93 | }
94 |
--------------------------------------------------------------------------------
/src/test/java/bad/robot/excel/sheet/ExcelCoordinateTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.sheet;
18 |
19 | import org.junit.Test;
20 |
21 | import static org.hamcrest.Matchers.is;
22 | import static org.junit.Assert.assertThat;
23 |
24 | public class ExcelCoordinateTest {
25 |
26 | @Test
27 | public void convertRowCoordinateToExcelRowForBasicAlphabet() {
28 | assertThat(asColumn(0), is("A"));
29 | assertThat(asColumn(1), is("B"));
30 | assertThat(asColumn(2), is("C"));
31 | assertThat(asColumn(3), is("D"));
32 | assertThat(asColumn(4), is("E"));
33 | assertThat(asColumn(5), is("F"));
34 | assertThat(asColumn(6), is("G"));
35 | assertThat(asColumn(7), is("H"));
36 | assertThat(asColumn(8), is("I"));
37 | assertThat(asColumn(9), is("J"));
38 | assertThat(asColumn(10), is("K"));
39 | assertThat(asColumn(11), is("L"));
40 | assertThat(asColumn(12), is("M"));
41 | assertThat(asColumn(13), is("N"));
42 | assertThat(asColumn(14), is("O"));
43 | assertThat(asColumn(15), is("P"));
44 | assertThat(asColumn(16), is("Q"));
45 | assertThat(asColumn(17), is("R"));
46 | assertThat(asColumn(18), is("S"));
47 | assertThat(asColumn(19), is("T"));
48 | assertThat(asColumn(20), is("U"));
49 | assertThat(asColumn(21), is("V"));
50 | assertThat(asColumn(22), is("W"));
51 | assertThat(asColumn(23), is("X"));
52 | assertThat(asColumn(24), is("Y"));
53 | assertThat(asColumn(25), is("Z"));
54 | }
55 |
56 | @Test
57 | public void convertRowCoordinateToExcelRowForExtendedAlphabet() {
58 | assertThat(asColumn(0), is("A"));
59 | assertThat(asColumn(25), is("Z"));
60 | assertThat(asColumn(26), is("AA"));
61 | assertThat(asColumn(701), is("ZZ"));
62 | assertThat(asColumn(702), is("AAA"));
63 | assertThat(asColumn(703), is("AAB"));
64 | assertThat(asColumn(18277), is("ZZZ"));
65 | assertThat(asColumn(18278), is("AAAA"));
66 | }
67 |
68 | public String asColumn(int column) {
69 | StringBuilder converted = new StringBuilder();
70 | while (column >= 0) {
71 | int remainder = column % 26;
72 | converted.insert(0, (char) (remainder + 'A'));
73 | column = (column / 26) - 1;
74 | }
75 | return converted.toString();
76 | }
77 |
78 | }
79 |
--------------------------------------------------------------------------------
/src/test/java/bad/robot/excel/style/ColourTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.style;
18 |
19 | import bad.robot.excel.OutputWorkbook;
20 | import bad.robot.excel.cell.Cell;
21 | import bad.robot.excel.cell.StringCell;
22 | import bad.robot.excel.workbook.PoiWorkbook;
23 | import org.apache.poi.ss.usermodel.Workbook;
24 | import org.junit.Test;
25 |
26 | import java.io.IOException;
27 |
28 | import static bad.robot.excel.WorkbookResource.getWorkbook;
29 | import static bad.robot.excel.column.ExcelColumnIndex.A;
30 | import static bad.robot.excel.sheet.Coordinate.coordinate;
31 | import static bad.robot.excel.style.Colour.*;
32 | import static bad.robot.excel.style.Fill.fill;
33 | import static bad.robot.excel.style.FontColour.fontColour;
34 | import static bad.robot.excel.style.FontSize.fontSize;
35 | import static bad.robot.excel.style.ForegroundColour.foregroundColour;
36 | import static bad.robot.excel.style.StyleBuilder.aStyle;
37 |
38 | public class ColourTest {
39 |
40 | @Test
41 | public void exampleUsage() throws IOException {
42 | StyleBuilder blue = aStyle().with(fill(foregroundColour(Blue))).with(fontSize("18")).with(fontColour(White));
43 | StyleBuilder yellow = aStyle().with(fill(foregroundColour(Yellow))).with(fontColour(White));
44 | Cell a1 = new StringCell("Blue", blue);
45 | Cell a2 = new StringCell("Yellow", yellow);
46 | Workbook workbook = getWorkbook("emptySheet.xlsx");
47 | PoiWorkbook sheet = new PoiWorkbook(workbook);
48 | sheet.replaceCell(coordinate(A, 1), a1);
49 | sheet.replaceCell(coordinate(A, 2), a2);
50 | OutputWorkbook.writeWorkbookToTemporaryFile(workbook, "exampleOfApplyColours");
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/test/java/bad/robot/excel/style/StyleBuilderTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2013, bad robot (london) ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package bad.robot.excel.style;
18 |
19 | import bad.robot.excel.cell.Cell;
20 | import bad.robot.excel.cell.DataFormat;
21 | import bad.robot.excel.cell.DoubleCell;
22 | import bad.robot.excel.column.ColumnIndex;
23 | import bad.robot.excel.row.Row;
24 | import bad.robot.excel.workbook.PoiWorkbook;
25 | import org.junit.Test;
26 |
27 | import java.util.HashMap;
28 |
29 | import static bad.robot.excel.WorkbookResource.getWorkbook;
30 | import static bad.robot.excel.cell.DataFormat.asTwoDecimalPlacesNumber;
31 | import static bad.robot.excel.column.ColumnIndex.column;
32 | import static bad.robot.excel.column.ExcelColumnIndex.A;
33 | import static bad.robot.excel.matchers.WorkbookMatcher.sameWorkbook;
34 | import static bad.robot.excel.style.Border.border;
35 | import static bad.robot.excel.style.BorderStyle.None;
36 | import static bad.robot.excel.style.BorderStyle.ThinSolid;
37 | import static bad.robot.excel.style.BottomBorder.bottom;
38 | import static bad.robot.excel.style.LeftBorder.left;
39 | import static bad.robot.excel.style.RightBorder.right;
40 | import static bad.robot.excel.style.StyleBuilder.aStyle;
41 | import static bad.robot.excel.style.TopBorder.top;
42 | import static org.hamcrest.MatcherAssert.assertThat;
43 |
44 | public class StyleBuilderTest {
45 |
46 | private final Border border = border(top(None), right(None), bottom(ThinSolid), left(ThinSolid));
47 | private final DataFormat numberFormat = asTwoDecimalPlacesNumber();
48 |
49 | @Test
50 | public void exampleOfCreatingARow() throws Exception {
51 | Cell cell = new DoubleCell(9999.99d, aStyle().with(border).with(numberFormat));
52 |
53 | HashMap columns = new HashMap();
54 | columns.put(column(A), cell);
55 |
56 | Row row = new Row(columns);
57 |
58 | PoiWorkbook editable = new PoiWorkbook(getWorkbook("emptySheet.xls")).appendRowToFirstSheet(row);
59 |
60 | assertThat(editable.workbook(), sameWorkbook(getWorkbook("sheetWithSingleCell.xls")));
61 | }
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/cellTypes.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/cellTypes.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/emptySheet.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/emptySheet.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/emptySheet.xlsx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/emptySheet.xlsx
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/replaceCellWithSameRowExpected.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/replaceCellWithSameRowExpected.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/rowWithThreeCells.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/rowWithThreeCells.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/rowWithThreeCellsAlternativeValues.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/rowWithThreeCellsAlternativeValues.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/rowWithThreeCellsButOnlyTwoPhysicalCells.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/rowWithThreeCellsButOnlyTwoPhysicalCells.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/rowWithThreePhysicalCells.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/rowWithThreePhysicalCells.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/rowWithTwoCells.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/rowWithTwoCells.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/rowWithVariousCells.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/rowWithVariousCells.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/sheetWithSingleCell.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/sheetWithSingleCell.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/sheetWithThreeRows.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/sheetWithThreeRows.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/sheetWithThreeRowsWithAlternativeValues.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/sheetWithThreeRowsWithAlternativeValues.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/sheetWithTwoRows.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/sheetWithTwoRows.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/shouldAppendRowTemplate.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/shouldAppendRowTemplate.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/shouldAppendRowTemplateExpected.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/shouldAppendRowTemplateExpected.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/shouldReplaceCellTemplate.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/shouldReplaceCellTemplate.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/shouldReplaceCellTemplateExpected.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/shouldReplaceCellTemplateExpected.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/shouldReplaceCellsInComplicatedExampleTemplate.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/shouldReplaceCellsInComplicatedExampleTemplate.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/shouldReplaceCellsInComplicatedExampleTemplateExpected.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/shouldReplaceCellsInComplicatedExampleTemplateExpected.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/shouldReplaceDateCellTemplate.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/shouldReplaceDateCellTemplate.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/shouldReplaceDateCellTemplateExpected.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/shouldReplaceDateCellTemplateExpected.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/workbookTemplate.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/workbookTemplate.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/workbookWithMultipleAlternativelyNamedSheets.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/workbookWithMultipleAlternativelyNamedSheets.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/workbookWithMultipleNamedSheets.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/workbookWithMultipleNamedSheets.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/workbookWithOneSheet.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/workbookWithOneSheet.xls
--------------------------------------------------------------------------------
/src/test/resource/bad/robot/excel/workbookWithTwoSheets.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tobyweston/simple-excel/8971367f390bc99631f41386e6c44763d709deb3/src/test/resource/bad/robot/excel/workbookWithTwoSheets.xls
--------------------------------------------------------------------------------
| | |