| | |
| | | import org.apache.poi.ss.usermodel.*; |
| | | import org.apache.poi.ss.util.CellRangeAddress; |
| | | import org.apache.poi.ss.util.CellRangeAddressList; |
| | | import org.apache.poi.xssf.streaming.SXSSFWorkbook; |
| | | import org.apache.poi.xssf.usermodel.XSSFClientAnchor; |
| | | import org.apache.poi.xssf.usermodel.XSSFDataValidation; |
| | | import org.apache.poi.hssf.usermodel.HSSFClientAnchor; |
| | | import org.apache.poi.hssf.usermodel.HSSFDataValidation; |
| | | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
| | | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
| | | import org.apache.poi.hssf.usermodel.HSSFClientAnchor; |
| | | import org.apache.poi.hssf.usermodel.HSSFDataValidation; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | |
| | |
| | | //sheet.setColumnWidth(1, 15 * 256); // 第二列宽度15字符 |
| | | //sheet.setColumnWidth(2, 25 * 256); // 第三列宽度25字符 // 设置第一列宽度为20个字符 |
| | | |
| | | ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) (cell.getColumnIndex()+1), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() +2), |
| | | ClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) (cell.getColumnIndex()+1), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() +2), |
| | | cell.getRow().getRowNum() + 1); |
| | | // 计算居中位置 |
| | | // int col1 = 0; // 中间列 |
| | |
| | | Font font = wb.createFont(); |
| | | font.setBold(true); |
| | | style.setFont(font); |
| | | style.setWrapText(true); // 设置自动换行 |
| | | style.setWrapText(true); // 设置自动换行 |
| | | cell.setCellStyle(style); |
| | | cell.setCellValue(tits[i]); |
| | | Cell cel = row.createCell(1); |
| | |
| | | if(i==3) |
| | | { |
| | | row = sheet.createRow(8); |
| | | // 设置行高为自动调整 |
| | | row.setHeight((short) -1); |
| | | // row.setHeight((short) 30); |
| | | row.setHeightInPoints(40); // 设置行高为20磅 |
| | | row = sheet.createRow(9); |
| | |
| | | //sheet.setColumnWidth(1, 15 * 256); // 第二列宽度15字符 |
| | | //sheet.setColumnWidth(2, 25 * 256); // 第三列宽度25字符 // 设置第一列宽度为20个字符 |
| | | |
| | | ClientAnchor anchor1 = new XSSFClientAnchor(0, 0, 0, 0, (short) (cell1.getColumnIndex()), cell1.getRow().getRowNum(), (short) (cell1.getColumnIndex() +2), |
| | | ClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 0, 0, (short) (cell1.getColumnIndex()), cell1.getRow().getRowNum(), (short) (cell1.getColumnIndex() +2), |
| | | cell1.getRow().getRowNum() + 1); |
| | | // 计算居中位置 |
| | | //// 设置图片大小和位置 |
| | |
| | | CellStyle style = wb.createCellStyle(); |
| | | style.setAlignment(HorizontalAlignment.CENTER); |
| | | style.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | // 设置单元格内容自动换行 |
| | | style.setWrapText(true); |
| | | style.setBorderRight(BorderStyle.THIN); |
| | | style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); |
| | | style.setBorderLeft(BorderStyle.THIN); |
| | |
| | | style.cloneStyleFrom(styles.get("data")); |
| | | style.setAlignment(HorizontalAlignment.CENTER); |
| | | style.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | // 设置单元格内容自动换行 |
| | | style.setWrapText(true); |
| | | style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex()); |
| | | style.setFillPattern(FillPatternType.SOLID_FOREGROUND); |
| | | Font headerFont = wb.createFont(); |
| | |
| | | style = wb.createCellStyle(); |
| | | style.setAlignment(HorizontalAlignment.CENTER); |
| | | style.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | // 设置单元格内容自动换行 |
| | | style.setWrapText(true); |
| | | Font totalFont = wb.createFont(); |
| | | totalFont.setFontName("Arial"); |
| | | totalFont.setFontHeightInPoints((short) 10); |
| | |
| | | style = wb.createCellStyle(); |
| | | style.cloneStyleFrom(styles.get("data")); |
| | | style.setAlignment(HorizontalAlignment.LEFT); |
| | | // 设置单元格内容自动换行 |
| | | style.setWrapText(true); |
| | | styles.put("data1", style); |
| | | |
| | | style = wb.createCellStyle(); |
| | | style.cloneStyleFrom(styles.get("data")); |
| | | style.setAlignment(HorizontalAlignment.CENTER); |
| | | // 设置单元格内容自动换行 |
| | | style.setWrapText(true); |
| | | styles.put("data2", style); |
| | | |
| | | style = wb.createCellStyle(); |
| | | style.cloneStyleFrom(styles.get("data")); |
| | | style.setAlignment(HorizontalAlignment.RIGHT); |
| | | // 设置单元格内容自动换行 |
| | | style.setWrapText(true); |
| | | styles.put("data3", style); |
| | | |
| | | return styles; |
| | |
| | | } else if (Excel.ColumnType.NUMERIC == attr.cellType()) { |
| | | cell.setCellValue(StringUtils.contains(Convert.toStr(value), ".") ? Convert.toDouble(value) : Convert.toInt(value)); |
| | | } else if (Excel.ColumnType.IMAGE == attr.cellType()) { |
| | | ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1), |
| | | ClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1), |
| | | cell.getRow().getRowNum() + 1); |
| | | String imagePath = Convert.toStr(value); |
| | | if (StringUtils.isNotEmpty(imagePath)) { |
| | |
| | | // 如果设置了提示信息则鼠标放上去提示. |
| | | if (StringUtils.isNotEmpty(attr.prompt())) { |
| | | // 这里默认设了2-101列提示. |
| | | setXSSFPrompt(sheet, "", attr.prompt(), 1, 100, column, column); |
| | | setHSSFPrompt(sheet, "", attr.prompt(), 1, 100, column, column); |
| | | } |
| | | // 如果设置了combo属性则本列只能选择不能输入 |
| | | if (attr.combo().length > 0) { |
| | | // 这里默认设了2-101列只能选择不能输入. |
| | | setXSSFValidation(sheet, attr.combo(), 1, 100, column, column); |
| | | setHSSFValidation(sheet, attr.combo(), 1, 100, column, column); |
| | | } |
| | | } |
| | | |
| | |
| | | public Cell addCell(Excel attr, Row row, T vo, Field field, int column) { |
| | | Cell cell = null; |
| | | try { |
| | | // 设置行高 |
| | | row.setHeight((short) 2000); |
| | | // 设置行高为自动调整 |
| | | row.setHeight((short) -1); |
| | | // 根据Excel中设置情况决定是否导出,有些情况需要保持为空,希望用户填写这一列. |
| | | if (attr.isExport()) { |
| | | // 创建cell |
| | |
| | | } |
| | | |
| | | /** |
| | | * 设置 POI XSSFSheet 单元格提示 |
| | | * 设置 POI HSSFSheet 单元格提示 |
| | | * |
| | | * @param sheet 表单 |
| | | * @param promptTitle 提示标题 |
| | |
| | | * @param firstCol 开始列 |
| | | * @param endCol 结束列 |
| | | */ |
| | | public void setXSSFPrompt(Sheet sheet, String promptTitle, String promptContent, int firstRow, int endRow, |
| | | public void setHSSFPrompt(Sheet sheet, String promptTitle, String promptContent, int firstRow, int endRow, |
| | | int firstCol, int endCol) { |
| | | DataValidationHelper helper = sheet.getDataValidationHelper(); |
| | | DataValidationConstraint constraint = helper.createCustomConstraint("DD1"); |
| | |
| | | * @param endCol 结束列 |
| | | * @return 设置好的sheet. |
| | | */ |
| | | public void setXSSFValidation(Sheet sheet, String[] textlist, int firstRow, int endRow, int firstCol, int endCol) { |
| | | public void setHSSFValidation(Sheet sheet, String[] textlist, int firstRow, int endRow, int firstCol, int endCol) { |
| | | DataValidationHelper helper = sheet.getDataValidationHelper(); |
| | | // 加载下拉列表内容 |
| | | DataValidationConstraint constraint = helper.createExplicitListConstraint(textlist); |
| | |
| | | // 数据有效性对象 |
| | | DataValidation dataValidation = helper.createValidation(constraint, regions); |
| | | // 处理Excel兼容性问题 |
| | | if (dataValidation instanceof XSSFDataValidation) { |
| | | dataValidation.setSuppressDropDownArrow(true); |
| | | dataValidation.setShowErrorBox(true); |
| | | } else { |
| | | if (dataValidation instanceof HSSFDataValidation) { |
| | | dataValidation.setSuppressDropDownArrow(false); |
| | | dataValidation.setShowErrorBox(true); |
| | | } |
| | | |
| | | sheet.addValidationData(dataValidation); |
| | |
| | | * 创建一个工作簿 |
| | | */ |
| | | public void createWorkbook() { |
| | | this.wb = new SXSSFWorkbook(500); |
| | | this.wb = new HSSFWorkbook(); |
| | | } |
| | | |
| | | /** |