use of com.jeesuite.common2.excel.model.ExcelMeta in project jeesuite-libs by vakinge.
the class ExcelBeanHelper method doCacheClass.
/**
* @param clazz
* @param canonicalName
* @return
* @throws IntrospectionException
*/
private static synchronized void doCacheClass(Class<?> clazz, String canonicalName) throws Exception {
if (aliasPropertyDescriptorCache.containsKey(canonicalName))
return;
Map<String, PropertyDescriptor> map = new HashMap<>();
Map<String, PropertyDescriptor> aliasMap = new HashMap<>();
List<TitleMeta> titleMetas = new ArrayList<>();
BeanInfo srcBeanInfo = Introspector.getBeanInfo(clazz);
PropertyDescriptor[] descriptors = srcBeanInfo.getPropertyDescriptors();
Map<String, TitleMeta> parentMap = new HashMap<>();
int maxRow = 1;
for (PropertyDescriptor descriptor : descriptors) {
String name = descriptor.getName();
if ("class".equals(name))
continue;
Method readMethod = descriptor.getReadMethod();
Method writeMethod = descriptor.getWriteMethod();
if (readMethod == null)
try {
readMethod = clazz.getMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1));
descriptor.setReadMethod(readMethod);
} catch (NoSuchMethodException | SecurityException e) {
}
if (writeMethod == null)
try {
writeMethod = clazz.getMethod("set" + name.substring(0, 1).toUpperCase() + name.substring(1), descriptor.getPropertyType());
descriptor.setWriteMethod(writeMethod);
} catch (NoSuchMethodException | SecurityException e) {
}
if (readMethod != null || writeMethod != null) {
map.put(descriptor.getName(), descriptor);
//
TitleCell annotation = null;
try {
annotation = clazz.getDeclaredField(name).getAnnotation(TitleCell.class);
} catch (NoSuchFieldException e) {
annotation = clazz.getSuperclass().getDeclaredField(name).getAnnotation(TitleCell.class);
}
if (annotation != null) {
aliasMap.put(annotation.name().trim(), descriptor);
TitleMeta cell = new TitleMeta(annotation.name());
cell.setValueType(annotation.type());
if (StringUtils.isBlank(annotation.parentName())) {
cell.setColumnIndex(annotation.column());
cell.setRowIndex(annotation.row());
titleMetas.add(cell);
} else {
TitleMeta cellParent = parentMap.get(annotation.parentName());
if (cellParent == null) {
cellParent = new TitleMeta(annotation.parentName());
cellParent.setValueType(annotation.type());
cellParent.setColumnIndex(annotation.column());
parentMap.put(annotation.parentName(), cellParent);
titleMetas.add(cellParent);
}
cell.setColumnIndex(annotation.column());
cell.setRowIndex(annotation.row());
cellParent.addChildren(cell);
maxRow = annotation.row() > maxRow ? annotation.row() : maxRow;
}
}
}
}
ExcelMeta excelMeta = new ExcelMeta(clazz, titleMetas, maxRow);
titleCellBeanCache.put(canonicalName, excelMeta);
aliasPropertyDescriptorCache.put(canonicalName, aliasMap);
}
use of com.jeesuite.common2.excel.model.ExcelMeta in project jeesuite-libs by vakinge.
the class ExcelWriter method write.
/**
* 将数据写入excel文件
*
* @param list 数据列表
* @param <T> 泛型
* @return 写入结果
*/
public <T> boolean write(List<T> list, Class<T> clazz) {
ExcelMeta excelMeta = ExcelBeanHelper.getExcelMeta(clazz);
try {
Sheet sheet = workbook.createSheet(this.sheetName);
sheet.setDefaultColumnWidth(15);
CellStyle titleStyle = workbook.createCellStyle();
titleStyle.setAlignment(HorizontalAlignment.CENTER);
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
titleStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// 下边框
titleStyle.setBorderBottom(BorderStyle.THIN);
titleStyle.setBorderLeft(BorderStyle.THIN);
Font font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 13);
titleStyle.setFont(font);
// 列值类型
Class<?>[] cellValueTypes = new Class<?>[excelMeta.getTitleColumnNum()];
// 写标题
for (int i = 1; i <= excelMeta.getTitleRowNum(); i++) {
Row excelRow = sheet.createRow(i - 1);
for (int j = 1; j <= excelMeta.getTitleColumnNum(); j++) {
TitleMeta titleMeta = excelMeta.getTitleMeta(i, j);
Cell cell = excelRow.createCell(j - 1);
cell.setCellValue(titleMeta == null ? "" : titleMeta.getTitle());
cell.setCellStyle(titleStyle);
cellValueTypes[j - 1] = titleMeta.getValueType();
}
}
// 合并表头
// sheet.addMergedRegion(new CellRangeAddress(0, 0, 3, 8));
// sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
mergeColumns(sheet, titleStyle);
mergeRows(sheet, titleStyle, excelMeta);
// 行数
int rowsCount = sheet.getPhysicalNumberOfRows();
// 冻结表头
// sheet.createFreezePane(0, rowsCount - 1);
// 写入内容
List<Object[]> rows = ExcelBeanHelper.beanToExcelValueArrays(list, clazz);
// int colsCount = sheet.getRow(0).getPhysicalNumberOfCells();
for (int i = 0; i < rows.size(); i++) {
Row excelRow = sheet.createRow(i + rowsCount);
Object[] vals = rows.get(i);
for (int j = 0; j < vals.length; j++) {
Cell cell = excelRow.createCell(j);
if (cellValueTypes[j] == int.class || cellValueTypes[j] == Integer.class) {
cell.setCellValue(vals[j] == null ? 0f : Integer.parseInt(vals[j].toString()));
} else if (cellValueTypes[j] == float.class || cellValueTypes[j] == Float.class || cellValueTypes[j] == double.class || cellValueTypes[j] == Double.class || cellValueTypes[j] == BigDecimal.class) {
cell.setCellValue(vals[j] == null ? 0d : Double.parseDouble(vals[j].toString()));
} else {
cell.setCellValue(vals[j] == null ? "" : vals[j].toString());
}
}
}
workbook.write(outputStream);
return true;
} catch (IOException e) {
LOG.error("流异常", e);
} catch (Exception e) {
LOG.error("其他异常", e);
} finally {
}
return false;
}
Aggregations