Skip to content

Commit

Permalink
Merge pull request #16 from vpday/master
Browse files Browse the repository at this point in the history
✨ to specify the style of a column by condition.
  • Loading branch information
hellokaton committed Dec 11, 2018
2 parents c065d13 + 5642187 commit 1727c18
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 20 deletions.
49 changes: 34 additions & 15 deletions src/main/java/io/github/biezhi/excel/plus/writer/ExcelWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,15 @@
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.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
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.math.BigDecimal;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -68,6 +59,8 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
CellStyle headerStyle;
CellStyle columnStyle = null;
CellStyle titleStyle;
List<CellStyle> specialColumnStyles = new ArrayList<>();
List<Predicate<String>> specialConditions = new ArrayList<>();

T data0 = data.iterator().next();
// Set Excel header
Expand Down Expand Up @@ -110,6 +103,11 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
columnStyle = this.defaultColumnStyle(workbook);
}

if (null != exporter.getSpecialColumn()) {
exporter.getSpecialColumn().values().forEach(var -> specialColumnStyles.add(var.apply(workbook)));
specialConditions = new ArrayList<>(exporter.getSpecialColumn().keySet());
}

String headerTitle = exporter.getHeaderTitle();
int colIndex = 0;
if (null != headerTitle) {
Expand All @@ -123,7 +121,7 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws

}

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

workbook.write(outputStream);
outputStream.flush();
Expand Down Expand Up @@ -171,24 +169,45 @@ default void writeColumnNames(int rowIndex, CellStyle headerStyle, Sheet sheet,
*
* @param sheet work sheet
* @param columnStyle each column style in the row.
* @param specialColumnStyles each special column style in the row.
* @param rowStyle row style
* @param specialConditions the judgment conditions for a special column
* @param iterator row data iterator
* @param startRow from the beginning of the line, the default is 1
* @param <T> Java Type
*/
default <T> void writeRows(Sheet sheet, CellStyle columnStyle, CellStyle rowStyle, Iterator<T> iterator, int startRow, List<Integer> columnIndexes) {
default <T> void writeRows(Sheet sheet, CellStyle columnStyle, List<CellStyle> specialColumnStyles, CellStyle rowStyle, List<Predicate<String>> specialConditions, Iterator<T> iterator, int startRow, List<Integer> columnIndexes) {
for (int rowNum = startRow; iterator.hasNext(); rowNum++) {
T item = iterator.next();
Row row = sheet.createRow(rowNum);
if (null != rowStyle) {
row.setRowStyle(rowStyle);
}

boolean isSpecialColumn = false;
int index = -1;
if (null != specialColumnStyles && null != specialConditions) {
here:
for (Integer col : columnIndexes) {
String value = ExcelUtils.getColumnValue(item, col);
for (int i = 0, length = specialConditions.size(); i < length; i++) {
index = i;
isSpecialColumn = specialConditions.get(i).test(value);
if (isSpecialColumn) {
break here;
}
}
}
}

Iterator<Integer> colIt = columnIndexes.iterator();
while (colIt.hasNext()) {
int col = colIt.next();
Cell cell = row.createCell(col);
String value = ExcelUtils.getColumnValue(item, col);
if (null != columnStyle) {
if (isSpecialColumn) {
cell.setCellStyle(specialColumnStyles.get(index));
} else if (null != columnStyle) {
cell.setCellStyle(columnStyle);
}
if (null != value) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/io/github/biezhi/excel/plus/writer/Exporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

/**
* Excel exporter
Expand All @@ -45,6 +48,7 @@ public class Exporter<T> {
private Function<Workbook, CellStyle> titleStyle;
private Function<Workbook, CellStyle> headerStyle;
private Function<Workbook, CellStyle> columnStyle;
private Map<Predicate<String>,Function<Workbook, CellStyle>> specialColumn = new ConcurrentHashMap<>(16);

public static <T> Exporter<T> create(Collection<T> data) {
Exporter<T> exporter = new Exporter<>();
Expand Down Expand Up @@ -77,6 +81,11 @@ public Exporter<T> columnStyle(Function<Workbook, CellStyle> function) {
return this;
}

public Exporter<T> specialColumn(Map<Predicate<String>,Function<Workbook, CellStyle>> specialColumn) {
this.specialColumn.putAll(specialColumn);
return this;
}

public Exporter<T> byTemplate(String templatePath) {
this.templatePath = templatePath;
return this;
Expand All @@ -103,6 +112,10 @@ public Function<Workbook, CellStyle> getColumnStyle() {
return columnStyle;
}

public Map<Predicate<String>, Function<Workbook, CellStyle>> getSpecialColumn() {
return specialColumn;
}

public String getTemplatePath() {
return templatePath;
}
Expand Down
56 changes: 51 additions & 5 deletions src/test/java/io/github/biezhi/excel/plus/ExcelPlusTest.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package io.github.biezhi.excel.plus;

import io.github.biezhi.excel.plus.enums.ExcelType;
import io.github.biezhi.excel.plus.enums.ParseType;
import io.github.biezhi.excel.plus.exception.ExcelException;
import io.github.biezhi.excel.plus.exception.ParseException;
import io.github.biezhi.excel.plus.model.CardSecret;
import io.github.biezhi.excel.plus.reader.Reader;
import io.github.biezhi.excel.plus.writer.Exporter;
import io.github.biezhi.excel.plus.writer.ResponseWrapper;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.junit.Assert;
import org.junit.Test;

Expand All @@ -21,7 +20,11 @@
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -144,6 +147,49 @@ public void testExportByTpl() throws ExcelException {
).writeAsFile(new File("template_rows.xls"));
}

@Test
public void testExportBySpecials() throws ExcelException {
List<CardSecret> cardSecrets = this.buildCardSecrets();

Map<Predicate<String>, Function<Workbook, CellStyle>> specialColumn = new ConcurrentHashMap<>(3);
specialColumn.put(var -> var.equals("vlfdzepjmlz2y43z7er4"), webHook -> {
XSSFCellStyle cellStyle = (XSSFCellStyle) webHook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.LEFT);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setWrapText(true);
XSSFColor xssfColor = new XSSFColor();
xssfColor.setRGB(new byte[]{(byte) 252, (byte) 228, (byte) 236});
cellStyle.setFillForegroundColor(xssfColor);
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Font font = webHook.createFont();
font.setFontName("Arial");
font.setFontHeightInPoints((short) 12);
cellStyle.setFont(font);
return cellStyle;
});
specialColumn.put(var -> var.equals("rasefq2rzotsmx526z6g"), webHook -> {
XSSFCellStyle cellStyle = (XSSFCellStyle) webHook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.LEFT);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setWrapText(true);
XSSFColor xssfColor = new XSSFColor();
xssfColor.setRGB(new byte[]{(byte) 255, (byte) 235, (byte) 238});
cellStyle.setFillForegroundColor(xssfColor);
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Font font = webHook.createFont();
font.setFontName("Arial");
font.setFontHeightInPoints((short) 13);
cellStyle.setFont(font);
return cellStyle;
});

Exporter<CardSecret> exporter = Exporter.create(cardSecrets)
.sheetName("卡密列表第一季数据").specialColumn(specialColumn);
exporter.setExcelType(ExcelType.XLSX);

excelPlus.export(exporter).writeAsFile(new File("export.xlsx"));
}

// @Test
public void testDownload() throws ExcelException {
List<CardSecret> cardSecrets = this.buildCardSecrets();
Expand Down

0 comments on commit 1727c18

Please sign in to comment.