Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Enable to set style to cells.

  • Loading branch information...
commit 1ec73a39a7824834afbec9b987b17466d55734ac 1 parent 5b3b124
@kawasima authored
View
2  src/main/java/net/unit8/axebomber/manager/impl/FileSystemBookManager.java
@@ -34,6 +34,7 @@ public Book create(String path) {
newbook = new Book(new HSSFWorkbook());
newbook.setPath(path + ".xls");
}
+ newbook.setEditable(true);
return newbook;
}
@@ -45,6 +46,7 @@ public Book open(String path) {
Workbook workbook = WorkbookFactory.create(fis);
Book book = new Book(workbook);
book.setPath(path);
+ book.setEditable(true);
return book;
} catch(IOException e) {
throw new BookIOException("", e);
View
17 src/main/java/net/unit8/axebomber/parser/Book.java
@@ -30,6 +30,7 @@
public class Book {
private Workbook workbook;
private String path;
+ private boolean editable = false;
public Book(Workbook workbook) {
this.workbook = workbook;
@@ -44,9 +45,13 @@ public Book(Workbook workbook) {
public Sheet getSheet(String name) {
org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheet(name);
if(sheet == null) {
- sheet = workbook.createSheet(name);
+ if (editable) {
+ sheet = workbook.createSheet(name);
+ } else {
+ throw new SheetNotFoundException(name);
+ }
}
- return new Sheet(sheet);
+ return new Sheet(sheet, editable);
}
/**
@@ -89,4 +94,12 @@ public String getPath() {
public void setPath(String path) {
this.path = path;
}
+
+ public boolean isEditable() {
+ return editable;
+ }
+
+ public void setEditable(boolean editable) {
+ this.editable = editable;
+ }
}
View
106 src/main/java/net/unit8/axebomber/parser/Range.java
@@ -0,0 +1,106 @@
+package net.unit8.axebomber.parser;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.Row;
+
+public class Range {
+ private static final Pattern R1C1 = Pattern.compile("(R(\\d+)?)?(C(\\d+)?)?");
+
+ private int x1, x2, y1, y2;
+ private org.apache.poi.ss.usermodel.Sheet sheet;
+
+ protected Range() {}
+
+ public Range(Cell beginCell, Cell endCell) {
+
+ }
+
+ public static Range parse(String rangeStr) {
+ String[] ranges = StringUtils.split(rangeStr, ":", 2);
+ Range range = new Range();
+ if (ranges.length == 1) {
+ Matcher m = R1C1.matcher(ranges[0]);
+ if (m.find()) {
+ range.y1 = parseIndex(m, 1, -1);
+ range.y2 = parseIndex(m, 1, Integer.MAX_VALUE);
+ range.x1 = parseIndex(m, 3, -1);
+ range.x2 = parseIndex(m, 3, Integer.MAX_VALUE);
+ } else
+ throw new IllegalArgumentException(rangeStr);
+ } else if (ranges.length == 2) {
+ Matcher m = R1C1.matcher(ranges[0]);
+ if (m.find()) {
+ range.y1 = parseIndex(m, 1, -1);
+ range.x1 = parseIndex(m, 3, -1);
+ }
+ m = R1C1.matcher(ranges[1]);
+ if (m.find()) {
+ range.y2 = parseIndex(m, 1, Integer.MAX_VALUE);
+ range.x2 = parseIndex(m, 3, Integer.MAX_VALUE);
+ }
+ } else {
+ throw new IllegalArgumentException(rangeStr);
+ }
+
+ if (range.y1 > range.y2) {
+ int tmp = range.y2;
+ range.y2 = range.y1;
+ range.y1 = tmp;
+ }
+ if (range.x1 > range.x2) {
+ int tmp = range.x2;
+ range.x2 = range.x1;
+ range.x1 = tmp;
+ }
+
+ return range;
+ }
+
+ private static int parseIndex(Matcher m, int i, int defaultValue) {
+ if (StringUtils.isEmpty(m.group(i))) {
+ return defaultValue;
+ } else {
+ return StringUtils.isEmpty(m.group(i+1)) ? 0 : Integer.parseInt(m.group(i+1));
+ }
+ }
+
+
+ public void setStyle(Style style) {
+ int top = Math.max(y1, sheet.getFirstRowNum());
+ int bottom = Math.min(y2, sheet.getLastRowNum());
+ for (int i = top; i <= bottom; i++) {
+ Row row = sheet.getRow(i);
+ int left = Math.max(x1, row.getFirstCellNum());
+ int right = Math.min(x2, row.getLastCellNum());
+
+ for(int j = left; j <= right; j++) {
+ int borderBits = 0;
+ if (i == top)
+ borderBits |= StyleManager.BORDER_TOP;
+ else if (i == bottom)
+ borderBits |= StyleManager.BORDER_BOTTOM;
+
+ if (j == left)
+ borderBits |= StyleManager.BORDER_LEFT;
+ else if (j == right)
+ borderBits |= StyleManager.BORDER_RIGHT;
+
+ org.apache.poi.ss.usermodel.Cell cell = row.getCell(j, Row.CREATE_NULL_AS_BLANK);
+ CellStyle cellStyle = StyleManager.getInstance(sheet.getWorkbook()).getStyle(style, borderBits);
+ cell.setCellStyle(cellStyle);
+ }
+ }
+ }
+ @Override
+ public String toString() {
+ return String.format("(%d,%d) to (%d,%d)", x1, y1, x2, y2);
+ }
+
+ public void setSheet(org.apache.poi.ss.usermodel.Sheet sheet) {
+ this.sheet = sheet;
+ }
+}
View
17 src/main/java/net/unit8/axebomber/parser/Row.java
@@ -1,12 +1,12 @@
/*******************************************************************************
* Copyright 2011 kawasima
- *
+ *
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -21,6 +21,7 @@
public class Row {
org.apache.poi.ss.usermodel.Row row;
private Map<String, Integer> labelColumns = null;
+ private boolean editable = false;
public Row(org.apache.poi.ss.usermodel.Row row, TableHeader tableHeader) {
this.row = row;
@@ -29,7 +30,7 @@ public Row(org.apache.poi.ss.usermodel.Row row, TableHeader tableHeader) {
}
public Cell cell(Integer columnIndex) {
- return cell(columnIndex, false);
+ return cell(columnIndex, editable);
}
public Cell cell(Integer columnIndex, boolean create) {
@@ -87,4 +88,12 @@ public Integer getIndex() {
public org.apache.poi.ss.usermodel.Row getSubstance() {
return row;
}
+
+ public boolean isEditable() {
+ return editable;
+ }
+
+ public void setEditable(boolean editable) {
+ this.editable = editable;
+ }
}
View
8 src/main/java/net/unit8/axebomber/parser/RowNotFoundException.java
@@ -0,0 +1,8 @@
+package net.unit8.axebomber.parser;
+
+@SuppressWarnings("serial")
+public class RowNotFoundException extends RuntimeException {
+ public RowNotFoundException(String msg) {
+ super(msg);
+ }
+}
View
40 src/main/java/net/unit8/axebomber/parser/Sheet.java
@@ -26,13 +26,18 @@
private org.apache.poi.ss.usermodel.Sheet sheet;
private Integer rowIndex;
private TableHeader tableHeader;
- private boolean isReadOnly = false;
+ private boolean editable = false;
public Sheet(org.apache.poi.ss.usermodel.Sheet sheet) {
+ this(sheet, false);
+ }
+
+ public Sheet(org.apache.poi.ss.usermodel.Sheet sheet, boolean editable) {
rowIndex = 0;
if(sheet == null)
throw new RuntimeException("sheet not found");
this.sheet = sheet;
+ this.editable = editable;
}
public String getName() {
@@ -103,10 +108,18 @@ public boolean stop(Row row) {
}
public Row getRow(int rowIndex) {
- org.apache.poi.ss.usermodel.Row row = sheet.getRow(rowIndex);
- if(row == null)
- row = this.sheet.createRow(rowIndex);
- return new Row(row, tableHeader);
+ org.apache.poi.ss.usermodel.Row nativeRow = sheet.getRow(rowIndex);
+ if(nativeRow == null) {
+ if (editable) {
+ nativeRow = this.sheet.createRow(rowIndex);
+ } else {
+ throw new RowNotFoundException("index " + rowIndex);
+ }
+ }
+ Row row = new Row(nativeRow, tableHeader);
+ if (editable)
+ row.setEditable(editable);
+ return row;
}
public Cell findCell(Pattern p) {
@@ -175,17 +188,24 @@ public Cell cell(int columnIndex) {
public void nextRow() {
rowIndex++;
- if(!isReadOnly) {
+ if(editable) {
if(sheet.getRow(rowIndex) == null) {
sheet.createRow(rowIndex);
}
}
}
- /**
- *
- */
- public void autoStyling() {
+ public Range createRange(String rangeStr) {
+ Range range = Range.parse(rangeStr);
+ range.setSheet(sheet);
+ return range;
+ }
+
+ public boolean isEditable() {
+ return editable;
+ }
+ public void setEditable(boolean editable) {
+ this.editable = editable;
}
}
View
10 src/main/java/net/unit8/axebomber/parser/SheetNotFoundException.java
@@ -0,0 +1,10 @@
+package net.unit8.axebomber.parser;
+
+@SuppressWarnings("serial")
+public class SheetNotFoundException extends RuntimeException {
+
+ public SheetNotFoundException(String name) {
+ super(name);
+ }
+
+}
View
62 src/main/java/net/unit8/axebomber/parser/Style.java
@@ -0,0 +1,62 @@
+package net.unit8.axebomber.parser;
+
+import org.apache.poi.ss.usermodel.BorderStyle;
+import org.apache.poi.ss.usermodel.IndexedColors;
+
+public class Style {
+ private IndexedColors color;
+ private IndexedColors backgroundColor;
+ private short borderStyle;
+ private IndexedColors borderColor;
+
+ @Override
+ public boolean equals(Object obj) {
+ if (!(obj instanceof Style))
+ return false;
+ Style style = (Style)obj;
+ return style.getColor() == this.color
+ && style.getBackgroundColor() == this.backgroundColor
+ && style.getBorderStyle() == this.borderStyle
+ && style.getBorderColor() == this.borderColor;
+ }
+
+ public IndexedColors getColor() {
+ return color;
+ }
+
+ public void setColor(IndexedColors color) {
+ this.color = color;
+ }
+
+ public IndexedColors getBackgroundColor() {
+ return backgroundColor;
+ }
+
+ public void setBackgroundColor(IndexedColors backgroundColor) {
+ this.backgroundColor = backgroundColor;
+ }
+
+ public void setBackgroundColor(String name) {
+ this.backgroundColor = IndexedColors.valueOf(name);
+ }
+
+ public short getBorderStyle() {
+ return borderStyle;
+ }
+
+ public void setBorderStyle(short borderStyle) {
+ this.borderStyle = borderStyle;
+ }
+ public void setBorderStyle(String name) {
+ BorderStyle borderStyle = BorderStyle.valueOf(name);
+ this.borderStyle = (short) borderStyle.ordinal();
+ }
+
+ public IndexedColors getBorderColor() {
+ return borderColor;
+ }
+
+ public void setBorderColor(IndexedColors borderColor) {
+ this.borderColor = borderColor;
+ }
+}
View
62 src/main/java/net/unit8/axebomber/parser/StyleManager.java
@@ -0,0 +1,62 @@
+package net.unit8.axebomber.parser;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.Workbook;
+
+public class StyleManager {
+ public static final int BORDER_TOP = 1;
+ public static final int BORDER_BOTTOM = 2;
+ public static final int BORDER_LEFT = 4;
+ public static final int BORDER_RIGHT = 8;
+
+ private static volatile Map<Workbook, StyleManager> cache = new HashMap<Workbook, StyleManager>();
+ private Workbook workbook;
+ private volatile Map<Style, List<CellStyle>> styles = new HashMap<Style, List<CellStyle>>();
+
+ protected StyleManager(Workbook workbook) {
+ this.workbook = workbook;
+ }
+
+ public static StyleManager getInstance(Workbook book) {
+ if (!cache.containsKey(book)) {
+ synchronized(StyleManager.class) {
+ if (!cache.containsKey(book)) {
+ cache.put(book, new StyleManager(book));
+ }
+ }
+ }
+ return cache.get(book);
+
+ }
+
+ public CellStyle getStyle(Style style, int borderBits) {
+ if (!styles.containsKey(style)) {
+ List<CellStyle> cellStyles = new ArrayList<CellStyle>();
+ for (int i=0; i <= (BORDER_TOP|BORDER_BOTTOM|BORDER_LEFT|BORDER_RIGHT); i++) {
+ CellStyle cellStyle = workbook.createCellStyle();
+ cellStyle.setFillForegroundColor(style.getBackgroundColor().getIndex());
+ cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
+ if ((i & BORDER_TOP) != 0) {
+ cellStyle.setBorderTop(style.getBorderStyle());
+ }
+ if ((i & BORDER_BOTTOM) != 0) {
+ cellStyle.setBorderBottom(style.getBorderStyle());
+ }
+ if ((i & BORDER_LEFT) != 0) {
+ cellStyle.setBorderLeft(style.getBorderStyle());
+ }
+ if ((i & BORDER_RIGHT) != 0) {
+ cellStyle.setBorderRight(style.getBorderStyle());
+ }
+ cellStyles.add(cellStyle);
+ }
+ styles.put(style, cellStyles);
+ }
+ return styles.get(style).get(borderBits);
+ }
+}
View
25 src/test/java/net/unit8/axebomber/parser/NewBookTest.java
@@ -0,0 +1,25 @@
+package net.unit8.axebomber.parser;
+
+import java.io.FileReader;
+import java.io.IOException;
+
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
+
+import org.apache.commons.io.IOUtils;
+import org.junit.Test;
+
+
+public class NewBookTest {
+ @Test
+ public void test() throws IOException, ScriptException {
+ ScriptEngine jruby = new ScriptEngineManager().getEngineByName("jruby");
+ FileReader reader = new FileReader("src/test/resources/new_book.rb");
+ try {
+ jruby.eval(reader);
+ } finally {
+ IOUtils.closeQuietly(reader);
+ }
+ }
+}
View
21 src/test/java/net/unit8/axebomber/parser/RangeTest.java
@@ -0,0 +1,21 @@
+package net.unit8.axebomber.parser;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class RangeTest extends Range{
+
+ @Test
+ public void test() {
+ Range range = Range.parse("R2C2:R3C3");
+ System.out.println(range);
+ range = Range.parse("R2C2");
+ System.out.println(range);
+ range = Range.parse("RC2");
+ System.out.println(range);
+ range = Range.parse("R2");
+ System.out.println(range);
+ }
+
+}
View
19 src/test/resources/new_book.rb
@@ -0,0 +1,19 @@
+require 'java'
+java_import "net.unit8.axebomber.manager.impl.FileSystemBookManager"
+java_import "net.unit8.axebomber.parser.Style"
+
+manager = FileSystemBookManager.new
+book = manager.create("target/new_book.xls");
+sheet = book.getSheet('test')
+
+row = sheet.getRow(0);
+row.cell(0).value = "No."
+row.cell(3).value = "title"
+
+range = sheet.createRange("R0")
+style = Style.new
+style.backgroundColor = "GOLD"
+style.borderStyle = "MEDIUM"
+range.style = style
+
+manager.save(book)
Please sign in to comment.
Something went wrong with that request. Please try again.