Excel to HTML Converter with Automatic Column Width Adjustment
import java.awt.; import java.io.; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator;
import org.apache.poi.ss.usermodel.; import org.apache.poi.xssf.usermodel.; import org.apache.poi.hssf.usermodel.; import org.apache.poi.ss.util.; import org.apache.poi.hssf.util.*; import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
public class ExcelToHtmlConverter { public static void main(String[] args) throws Exception { String excelFilePath = 'input.xlsx'; File inputFile = new File(excelFilePath);
//Get the workbook instance for XLSX file
Workbook workbook = null;
if (excelFilePath.endsWith('xlsx')) {
workbook = new XSSFWorkbook(new FileInputStream(inputFile));
} else if (excelFilePath.endsWith('xls')) {
workbook = new HSSFWorkbook(new FileInputStream(inputFile));
}
//Get the first sheet from the workbook
Sheet sheet = workbook.getSheetAt(0);
//Create a new HTML table
StringBuffer htmlTable = new StringBuffer('<table style="border-collapse: collapse;">');
//Get the maximum number of columns used by any row in the sheet
int maxColumns = 0;
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
int lastCellNum = row.getLastCellNum();
if (lastCellNum > maxColumns) {
maxColumns = lastCellNum;
}
}
//Calculate the width of each column based on the maximum number of characters in any cell of that column
int[] columnWidths = new int[maxColumns];
rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
int columnNum = 0;
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
String cellValue = '';
switch (cell.getCellType()) {
case STRING:
cellValue = cell.getStringCellValue();
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd HH:mm:ss');
cellValue = sdf.format(date);
} else {
cellValue = String.valueOf(cell.getNumericCellValue());
}
break;
case BOOLEAN:
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case FORMULA:
cellValue = cell.getCellFormula();
break;
case BLANK:
cellValue = '';
break;
default:
cellValue = cell.toString();
}
int cellLength = cellValue.length();
if (cellLength > columnWidths[columnNum]) {
columnWidths[columnNum] = cellLength;
}
columnNum++;
}
}
//Iterate through each rows from first sheet
rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
//Create a new HTML table row
htmlTable.append('<tr>');
//Iterate through each columns from the row
Iterator<Cell> cellIterator = row.cellIterator();
int columnNum = 0;
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
//Create a new HTML table cell
htmlTable.append('<td style="border: 1px solid #ddd; padding: 8px;');
//Set the width of the cell based on the width of the column
int columnWidth = columnWidths[columnNum] * 10;
htmlTable.append('width: ' + columnWidth + 'px;');
//Close the HTML table cell opening tag
htmlTable.append('">');
//Get the cell value and append to HTML table cell
switch (cell.getCellType()) {
case STRING:
htmlTable.append(cell.getStringCellValue());
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd HH:mm:ss');
String formattedDate = sdf.format(date);
htmlTable.append(formattedDate);
} else {
htmlTable.append(cell.getNumericCellValue());
}
break;
case BOOLEAN:
htmlTable.append(cell.getBooleanCellValue());
break;
case FORMULA:
htmlTable.append(cell.getCellFormula());
break;
case BLANK:
htmlTable.append('');
break;
default:
htmlTable.append(cell);
}
//Close the HTML table cell
htmlTable.append('</td>');
columnNum++;
}
//Close the HTML table row
htmlTable.append('</tr>');
}
//Close the HTML table
htmlTable.append('</table>');
//Close the workbook
workbook.close();
//Create a new HTML file and write the table content
String htmlFilePath = 'output.html';
File htmlFile = new File(htmlFilePath);
BufferedWriter writer = new BufferedWriter(new FileWriter(htmlFile));
writer.write(htmlTable.toString());
writer.close();
//Open the HTML file in default browser
Desktop.getDesktop().browse(htmlFile.toURI());
}
}
原文地址: https://www.cveoy.top/t/topic/f0Xo 著作权归作者所有。请勿转载和采集!