| package com.example.demoweb.demo; |
| |
| import org.apache.poi.ss.usermodel.*; |
| import org.apache.poi.ss.util.CellRangeAddress; |
| import org.apache.poi.ss.util.RegionUtil; |
| import org.apache.poi.xssf.usermodel.XSSFCellStyle; |
| import org.apache.poi.xssf.usermodel.XSSFFont; |
| import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
| |
| import java.io.FileOutputStream; |
| import java.io.IOException; |
| import java.io.OutputStream; |
| import java.text.SimpleDateFormat; |
| import java.util.Date; |
| import java.util.HashMap; |
| import java.util.Map; |
| |
| public class ExcelExportUtil { |
| |
| public static void main(String[] args) { |
| Workbook wb = new XSSFWorkbook(); |
| Sheet sheet = wb.createSheet("test"); |
| Row row1 = sheet.createRow(0); |
| |
| |
| |
| |
| |
| |
| Cell mergeCell = row1.createCell(1); |
| ExcelExportUtil.addWlergedRegion(wb, sheet, 0, 1,1, 1); |
| String mergeMaxValue = "北京市卫健委刚刚发布消息,10月25日0时至24时,北京无新增报告本地确诊病例、疑似病例和无症状感染者;无新增报告境外输入确诊病例、疑似病例和无症状感染者。私家车等交通方式,若乘坐公共交通工具,全程佩戴口單,注意与其他乘客保持安全距离,乘坐时尽量开窗通风;就医时全程佩戴口罩;做好手卫生,尽量避免触摸门把手、挂号机、取款机等物体表面,接触后及时洗手或用速干手消毒剂揉搓双手。打喷嚏、咳嗽时用纸巾或肘臂遮挡;"; |
| String mergeMinValue = "啊不吃等"; |
| ExcelExportUtil.setCellValue(wb, mergeCell, mergeMaxValue); |
| ExcelExportUtil.calcAndSetRowHeigt(mergeCell); |
| |
| Cell cell1_1 = row1.createCell(0); |
| |
| ExcelExportUtil.setCellValue(wb, cell1_1, "尽量选择楼梯步行,若乘坐厢式电梯,应分散乘梯,避免同梯人员过多过密"); |
| ExcelExportUtil.calcAndSetRowHeigt(cell1_1); |
| |
| if(null != sheet.getRow(1)){ |
| Row row2 = sheet.getRow(1); |
| if(null != row2.getCell(0)){ |
| Cell cell2_1 = row2.getCell(0); |
| System.out.println(cell2_1.getStringCellValue()); |
| } else { |
| Cell cell2_1 = row2.createCell(0); |
| |
| ExcelExportUtil.setCellValue(wb, cell2_1, "市卫健委提醒,疫情防控常态化形势下,市民朋友前往医疗机构,要备好口罩、消毒湿纸巾或免洗洗手液。如有发热症状的患者就医时应佩戴口罩,"); |
| ExcelExportUtil.calcAndSetRowHeigt(cell2_1); |
| } |
| } |
| |
| try { |
| OutputStream out = new FileOutputStream("C:\\Users\\hegg\\Desktop\\信用管理模板文件(未加密)/" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()).toString() +".xlsx"); |
| wb.write(out); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static void addWlergedRegion(Workbook wb, Sheet sheet, int firstRow, int lastRow, int firstCol, int lastCol) { |
| |
| CellRangeAddress regionB = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol); |
| sheet.addMergedRegion(regionB); |
| |
| |
| |
| |
| RegionUtil.setBorderBottom(BorderStyle.THIN, regionB, sheet); |
| RegionUtil.setBorderLeft(BorderStyle.THIN, regionB, sheet); |
| RegionUtil.setBorderRight(BorderStyle.THIN, regionB, sheet); |
| RegionUtil.setBorderTop(BorderStyle.THIN, regionB, sheet); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| public static void setCellValue(Workbook wb, Cell cell, String value) { |
| cell.setCellValue(value); |
| cell.setCellStyle(setCellStyle(wb)); |
| } |
| |
| |
| |
| |
| |
| |
| |
| public static CellStyle setCellStyle(Workbook wb) { |
| CellStyle style = wb.createCellStyle(); |
| |
| |
| style.setBorderBottom(BorderStyle.THIN); |
| style.setBorderLeft(BorderStyle.THIN); |
| style.setBorderRight(BorderStyle.THIN); |
| style.setBorderTop(BorderStyle.THIN); |
| |
| style.setVerticalAlignment(VerticalAlignment.CENTER); |
| |
| style.setWrapText(true); |
| Font font = wb.createFont(); |
| font.setFontName("宋体"); |
| font.setFontHeightInPoints((short) 11); |
| style.setFont(font); |
| return style; |
| } |
| |
| |
| |
| |
| |
| |
| private static Map<String, Object> getCellInfo(Cell cell) { |
| Sheet sheet = cell.getSheet(); |
| int rowIndex = cell.getRowIndex(); |
| int columnIndex = cell.getColumnIndex(); |
| boolean isPartOfRegion = false; |
| int firstColumn = 0; |
| int lastColumn = 0; |
| int firstRow = 0; |
| int lastRow = 0; |
| int sheetMergeCount = sheet.getNumMergedRegions(); |
| for (int i = 0; i < sheetMergeCount; i++) { |
| CellRangeAddress ca = sheet.getMergedRegion(i); |
| firstColumn = ca.getFirstColumn(); |
| lastColumn = ca.getLastColumn(); |
| firstRow = ca.getFirstRow(); |
| lastRow = ca.getLastRow(); |
| if (rowIndex >= firstRow && rowIndex <= lastRow) { |
| if (columnIndex >= firstColumn && columnIndex <= lastColumn) { |
| isPartOfRegion = true; |
| break; |
| } |
| } |
| } |
| Map<String, Object> map = new HashMap<>(); |
| Integer width = 0; |
| Integer height = 0; |
| boolean isPartOfRowsRegion = false; |
| if (isPartOfRegion) { |
| for (int i = firstColumn; i <= lastColumn; i++) { |
| width += sheet.getColumnWidth(i); |
| } |
| for (int i = firstRow; i <= lastRow; i++) { |
| height += sheet.getRow(i).getHeight(); |
| } |
| if (lastRow > firstRow) { |
| isPartOfRowsRegion = true; |
| } |
| } else { |
| width = sheet.getColumnWidth(columnIndex); |
| height += cell.getRow().getHeight(); |
| } |
| map.put("firstRow", firstRow); |
| map.put("lastRow", lastRow); |
| map.put("firstColumn", firstColumn); |
| map.put("lastColumn", lastColumn); |
| map.put("width", width); |
| map.put("height", height); |
| map.put("isPartOfRowsRegion", isPartOfRowsRegion); |
| return map; |
| } |
| |
| |
| |
| |
| |
| |
| |
| private static String getCellContentAsString(Cell cell) { |
| if (null == cell) { |
| return ""; |
| } |
| String result = ""; |
| switch (cell.getCellType()) { |
| case NUMERIC: |
| String s = String.valueOf(cell.getNumericCellValue()); |
| if (s != null) { |
| if (s.endsWith(".0")) { |
| s = s.substring(0, s.length() - 2); |
| } |
| } |
| result = s; |
| break; |
| case STRING: |
| result = String.valueOf(cell.getStringCellValue()); |
| break; |
| case BLANK: |
| break; |
| case BOOLEAN: |
| result = String.valueOf(cell.getBooleanCellValue()); |
| break; |
| case ERROR: |
| break; |
| default: |
| break; |
| } |
| return result; |
| } |
| |
| |
| |
| |
| public static void calcAndSetRowHeigt(Cell cell) { |
| |
| String cellContent = getCellContentAsString(cell); |
| if (null != cellContent && !"".equals(cellContent)) { |
| Row row = cell.getRow(); |
| |
| double maxHeight = row.getHeight(); |
| |
| Map<String, Object> cellInfoMap = getCellInfo(cell); |
| Integer cellWidth = (Integer) cellInfoMap.get("width"); |
| Integer cellHeight = (Integer) cellInfoMap.get("height"); |
| if (cellHeight > maxHeight) { |
| maxHeight = cellHeight; |
| } |
| XSSFCellStyle cellstyle = (XSSFCellStyle) cell.getCellStyle(); |
| XSSFFont font = cellstyle.getFont(); |
| |
| short fontHeight = font.getFontHeight(); |
| double cellContentWidth = cellContent.getBytes().length * 2 * 256; |
| |
| |
| double stringNeedsRows = (double) cellContentWidth / cellWidth; |
| if (stringNeedsRows < 1.0) { |
| stringNeedsRows = 1.0; |
| } |
| |
| double stringNeedsHeight = (double) fontHeight * stringNeedsRows; |
| |
| if (stringNeedsHeight > maxHeight) { |
| maxHeight = stringNeedsHeight / 2.25; |
| |
| maxHeight = Math.ceil(maxHeight); |
| |
| Boolean isPartOfRowsRegion = (Boolean) cellInfoMap.get("isPartOfRowsRegion"); |
| if (isPartOfRowsRegion) { |
| Integer firstRow = (Integer) cellInfoMap.get("firstRow"); |
| Integer lastRow = (Integer) cellInfoMap.get("lastRow"); |
| double addHeight = (maxHeight - cellHeight) / (lastRow - firstRow + 1); |
| for (int i = firstRow; i <= lastRow; i++) { |
| double rowsRegionHeight = row.getSheet().getRow(i).getHeight() + addHeight; |
| row.getSheet().getRow(i).setHeight((short) (rowsRegionHeight)); |
| } |
| } else { |
| if(maxHeight > cell.getRow().getHeight()){ |
| row.setHeight((short)(maxHeight)); |
| } |
| } |
| } else { |
| row.setHeight((short)(stringNeedsHeight)); |
| } |
| } |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?