猿问

Apache POI:找出溢出到下一列的文本的 colspan

我正在使用 Apache POI 将 Excel 转换为 HTML。我希望 HTML 表格与它在 Excel 中显示的完全一样。我经常看到 Excel 单元格中的数据溢出到下一列的情况。有没有办法找出数据跨越多少列,以便我可以向标签添加colspan属性?TD从 Excel 中查看此屏幕截图:

在这种情况下,单元格 A2 中的数据溢出到单元格 B2(A3 也溢出到 B3)。有没有办法查到A2单元格对应的TD标签需要colspan="2"属性?

数据溢出的单元格没有合并,所以我不能真正使用像sheet.getNumMergedRegions()

我想如果我能以某种方式找出 Excel 中列的“可见”宽度,我也可以计算它。但是,sheet.getColumnWidth()仅提供实际宽度。我没有看到在 Excel 中找出列的“可见”宽度的方法。在上面的截图链接中,A 列的可见宽度非常小。有没有办法找到“可见”宽度?

我正在使用 Apache POI 3.17


慕桂英3389331
浏览 140回答 1
1回答

人到中年有点甜

Apache poi能够根据内容自动调整列的大小。所以它需要能够计算特殊内容需要的列宽。这就是SheetUtil.getCellWidth正在做的事情。此外,还需要了解Microsoft在Excel. 例如,在ExcelsGUI中,列宽为 10 意味着默认字符宽度的 10 个字符适合单元格宽度。但在内部,宽度是以默认字符宽度的 1/256 为单位计算的。这就是为什么apache poi决定以字符宽度的 1/256 为单位获取Sheet.getColumnWidth的原因。所以如果你有一个Cell cell单元格索引c和一个特殊的内容,那么使用Workbook workbook......DataFormatter dataFormatter = new DataFormatter();...int defaultCharWidth = SheetUtil.getDefaultCharWidth(workbook);...double cellValueWidth = SheetUtil.getCellWidth(cell, defaultCharWidth, dataFormatter, false);int neededColunmnWidth = (int)cellValueWidth*256;int columnWidth = sheet.getColumnWidth(c);...您可以确定内容是否适合单元格。它适合 if columnWidth >= neededColunmnWidth,否则它不适合并且colspan必须使用。让我们有一个完整的例子来说明原理:床单:代码:import org.apache.poi.ss.usermodel.*;import org.apache.poi.ss.util.SheetUtil;import java.io.*;class ExcelToHTMLColspan {&nbsp;public static void main(String[] args) throws Exception{&nbsp; Workbook workbook = WorkbookFactory.create(new FileInputStream("Test.xlsx"));&nbsp; DataFormatter dataFormatter = new DataFormatter();&nbsp; int defaultCharWidth = SheetUtil.getDefaultCharWidth(workbook);&nbsp; int lastColumnToExport = 5; // column E&nbsp; Sheet sheet = workbook.getSheetAt(0);&nbsp; Row row;&nbsp; Cell cell;&nbsp; String cellValue;&nbsp; StringBuilder tableHTML = new StringBuilder();&nbsp; tableHTML.append("<TABLE>");&nbsp; tableHTML.append("<COLGROUP>");&nbsp; for (int c = 0; c < lastColumnToExport; c++) {&nbsp; &nbsp;long columnWidthPx = Math.round(sheet.getColumnWidthInPixels(c));&nbsp; &nbsp;tableHTML.append("<COL width=\"" + columnWidthPx + "\"/>");&nbsp; }&nbsp; tableHTML.append("</COLGROUP>");&nbsp; for (int r = 0; r <= sheet.getLastRowNum(); r++) {&nbsp; &nbsp;row = sheet.getRow(r); if (row == null) row = sheet.createRow(r);&nbsp; &nbsp;long rowHeightPx = Math.round(row.getHeightInPoints() * 92f / 72f);&nbsp; &nbsp;tableHTML.append("<TR height=\"" + rowHeightPx + "\">");&nbsp; &nbsp;int c = 0;&nbsp; &nbsp;while(c < lastColumnToExport) {&nbsp; &nbsp; tableHTML.append("<TD");&nbsp; &nbsp; cell = row.getCell(c); if (cell == null) cell = row.createCell(c);&nbsp; &nbsp; cellValue = dataFormatter.formatCellValue(cell);&nbsp; &nbsp; double cellValueWidth = SheetUtil.getCellWidth(cell, defaultCharWidth, dataFormatter, false);&nbsp; &nbsp; int neededColunmnWidth = (int)cellValueWidth*256;&nbsp; &nbsp; int columnWidth = sheet.getColumnWidth(c);&nbsp; &nbsp; if (columnWidth < neededColunmnWidth) {&nbsp; &nbsp; &nbsp;int colSpan = 1;&nbsp; &nbsp; &nbsp;while(columnWidth < neededColunmnWidth) {&nbsp; &nbsp; &nbsp; colSpan++;&nbsp; &nbsp; &nbsp; c++;&nbsp; &nbsp; &nbsp; columnWidth += sheet.getColumnWidth(c);&nbsp; &nbsp; &nbsp;}&nbsp;&nbsp; &nbsp; &nbsp;tableHTML.append(" colspan=\"" + colSpan + "\""&nbsp; + ">" + cellValue);&nbsp; &nbsp; &nbsp;c++;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp;tableHTML.append(">" + cellValue);&nbsp; &nbsp; &nbsp;c++;&nbsp; &nbsp; }&nbsp; &nbsp; tableHTML.append("</TD>");&nbsp; &nbsp;}&nbsp; &nbsp;tableHTML.append("</TR>");&nbsp; }&nbsp; tableHTML.append("</TABLE>");&nbsp; workbook.close();System.out.println(tableHTML.toString());&nbsp; //creating a sample HTML file&nbsp;&nbsp; String encoding = "UTF-8";&nbsp; FileOutputStream fos = new FileOutputStream("result.html");&nbsp; OutputStreamWriter writer = new OutputStreamWriter(fos, encoding);&nbsp; writer.write("<!DOCTYPE html>\n");&nbsp; writer.write("<html lang=\"en\">");&nbsp; writer.write("<head>");&nbsp; writer.write("<meta charset=\"utf-8\"/>");&nbsp; writer.write("<style>");&nbsp; writer.write("table {border-collapse: collapse; table-layout: fixed;}");&nbsp; writer.write("table, tr, td {border: 1px solid black;}");&nbsp; writer.write("td {font: 11pt Calibri, arial, sans-serif;}");&nbsp; writer.write("</style>");&nbsp; writer.write("</head>");&nbsp; writer.write("<body>");&nbsp; writer.write(tableHTML.toString());&nbsp; writer.write("</body>");&nbsp; writer.write("</html>");&nbsp; writer.close();&nbsp; java.awt.Desktop.getDesktop().browse(new File("result.html").toURI());&nbsp;}}结果:
随时随地看视频慕课网APP

相关分类

Java
我要回答