我们正在尝试从 Excel 单元格获取 html 格式(基本上是 html 格式的单元格文本),包括项目符号、斜体、换行符、突出显示、超链接等。
我们正在使用 Apache POI,但它不提供这些确切的功能,也尝试过 Aspose cells,但每次都会抛出“无法找到 com.aspose:aspose-cells:24.4”错误,即使在添加正确的存储库 URL 后也如下所示:
repositories {
maven {
url "https://releases.aspose.com/java/repo/"
}
mavenCentral()
jcenter()
}
有人可以建议我们如何实现 Excel 单元格中以下文本的此要求吗:
也尝试过 Aspose 单元格,但抛出“找不到 com.aspose:aspose-cells:24.4" 即使添加后每次都会出错 正确的存储库 URL 也如下所示
我在 eclipse 中使用示例 Gradle 项目进行了测试,其中目标 Java 版本为 21。它运行良好,并且 Aspose.Cells for Java 24.4 和 Aspose.Slides for Java 24.4 库都添加到 eclipse 中的“项目和外部依赖项”节点。这里是项目中的gradle.build文件内容,供大家参考。
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java application project to get you started.
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.7/userguide/building_java_projects.html in the Gradle documentation.
*/
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
// Aspose repository
maven {
url "https://releases.aspose.com/java/repo/"
}
}
dependencies {
// Use JUnit test framework.
testImplementation libs.junit
// This dependency is used by the application.
implementation libs.guava
implementation("com.aspose:aspose-cells:24.4")
implementation("com.aspose:aspose-slides:24.4:jdk16")
}
// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
application {
// Define the main class for the application.
mainClass = 'org.example.App'
}
现在开始你的任务“如何从 Java 中的 Excel 工作表单元格获取 html 格式”,有两种方法可以完成你的任务。
我在应用程序文件夹中放置了一个示例文件“SampleExcelFile.xlsx”。现在,我在主 (Java) 类中编写以下示例代码来检索工作表中单元格的 HTML。我还将工作簿保存为 HTML 文件。
例如,
示例代码:
/*
* This source file was generated by the Gradle 'init' task
*/
package org.example;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
import com.aspose.cells.Cells;
import com.aspose.cells.Cell;
import com.aspose.cells.SaveFormat;
import java.nio.file.ClosedFileSystemException;
public class App {
public static void main(String[] args) throws Exception {
// Open the Excel file
Workbook workbook = new Workbook("SampleExcelFile.xlsx");
// Get the first worksheet in the workbook
Worksheet worksheet = workbook.getWorksheets().get(0);
// Access cell A1
Cells cells = worksheet.getCells();
Cell cell = cells.get("A1");
// Get HTML formatting string of the cell
String html = cell.getHtmlString();
// Save to HTML file
workbook.save("out1.html", SaveFormat.HTML);
}
}
希望这会有所帮助。