|
@@ -98,22 +98,31 @@ public final class ExcelWriter implements Closeable {
|
|
public <T> boolean write(List<T> list, Class<T> clazz) {
|
|
public <T> boolean write(List<T> list, Class<T> clazz) {
|
|
|
|
|
|
ExcelMeta excelMeta = ExcelBeanHelper.getExcelMeta(clazz);
|
|
ExcelMeta excelMeta = ExcelBeanHelper.getExcelMeta(clazz);
|
|
|
|
+
|
|
try {
|
|
try {
|
|
|
|
+
|
|
Sheet sheet = workbook.createSheet(this.sheetName);
|
|
Sheet sheet = workbook.createSheet(this.sheetName);
|
|
sheet.setDefaultColumnWidth(15);
|
|
sheet.setDefaultColumnWidth(15);
|
|
|
|
+
|
|
CellStyle titleStyle = workbook.createCellStyle();
|
|
CellStyle titleStyle = workbook.createCellStyle();
|
|
|
|
+
|
|
titleStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
titleStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
titleStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
|
|
titleStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
|
|
titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
titleStyle.setBorderBottom(BorderStyle.THIN); //下边框
|
|
titleStyle.setBorderBottom(BorderStyle.THIN); //下边框
|
|
titleStyle.setBorderLeft(BorderStyle.THIN);
|
|
titleStyle.setBorderLeft(BorderStyle.THIN);
|
|
|
|
+
|
|
Font font = workbook.createFont();
|
|
Font font = workbook.createFont();
|
|
|
|
+
|
|
font.setFontName("宋体");
|
|
font.setFontName("宋体");
|
|
font.setFontHeightInPoints((short) 13);
|
|
font.setFontHeightInPoints((short) 13);
|
|
|
|
+
|
|
titleStyle.setFont(font);
|
|
titleStyle.setFont(font);
|
|
|
|
+
|
|
//列值类型
|
|
//列值类型
|
|
Class<?>[] cellValueTypes = new Class<?>[excelMeta.getTitleColumnNum()];
|
|
Class<?>[] cellValueTypes = new Class<?>[excelMeta.getTitleColumnNum()];
|
|
|
|
+
|
|
//写标题
|
|
//写标题
|
|
for (int i = 1; i <= excelMeta.getTitleRowNum(); i++) {
|
|
for (int i = 1; i <= excelMeta.getTitleRowNum(); i++) {
|
|
Row excelRow = sheet.createRow(i - 1);
|
|
Row excelRow = sheet.createRow(i - 1);
|
|
@@ -125,6 +134,7 @@ public final class ExcelWriter implements Closeable {
|
|
cellValueTypes[j - 1] = titleMeta.getValueType();
|
|
cellValueTypes[j - 1] = titleMeta.getValueType();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
//合并表头
|
|
//合并表头
|
|
//sheet.addMergedRegion(new CellRangeAddress(0, 0, 3, 8));
|
|
//sheet.addMergedRegion(new CellRangeAddress(0, 0, 3, 8));
|
|
//sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
|
|
//sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
|
|
@@ -133,11 +143,13 @@ public final class ExcelWriter implements Closeable {
|
|
|
|
|
|
// 行数
|
|
// 行数
|
|
int rowsCount = sheet.getPhysicalNumberOfRows();
|
|
int rowsCount = sheet.getPhysicalNumberOfRows();
|
|
|
|
+
|
|
//冻结表头
|
|
//冻结表头
|
|
//sheet.createFreezePane(0, rowsCount - 1);
|
|
//sheet.createFreezePane(0, rowsCount - 1);
|
|
|
|
|
|
// 写入内容
|
|
// 写入内容
|
|
List<Object[]> rows = ExcelBeanHelper.beanToExcelValueArrays(list, clazz);
|
|
List<Object[]> rows = ExcelBeanHelper.beanToExcelValueArrays(list, clazz);
|
|
|
|
+
|
|
// 列数
|
|
// 列数
|
|
//int colsCount = sheet.getRow(0).getPhysicalNumberOfCells();
|
|
//int colsCount = sheet.getRow(0).getPhysicalNumberOfCells();
|
|
for (int i = 0; i < rows.size(); i++) {
|
|
for (int i = 0; i < rows.size(); i++) {
|
|
@@ -157,14 +169,17 @@ public final class ExcelWriter implements Closeable {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
workbook.write(outputStream);
|
|
workbook.write(outputStream);
|
|
return true;
|
|
return true;
|
|
|
|
+
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
LOG.error("流异常", e);
|
|
LOG.error("流异常", e);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
LOG.error("其他异常", e);
|
|
LOG.error("其他异常", e);
|
|
} finally {
|
|
} finally {
|
|
}
|
|
}
|
|
|
|
+
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -173,6 +188,7 @@ public final class ExcelWriter implements Closeable {
|
|
* 合并列
|
|
* 合并列
|
|
*/
|
|
*/
|
|
private void mergeColumns(Sheet sheet, CellStyle cellStyle) {
|
|
private void mergeColumns(Sheet sheet, CellStyle cellStyle) {
|
|
|
|
+
|
|
// 行数
|
|
// 行数
|
|
int rowsCount = sheet.getPhysicalNumberOfRows();
|
|
int rowsCount = sheet.getPhysicalNumberOfRows();
|
|
// 列数
|
|
// 列数
|
|
@@ -186,12 +202,15 @@ public final class ExcelWriter implements Closeable {
|
|
|
|
|
|
for (int r = 0; r < rowsCount; r++) {
|
|
for (int r = 0; r < rowsCount; r++) {
|
|
row = sheet.getRow(r);
|
|
row = sheet.getRow(r);
|
|
|
|
+
|
|
// 重置
|
|
// 重置
|
|
colSpan = 0;
|
|
colSpan = 0;
|
|
row = sheet.getRow(r);
|
|
row = sheet.getRow(r);
|
|
for (int c = 0; c < colsCount; c++) {
|
|
for (int c = 0; c < colsCount; c++) {
|
|
|
|
+
|
|
cell1 = row.getCell(c);
|
|
cell1 = row.getCell(c);
|
|
cell2 = row.getCell(c + 1);
|
|
cell2 = row.getCell(c + 1);
|
|
|
|
+
|
|
if (cell1 == null) {// 如果当前单元格是空的,跳过,继续当前行的后一个单元格查找
|
|
if (cell1 == null) {// 如果当前单元格是空的,跳过,继续当前行的后一个单元格查找
|
|
if (c == colsCount - 1) {
|
|
if (c == colsCount - 1) {
|
|
break;
|
|
break;
|
|
@@ -199,6 +218,7 @@ public final class ExcelWriter implements Closeable {
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
if (cell2 == null) {// 说明当前行已经到最后一个单元格了
|
|
if (cell2 == null) {// 说明当前行已经到最后一个单元格了
|
|
if (colSpan >= 1) {// 判断colSpan是否大于等于1,大于1就要合并了
|
|
if (colSpan >= 1) {// 判断colSpan是否大于等于1,大于1就要合并了
|
|
// 合并行中连续相同的值的单元格
|
|
// 合并行中连续相同的值的单元格
|
|
@@ -206,6 +226,7 @@ public final class ExcelWriter implements Closeable {
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
if (cell1 != null && cell2 != null) {
|
|
if (cell1 != null && cell2 != null) {
|
|
// 如果当前单元格和下一个单元格内容相同,那么colSpan加1
|
|
// 如果当前单元格和下一个单元格内容相同,那么colSpan加1
|
|
if (cell1.getStringCellValue().equals(cell2.getStringCellValue())) {
|
|
if (cell1.getStringCellValue().equals(cell2.getStringCellValue())) {
|
|
@@ -238,6 +259,7 @@ public final class ExcelWriter implements Closeable {
|
|
Row row = null;
|
|
Row row = null;
|
|
Cell cell = null;
|
|
Cell cell = null;
|
|
String[] lastRowVals = new String[excelMeta.getTitleColumnNum()];
|
|
String[] lastRowVals = new String[excelMeta.getTitleColumnNum()];
|
|
|
|
+
|
|
for (int r = 0; r < excelMeta.getTitleRowNum(); r++) {
|
|
for (int r = 0; r < excelMeta.getTitleRowNum(); r++) {
|
|
for (int c = 0; c < excelMeta.getTitleColumnNum(); c++) {
|
|
for (int c = 0; c < excelMeta.getTitleColumnNum(); c++) {
|
|
row = sheet.getRow(r);
|
|
row = sheet.getRow(r);
|