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