Skip to content

Commit

Permalink
🔖 release 0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Feb 6, 2018
1 parent 0c1013b commit 025a9af
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 57 deletions.
74 changes: 74 additions & 0 deletions src/main/java/io/github/biezhi/excel/plus/Constant.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.github.biezhi.excel.plus;

import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;

/**
* Excel plus constant
*
Expand All @@ -20,4 +23,75 @@ public interface Constant {
*/
int DEFAULT_ORDER = -1;

default CellStyle defaultTitleStyle(Workbook workbook) {
CellStyle style = workbook.createCellStyle();

style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);

style.setBorderTop(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);

style.setFillForegroundColor(HSSFColor.HSSFColorPredefined.WHITE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

Font font = workbook.createFont();
font.setFontHeightInPoints((short) 18);
font.setBold(true);
font.setFontName("Arial");
style.setFont(font);
return style;
}

/**
* The default Excel header style.
*
* @param workbook Excel workbook
* @return header row cell style
*/
default CellStyle defaultHeaderStyle(Workbook workbook) {
CellStyle headerStyle = workbook.createCellStyle();

headerStyle.setAlignment(HorizontalAlignment.CENTER);
headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
headerStyle.setBorderTop(BorderStyle.THIN);
headerStyle.setBorderRight(BorderStyle.THIN);
headerStyle.setBorderBottom(BorderStyle.THIN);
headerStyle.setBorderLeft(BorderStyle.THIN);

headerStyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.WHITE.getIndex());
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

Font font = workbook.createFont();
font.setFontHeightInPoints((short) 12);
font.setBold(true);
font.setFontName("Arial");
headerStyle.setFont(font);
return headerStyle;
}

/**
* The default Excel column style.
*
* @param workbook Excel workbook
* @return row column cell style
*/
default CellStyle defaultColumnStyle(Workbook workbook) {
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setWrapText(true);

Font font = workbook.createFont();
font.setFontName("Arial");
cellStyle.setFont(font);
return cellStyle;
}

}
98 changes: 42 additions & 56 deletions src/main/java/io/github/biezhi/excel/plus/writer/ExcelWriter.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package io.github.biezhi.excel.plus.writer;

import io.github.biezhi.excel.plus.Constant;
import io.github.biezhi.excel.plus.enums.ExcelType;
import io.github.biezhi.excel.plus.exception.ExcelException;
import io.github.biezhi.excel.plus.utils.ExcelUtils;
import io.github.biezhi.excel.plus.utils.Pair;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -22,7 +24,7 @@
* @author biezhi
* @date 2018/2/4
*/
public interface ExcelWriter {
public interface ExcelWriter extends Constant {

/**
* Default Export method
Expand All @@ -43,38 +45,56 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
Workbook workbook;
CellStyle headerStyle;
CellStyle columnStyle = null;
CellStyle titleStyle = null;

T data0 = data.iterator().next();
// Set Excel header
Iterator<T> iterator = data.iterator();

List<Pair<Integer, String>> writeFieldNames = ExcelUtils.getWriteFieldNames(data0.getClass());

List<Integer> columnIndexes = writeFieldNames.stream().map(Pair::getK).collect(Collectors.toList());

int startRow = exporter.startRow();

if (null != exporter.getTemplatePath()) {
InputStream in = ExcelWriter.class.getClassLoader().getResourceAsStream(exporter.getTemplatePath());
workbook = WorkbookFactory.create(in);
sheet = workbook.getSheetAt(0);

} else {
workbook = exporter.getExcelType().equals(ExcelType.XLSX) ? new XSSFWorkbook() : new HSSFWorkbook();
sheet = workbook.createSheet(ExcelUtils.getSheetName(data0));

if (null != exporter.getTitleStyle()) {
titleStyle = exporter.getTitleStyle().apply(workbook);
} else {
titleStyle = this.defaultTitleStyle(workbook);
}

if (null != exporter.getHeaderStyle()) {
headerStyle = exporter.getHeaderStyle().apply(workbook);
} else {
headerStyle = defaultHeaderStyle(workbook);
headerStyle = this.defaultHeaderStyle(workbook);
}

if (null != exporter.getColumnStyle()) {
columnStyle = exporter.getColumnStyle().apply(workbook);
} else {
columnStyle = defaultColumnStyle(workbook);
columnStyle = this.defaultColumnStyle(workbook);
}

this.writeRowHead(headerStyle, sheet, writeFieldNames);
}
String headerTitle = exporter.getHeaderTitle();
int colIndex = 0;
if (null != headerTitle) {
colIndex = 1;
int maxColIndex = columnIndexes.stream().max(Comparator.comparingInt(Integer::intValue)).get();
this.writeTitleRow(titleStyle, sheet, headerTitle, maxColIndex);
}
this.writeColumnNames(colIndex, headerStyle, sheet, writeFieldNames);
startRow += colIndex;

List<Integer> columnIndexes = writeFieldNames.stream().map(Pair::getK).collect(Collectors.toList());
}

this.writeRows(sheet, columnStyle, null, iterator, startRow, columnIndexes);

Expand All @@ -86,15 +106,28 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
}
}

default void writeTitleRow(CellStyle cellStyle, Sheet sheet, String title, int maxColIndex) {
Row titleRow = sheet.createRow(0);
for (int i = 0; i <= maxColIndex; i++) {
Cell cell = titleRow.createCell(i);
if (i == 0) {
cell.setCellValue(title);
}
cell.setCellStyle(cellStyle);
}
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, maxColIndex));
}

/**
* Write the header row to Sheet.
*
* @param rowIndex start row index
* @param headerStyle header row cell style
* @param sheet work sheet
* @param columnNames column names
*/
default void writeRowHead(CellStyle headerStyle, Sheet sheet, List<Pair<Integer, String>> columnNames) {
Row rowHead = sheet.createRow(0);
default void writeColumnNames(int rowIndex, CellStyle headerStyle, Sheet sheet, List<Pair<Integer, String>> columnNames) {
Row rowHead = sheet.createRow(rowIndex);
columnNames.forEach(pair -> {
Integer colIndex = pair.getK();
String columnName = pair.getV();
Expand Down Expand Up @@ -137,53 +170,6 @@ default <T> void writeRows(Sheet sheet, CellStyle columnStyle, CellStyle rowStyl
}
}

/**
* The default Excel header style.
*
* @param workbook Excel workbook
* @return header row cell style
*/
default CellStyle defaultHeaderStyle(Workbook workbook) {
CellStyle headerStyle = workbook.createCellStyle();

headerStyle.setAlignment(HorizontalAlignment.CENTER);
headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
headerStyle.setBorderTop(BorderStyle.THIN);
headerStyle.setBorderRight(BorderStyle.THIN);
headerStyle.setBorderBottom(BorderStyle.THIN);
headerStyle.setBorderLeft(BorderStyle.THIN);

headerStyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.WHITE.getIndex());
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

Font headerFont = workbook.createFont();
headerFont.setFontHeightInPoints((short) 12);
headerFont.setBold(true);
headerStyle.setFont(headerFont);
return headerStyle;
}

/**
* The default Excel column style.
*
* @param workbook Excel workbook
* @return row column cell style
*/
default CellStyle defaultColumnStyle(Workbook workbook) {
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setWrapText(true);

Font cellFont = workbook.createFont();
cellStyle.setFont(cellFont);
return cellStyle;
}

/**
* Export excel
*
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/io/github/biezhi/excel/plus/writer/Exporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
*/
public class Exporter<T> {

private String headerTitle;
private String templatePath;
private ExcelType excelType;
private Collection<T> data;
private int startRow = 1;

private Function<Workbook, CellStyle> titleStyle;
private Function<Workbook, CellStyle> headerStyle;
private Function<Workbook, CellStyle> columnStyle;

Expand All @@ -29,6 +31,16 @@ public static <T> Exporter<T> create(Collection<T> data) {
return exporter;
}

public Exporter<T> title(String title) {
this.headerTitle = title;
return this;
}

public Exporter<T> titleStyle(Function<Workbook, CellStyle> function) {
this.titleStyle = function;
return this;
}

public Exporter<T> headerStyle(Function<Workbook, CellStyle> function) {
this.headerStyle = function;
return this;
Expand All @@ -49,10 +61,14 @@ public Exporter<T> startRow(int startRow) {
return this;
}

public int startRow(){
public int startRow() {
return this.startRow;
}

public Function<Workbook, CellStyle> getTitleStyle() {
return titleStyle;
}

public Function<Workbook, CellStyle> getHeaderStyle() {
return headerStyle;
}
Expand All @@ -69,6 +85,10 @@ public Collection<T> getData() {
return data;
}

public String getHeaderTitle() {
return headerTitle;
}

public void setExcelType(ExcelType excelType) {
this.excelType = excelType;
}
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/io/github/biezhi/excel/plus/Examples.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public void testExport() throws ExcelException {
excelPlus.export(cardSecrets).writeAsFile(new File("卡密列表.xls"));
}

@Test
public void testExportTitle() throws ExcelException {
List<CardSecret> cardSecrets = this.buildCardSecrets();
excelPlus.export(Exporter.create(cardSecrets).title("卡密列表第一季数据")).writeAsFile(new File("卡密列表.xls"));
}

private List<CardSecret> buildCardSecrets() {
List<CardSecret> cardSecrets = new ArrayList<>();
cardSecrets.add(new CardSecret(1, "vlfdzepjmlz2y43z7er4", new BigDecimal("20"), true));
Expand Down

0 comments on commit 025a9af

Please sign in to comment.