|
@@ -672,4 +672,86 @@ public class POIUtil {
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将数据集dataset导出到fileName文件中
|
|
|
+ *
|
|
|
+ * @param headColumns 导出文件的列名
|
|
|
+ * @param fieldColumns
|
|
|
+ * @param dataset 数据源
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ * @throws NoSuchMethodException
|
|
|
+ * @throws InvocationTargetException
|
|
|
+ * @throws IllegalAccessException
|
|
|
+ */
|
|
|
+ public static <T> HSSFWorkbook multipleSheetExportExcel(String[] headColumns, String[] fieldColumns, List<T> dataset,HSSFWorkbook workbook) throws IOException, IllegalAccessException,
|
|
|
+ InvocationTargetException, NoSuchMethodException {
|
|
|
+ if (headColumns == null) {
|
|
|
+ throw new UtilException("excel列名不能为空");
|
|
|
+ }
|
|
|
+ if (dataset == null) {
|
|
|
+ throw new UtilException("数据集不能为空");
|
|
|
+ }
|
|
|
+ /*if (dataset.size() > MAX_DATA_SIZE) {
|
|
|
+ throw new UtilException("数据集太大,不能导出.最大数据集不能超过" + MAX_DATA_SIZE);
|
|
|
+ }*/
|
|
|
+ if(workbook == null) {
|
|
|
+ workbook = new HSSFWorkbook();
|
|
|
+ }
|
|
|
+ HSSFCellStyle style = workbook.createCellStyle(); // 获取单元格样式
|
|
|
+ /************** 设置单元格样式 *************/
|
|
|
+ style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直
|
|
|
+ style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平
|
|
|
+ style.setWrapText(true);
|
|
|
+ HSSFFont font = workbook.createFont();
|
|
|
+ font.setColor(HSSFFont.COLOR_NORMAL);
|
|
|
+ font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
|
|
|
+ style.setFont(font);
|
|
|
+
|
|
|
+ HSSFSheet sheet = workbook.createSheet(); // 创建sheet
|
|
|
+ // 设置表头
|
|
|
+ // 创建第一行
|
|
|
+ HSSFRow row1 = sheet.createRow(0);
|
|
|
+ HSSFCell cell = null;
|
|
|
+ for (int i = 0; i < headColumns.length; i++) {
|
|
|
+ // 创建列
|
|
|
+ cell = row1.createCell(i);
|
|
|
+ // 定义单元格为字符串类型
|
|
|
+ cell.setCellType(HSSFCell.CELL_TYPE_STRING);
|
|
|
+ // 设置单元格的样式
|
|
|
+ cell.setCellStyle(style);
|
|
|
+ // 设置单元格的值
|
|
|
+ cell.setCellValue(headColumns[i]);
|
|
|
+ }
|
|
|
+ HSSFRow row = null;
|
|
|
+ // 添加数据
|
|
|
+ T data = null;
|
|
|
+ Object obj = null;
|
|
|
+ for (int i = 1; i <= dataset.size(); i++) {
|
|
|
+ data = dataset.get(i - 1);
|
|
|
+ if (data != null) {
|
|
|
+ row = sheet.createRow(i);
|
|
|
+ for (int j = 0; j < fieldColumns.length; j++) {
|
|
|
+ cell = row.createCell(j);
|
|
|
+ try {
|
|
|
+ obj = PropertyUtils.getNestedProperty(data, fieldColumns[j]);
|
|
|
+ if (obj instanceof Date) {
|
|
|
+ obj = sdf.format(obj);
|
|
|
+ }
|
|
|
+ } catch (NestedNullException e) {
|
|
|
+ LOGGER.warn(e.getMessage());
|
|
|
+ obj = null;
|
|
|
+ }
|
|
|
+ if (obj != null) {
|
|
|
+ cell.setCellValue(obj.toString());
|
|
|
+ } else {
|
|
|
+ cell.setCellValue("");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return workbook;
|
|
|
+ }
|
|
|
+
|
|
|
}
|