shz.excel.handler.ExcelWriteHandler Maven / Gradle / Ivy
The newest version!
package shz.excel.handler;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import shz.excel.ExcelHelp;
public class ExcelWriteHandler {
public void invokeHead(Workbook book, int sheetIndex, Sheet sheet, int rowIndex, Row row) {
}
public void setHeadValue(Workbook book, int sheetIndex, Sheet sheet, int rowIndex, Row row, int columnIndex, Cell cell, String headName) {
//设置列宽
int columnWidth = sheet.getColumnWidth(columnIndex);
int newColumnWidth = headName.length() * 256;
if (newColumnWidth > columnWidth) sheet.setColumnWidth(columnIndex, newColumnWidth);
// 设置行高
float heightInPoints = row.getHeightInPoints();
float newHeightInPoints = 20;
if (newHeightInPoints > heightInPoints) row.setHeightInPoints(newHeightInPoints);
CellStyle cellStyle = book.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
Font font = book.createFont();
font.setFontName("宋体");
font.setBold(true);
font.setFontHeightInPoints((short) 14);
font.setColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
cellStyle.setFont(font);
cell.setCellStyle(cellStyle);
cell.setCellValue(headName);
}
public void invoke(Workbook book, int sheetIndex, Sheet sheet, int rowIndex, Row row) {
}
public void setValue(Workbook book, int sheetIndex, Sheet sheet, int rowIndex, Row row, int columnIndex, Cell cell, Object value) {
if (value instanceof Cell) cell.setCellValue(ExcelHelp.getCellValue((Cell) value));
else cell.setCellValue(value == null ? "" : value.toString());
}
}