
br.com.objectos.way.io.POIWorkbook Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2014 Objectos, Fábrica de Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package br.com.objectos.way.io;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import br.com.objectos.xls.Spreadsheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
/**
* @author [email protected] (Marcio Endo)
*/
class POIWorkbook {
private final ConcurrentMap headerStyleMap = new ConcurrentHashMap();
private final ConcurrentMap bodyStyleMap = new ConcurrentHashMap();
private final Workbook wb;
public POIWorkbook() {
wb = new HSSFWorkbook();
}
public POISheet createSheet() {
Sheet sheet = wb.createSheet();
return new POISheet(this, sheet);
}
public POISheet createSheet(String name) {
Sheet sheet = wb.createSheet(name);
return new POISheet(this, sheet);
}
public Spreadsheet createSpreadsheet() {
return Spreadsheet.newSpreadsheet(wb);
}
public void write(OutputStream stream) throws IOException {
wb.write(stream);
}
public CellStyle createHeaderStyle(TableStyle tableStyle) {
return createStyle(headerStyleMap, tableStyle, null);
}
public CellStyle createBodyStyle(TableStyle tableStyle, TableFormat tableFormat) {
return createStyle(bodyStyleMap, tableStyle, tableFormat);
}
Workbook getWb() {
return wb;
}
private CellStyle createStyle(
ConcurrentMap map, TableStyle tableStyle, TableFormat tableFormat) {
StyleKey styleKey = new StyleKey(tableStyle, tableFormat);
CellStyle style = map.get(styleKey);
if (style == null) {
synchronized (wb) {
CellStyle newStyle = tableStyle.createStyle(wb);
if (tableFormat != null) {
tableFormat.apachePOI(wb, newStyle);
}
style = map.putIfAbsent(styleKey, newStyle);
if (style == null) {
style = newStyle;
}
}
}
return style;
}
private static class StyleKey {
final TableStyle style;
final TableFormat format;
public StyleKey(TableStyle style, TableFormat format) {
this.style = style;
this.format = format;
}
@Override
public final int hashCode() {
return Objects.hash(style, format);
}
@Override
public final boolean equals(final Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof StyleKey) {
final StyleKey that = (StyleKey) obj;
return Objects.equals(style, that.style)
&& Objects.equals(format, that.format);
} else {
return false;
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy