java使用poi把excel转Html在线预览其中excel里面有图片
时间: 2023-08-11 18:04:20 浏览: 84
JAVA POI Excel转Html
要在Java中使用POI将Excel转换为HTML并在其中包含图片,可以使用以下代码:
```java
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFPictureData;
import java.io.*;
import java.util.Base64;
public class ExcelToHtmlConverter {
public static void main(String[] args) {
try {
FileInputStream fileInputStream = new FileInputStream("input.xlsx");
Workbook workbook = new XSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
FileOutputStream fileOutputStream = new FileOutputStream("output.html");
PrintWriter printWriter = new PrintWriter(fileOutputStream);
printWriter.println("<!DOCTYPE html>");
printWriter.println("<html>");
printWriter.println("<head>");
printWriter.println("<title>Excel to HTML</title>");
printWriter.println("</head>");
printWriter.println("<body>");
for (Row row : sheet) {
printWriter.println("<tr>");
for (Cell cell : row) {
printWriter.print("<td>");
CellType cellType = cell.getCellType();
if (cellType == CellType.STRING) {
printWriter.print(cell.getStringCellValue());
} else if (cellType == CellType.NUMERIC) {
printWriter.print(cell.getNumericCellValue());
} else if (cellType == CellType.BOOLEAN) {
printWriter.print(cell.getBooleanCellValue());
}
if (cell instanceof XSSFSheet) {
XSSFSheet xssfSheet = (XSSFSheet) sheet;
for (POIXMLDocumentPart part : xssfSheet.getRelations()) {
if (part instanceof XSSFPictureData) {
XSSFPictureData pictureData = (XSSFPictureData) part;
String base64Image = Base64.getEncoder().encodeToString(p
阅读全文