Commit dc86262c authored by yanzg's avatar yanzg

接口文档的支持

parent 3042e676
......@@ -7,10 +7,8 @@ import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.table.TableHead;
import com.yanzuoguang.util.table.TableHeadHelper;
import com.yanzuoguang.util.table.TableHeadItem;
import org.apache.poi.ss.usermodel.Cell;
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.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
......@@ -49,6 +47,11 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
*/
private Sheet sheet;
/**
* 单元格央视
*/
private CellStyle style;
/**
* 行号
*/
......@@ -161,6 +164,9 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
// 创建工作簿对象
workbook = new XSSFWorkbook();
sheet = workbook.createSheet();
style = initColumnCenterstyle(workbook);
// 行和列都是从0开始计数,且起始结束都会合并
// 写入标题、子标题
......@@ -171,6 +177,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
writeHead(head);
rowIndex += head.getTotalRow();
}
/**
......@@ -185,8 +192,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
// 创建一行
Row row = sheet.createRow(rowIndex);
row.setHeight(getUnit(rowHeight));
Cell cell = row.createCell(0);
cell.setCellValue(content);
Cell cell = createCell(row, 0, content);
// 这里是合并excel中多列为1列
CellRangeAddress region = new CellRangeAddress(rowIndex, rowIndex, 0, columnLength);
sheet.addMergedRegion(region);
......@@ -208,8 +214,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
// 写入列头
for (TableHeadItem headItem : head.getColumns()) {
Row row = rows[headItem.getRow()];
Cell cell = row.createCell(headItem.getColumn());
cell.setCellValue(headItem.getName());
Cell cell = createCell(row, headItem.getColumn(), headItem.getName());
// 判断是否需要合并列头
if (headItem.getColumnCell() > 1 || headItem.getRowCell() > 1) {
......@@ -300,19 +305,11 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
mergerData(mergerData, columnPos);
} else {
// 当不需要合并历史记录时,则创建新的内容
Cell cell = row.createCell(columnPos);
cell.setCellValue(value);
Cell cell = createCell(row, columnPos, value);
}
// if (mergerData.getRowIndexHistory() != mergerData.getRowIndex() - 1) {
// // 合并历史记录单元格
// mergerData(mergerData, columnPos);
// }
} else {
// 不合并时直接写入单元格内容
Cell cell = row.createCell(columnPos);
cell.setCellValue(value);
Cell cell = createCell(row, columnPos, value);
}
}
......@@ -330,6 +327,8 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
int rowEnd = rowStart + mergerColumn.getRowCell() - 1;
CellRangeAddress region = new CellRangeAddress(rowStart, rowEnd, columnPos, columnPos);
sheet.addMergedRegion(region);
Cell cell = sheet.getRow(rowStart).getCell(columnPos);
}
/**
......@@ -385,4 +384,53 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
String fileName = String.format("%s/%s", this.config.getServerPath(), this.config.getFileName());
return fileName;
}
/**
* 创建单元格
*
* @param row
* @param column
* @param content
* @return
*/
private Cell createCell(Row row, int column, String content) {
Cell cell = row.createCell(column);
cell.setCellStyle(style);
cell.setCellValue(content);
return cell;
}
/**
* <br>
* <b>功能:</b>单元格的默认样式<br>
* <b>作者:</b>yixq<br>
* <b>@param wb
* <b>@return</b>
*/
public CellStyle initColumnCenterstyle(Workbook wb) {
CellStyle cellStyle = wb.createCellStyle();
Font font = wb.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 10);
cellStyle.setFont(font);
// 左右居中
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
// 上下居中
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
cellStyle.setWrapText(true);
cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderLeft((short) 1);
cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderRight((short) 1);
// 设置单元格的边框为粗体
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
// 设置单元格的边框颜色.
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
// 设置单元格的背景颜色.
cellStyle.setFillForegroundColor(HSSFColor.WHITE.index);
return cellStyle;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment