本文将介绍如何使用Spire.XLS for Java删除Excel文档中的公式同时保留其计算值。
原Excel文档:

import com.spire.xls.*;
public class RemoveFormulasButKeepValues {
public static void main(String[] args) {
//创建Workbook实例
Workbook workbook = new Workbook();
//加载Excel文档
workbook.loadFromFile("Input.xlsx");
//遍历所有工作表
for (Worksheet sheet : (Iterable) workbook.getWorksheets())
{
//遍历所有单元格
for (CellRange cell : (Iterable) sheet.getRange())
{
//判断单元格是否包含公式
if (cell.hasFormula())
{
//获取公式值
Object value = cell.getFormulaValue();
//清除单元格内容
cell.clear(ExcelClearOptions.ClearContent);
//将公式值填充到单元格中
cell.setValue(value.toString());
}
}
}
//保存结果文档
workbook.saveToFile("RemoveFormulasButKeepValues.xlsx", ExcelVersion.Version2013);
}
}
结果文档:

本教程将介绍如何使用Spire.PDF for Java为已有的PDF文档不同页面添加不同的页眉。
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.*;
public class addDifferentHeaders {
public static void main(String[] args) {
String output = "output/addDifferentHeaders.pdf";
//加载示例文档
PdfDocument doc = new PdfDocument();
doc.loadFromFile("Sample.pdf");
//定义页眉文本
String header1 = "Spire.PDF 示例";
String header2 = "添加PDF页眉";
//设置文本字体、样式、颜色、大小及位置
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("宋体", Font.BOLD,14),true);
PdfBrush brush= PdfBrushes.getBlue();
Rectangle2D rect = new Rectangle2D.Float();
Dimension2D dimension2D = new Dimension();
dimension2D.setSize(doc.getPageSettings().getSize().getWidth(),50f);
rect.setFrame(new Point2D.Float(0, 20), dimension2D);
PdfStringFormat format=new PdfStringFormat();
format.setAlignment(PdfTextAlignment.Center);
//为第一页添加页眉
doc.getPages().get(0).getCanvas().drawString(header1,font,brush,rect,format);
//为第二页添加页眉
format.setAlignment( PdfTextAlignment.Left);
doc.getPages().get(1).getCanvas().drawString(header2, font, brush, rect, format);
//保存文档
doc.saveToFile(output, FileFormat.PDF);
}
}
效果图:

本文介绍使用Spire.Doc for Java批量删除Word文档中的空白段落。
测试文档如下,包含多行空白无内容的段落:

import com.spire.doc.*;
import com.spire.doc.documents.DocumentObjectType;
import com.spire.doc.documents.Paragraph;
public class DeleteBlankParas {
public static void main(String[] args) {
//加载Word测试文档
Document doc = new Document();
doc.loadFromFile("sample.docx");
//遍历Section
for(int i = 0; i < doc.getSections().getCount();i++)
{
//获取section
Section section = doc.getSections().get(i);
//遍历section中的对象
for (int j = 0;j < section.getBody().getChildObjects().getCount();j++)
{
//获取对象类型
Object object = section.getBody().getChildObjects().get(j).getDocumentObjectType();
//遍历段落
for(int z = 0 ; z < section.getParagraphs().getCount();z++)
{
//获取段落
Paragraph paragraph = section.getParagraphs().get(z);
//判断对象类型是否为段落
if(object.equals(DocumentObjectType.Paragraph))
{
//判断段落内容是否为空
if(paragraph.getChildObjects().getLastItem() == null)
{
//删除空白段落
section.getBody().getParagraphs().remove(paragraph);
z--;
}
}
}
}
}
//保存文档
doc.saveToFile("DeleteBlankParas.docx",FileFormat.Docx_2013);
doc.dispose();
}
}
空白段落删除效果:

Spire.Presentation for Java 3.10.2已发布。该版本增强了转换形状到图片以及转换PPT到PDF/图片的功能,此外还修复了加载PPTX文档时出现的问题。详情请阅读以下内容。
问题修复:
https://www.e-iceblue.cn/Downloads/Spire-Presentation-JAVA.html
在 Word 文档中,段落和文本背景颜色是文档设计的一个重要元素。适当的段落和文本背景色可以起到突出显示特定段落或文本、增强文本对比度以方便阅读的作用,同时还能填补版面空白,从而帮助排版。本文将介绍如何使用 Spire.Doc for Java 通过 Java 程序设置段落和文本的背景颜色。
首先,您需要在 Java 程序中添加 Spire.Doc.jar 文件作为依赖项。您可以从此链接下载 JAR 文件;如果您使用 Maven,则可以通过在 pom.xml 文件中添加以下代码导入 JAR 文件。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>14.9.5</version>
</dependency>
</dependencies>
在设置段落的背景颜色时,需要先获取指定段落,然后使用 Paragraph.getFormat().setBackColor() 方法设置其背景颜色。具体操作步骤如下:
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import java.awt.*;
public class setParagraphBackgroundColor {
public static void main(String[] args) {
//创建一个Document类的对象
Document document = new Document();
//载入Word文档
document.loadFromFile("示例.docx");
//获取文档第一节
Section section = document.getSections().get(0);
//获取该节第四个段落
Paragraph paragraph = section.getParagraphs().get(3);
//将此段落的背景颜色设置为浅灰色
paragraph.getFormat().setBackColor(Color.LIGHT_GRAY);
//保存文档
document.saveToFile("段落背景色.docx", FileFormat.Docx_2013);
document.dispose();
}
}
Spire.Doc for Java 提供了 Document.findAllString() 方法用于查找 Word 文档中出现的所有特定文本,以及 TextRange.getCharacterFormat().setTextBackgroundColor() 方法用于设置特定文本的背景颜色。为现有文本设置背景色的详细操作步骤如下。
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class setTextBackgroundColor {
public static void main(String[] args) {
//创建Document类的对象
Document document = new Document();
//载入Word文档
document.loadFromFile("示例.docx");
//找到要设置背景色的文本
TextSelection[] textSelections = document.findAllString("糖分摄入", false, true);
//循环遍历查找结果
for (TextSelection selection : textSelections){
//获取一个结果为文本范围
TextRange textRange = selection.getAsOneRange();
//设置该查找结果的背景色
textRange.getCharacterFormat().setTextBackgroundColor(Color.CYAN);
}
//为第一个查找结果设置背景色
//TextRange textRange = textSelections[0].getAsOneRange();
//textRange.getCharacterFormat().setTextBackgroundColor(Color.CYAN);
//保存文档
document.saveToFile("文本背景色.docx", FileFormat.Docx_2013);
document.dispose();
}
}
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
在处理 Excel 数据时,微软 Excel 中的 “文本分列(Text to Columns)” 功能是一个非常实用的工具,它可以将单元格中的文本按指定规则拆分到多个列中。当导入的数据格式不够规范时,这一功能尤其有用。而对于 Java 开发者来说,如果能通过编程方式实现这一操作,将大大提升 Excel 数据处理工作的效率。
本文将带你学习如何使在 Java 中将文本拆分为多列,实现自动化处理。
Spire.XLS for Java 是一个实用的第三方组件,可完成在 Java 中读取、写入以及转换 Excel 的 XLS 或 XLSX 文件等工作。要开始使用该库,你需要先添加相应的依赖。目前常见的导入方式有两种: 方法一:通过 Maven 安装 如果你使用 Maven 构建项目,只需在项目的 pom.xml 文件中添加以下内容即可:
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls</artifactId>
<version>16.9.2</version>
</dependency>
</dependencies>
方法二:手动安装
加载 Excel 文件并访问工作表 使用 Workbook.loadFromFile() 方法加载输入的 Excel 文件,然后访问其中的指定工作表。
访问单元格并获取数据 遍历工作表中的每一行,定位到目标单元格,通过 CellRange.getText() 方法获取该单元格中的文本内容。
拆分 Excel 单元格中的文本 使用 Java 的 String.split(String regex) 方法,按照指定的分隔符(如 ",")对文本进行拆分。
将拆分后的文本写入多个列中 遍历拆分出的数据,并将其逐个写入到对应的列中,实现“文本分列”的效果。
保存修改后的 Excel 文件 使用 Workbook.saveToFile() 方法,将更新后的 Excel 文件保存到指定位置。
下方的代码展示了怎样将 Excel 中的文本拆分,并保存到同一个工作表中:
import com.spire.xls.*;
public class splitDataIntoMultipleColumns {
public static void main(String[] args) {
// 创建 Workbook 对象
Workbook workbook = new Workbook();
// 加载 Excel 文件
workbook.loadFromFile("/成绩.xlsx");
// 获取第一个工作表
Worksheet sheet = workbook.getWorksheets().get(0);
// Iterate through each row in the worksheet 遍历工作表中的每一行
for (int i = 0; i < sheet.getLastRow(); i++)
{
// 获取当前行第一个单元格的文本
String text = sheet.getRange().get(i + 1, 1).getText();
// 将文本按照逗号进行拆分
String[] splitText = text.split(",");
// Iterate through each split data 遍历拆分后的数据
for (int j = 0; j < splitText.length; j++)
{
// 将拆分后的数据写入不同的列
sheet.getRange().get(i + 1, j + 3).setText(splitText[j]);
}
}
// 设置自适应列宽
sheet.getAllocatedRange().autoFitColumns();
// 保存结果文档
workbook.saveToFile("/拆分Excel数据.xlsx", ExcelVersion.Version2016);
}
}

温馨提示:代码中的分隔符必须与源文件中的实际字符一致,尤其要注意区分英文逗号(,)和中文逗号(,)。
将拆分后的数据保存到同一个工作表中,更适合轻量级处理和快速查看的场景。而如果你希望更稳妥地处理数据,并避免覆盖原始内容,那么将拆分结果写入一个新的工作表会是更合适的选择。 这一方案的整体逻辑与上一章节中介绍的保存到同一工作表的方式非常相似。你可以参考它,了解拆分流程的具体执行顺序。
在实现上,主要的区别在于:
import com.spire.xls.*;
public class splitDataIntoMultipleColumns {
public static void main(String[] args) {
// 创建 Workbook 对象
Workbook workbook = new Workbook();
// 加载 Excel 文件(输入路径)
workbook.loadFromFile("/成绩.xlsx");
// 获取第一个工作表(原始数据)
Worksheet sourceSheet = workbook.getWorksheets().get(0);
// 新增一个工作表,用于存放拆分结果
Worksheet resultSheet = workbook.getWorksheets().add("拆分结果");
// 遍历原始工作表的每一行
for (int i = 0; i < sourceSheet.getLastRow(); i++) {
// 获取当前行第 1 列的文本(A 列)
String text = sourceSheet.getRange().get(i + 1, 1).getText();
// 按逗号拆分文本
String[] splitText = text.split(",");
// 将拆分后的内容写入新工作表的同一行,不同列
for (int j = 0; j < splitText.length; j++) {
resultSheet.getRange().get(i + 1, j + 1).setText(splitText[j]);
}
}
// 自动调整新工作表的列宽
resultSheet.getAllocatedRange().autoFitColumns();
// 保存到指定文件(输出路径)
workbook.saveToFile("/拆分Excel数据新表.xlsx", ExcelVersion.Version2016);
}
}

在拆分文本时,除了常见的逗号,还可能遇到空格、分号或制表符等其他分隔符。你只需根据实际需求,调整 split() 方法中的正则表达式即可实现不同分隔符的拆分。
例如:
在 Spire.XLS for Java 的帮助下,使用 Java 实现 Excel 文本分列操作既高效又简便,能够提升工作效率并保证数据一致性。该方法支持多种分隔符,适用于处理用户输入、日志和报表等多种场景。你还可以申请免费30天试用许可,全面体验 Spire.XLS for Java 的强大功能,无任何评估限制。
在之前的文章中我们介绍了如何给PowerPoint文档中的形状添加动画效果,在这篇文章中我们将介绍如何给PowerPoint文档中的指定段落添加动画效果。
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import com.spire.presentation.drawing.animation.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class AddAnimationOnParagraph {
public static void main(String[] args) throws Exception {
//创建Presentation实例
Presentation ppt = new Presentation();
//获取第一张幻灯片
ISlide slide = ppt.getSlides().get(0);
//添加一个形状到幻灯片
IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Double(150, 150, 450, 100));
shape.getFill().setFillType(FillFormatType.SOLID);
shape.getFill().getSolidColor().setColor(Color.gray);
shape.getShapeStyle().getLineColor().setColor(Color.white);
shape.appendTextFrame("This demo shows how to apply animation on paragraph in PPT document.");
//给形状中的第一个段落添加动画效果
AnimationEffect animation = shape.getSlide().getTimeline().getMainSequence().addEffect(shape, AnimationEffectType.FLOAT);
animation.setStartEndParagraphs(0, 0);
//保存结果文档
ppt.saveToFile("AddAnimationOnPara.pptx", FileFormat.PPTX_2013);
ppt.dispose();
}
}
效果图:

Spire.PDF 6.10.9已发布。该版本增强了增强了PDF 转换到 PDF/A1A, PDF/A1B, PDF/A3A 以及图片的功能,并且修复了提取图形和字体出现的问题。详情请阅读以下内容。
问题修复:
本文介绍如何使用Spire.Presentation for Java来编辑PPT幻灯片中已有的SmartArt图形,包括重置图形样式、颜色、添加/删除图形节点、编辑节点内容、添加超链接到节点(链接到网页、链接到指定幻灯片)等。
import com.spire.presentation.*;
import com.spire.presentation.diagrams.*;
public class ModifySmartArt {
public static void main(String[] args) throws Exception {
//加载PPT文档
Presentation ppt = new Presentation();
ppt.loadFromFile("sample.pptx");
//获取SmartArt图形
ISmartArt smartart = (ISmartArt) ppt.getSlides().get(0).getShapes().get(0);
//重置图形样式及颜色
smartart.setStyle(SmartArtStyleType.SUBTLE_EFFECT);
smartart.setColorStyle(SmartArtColorType.COLORFUL_ACCENT_COLORS_2_TO_3);
//获取SmartArt图形的节点集合
ISmartArtNodeCollection nodes = smartart.getNodes();
nodes.get(1).getTextFrame().setText("新修改的节点内容");//更改节点内容
nodes.get(1).getTextFrame().setAutofitType(TextAutofitType.SHAPE);//设置节点形状的文本自适应类型
//添加超链接到节点
nodes.get(2).setClick(new ClickHyperlink("https://www.baidu.com/"));//添加指向网页的超链接
nodes.get(3).setClick(new ClickHyperlink(ppt.getSlides().get(1)));//添加指向指定幻灯片的超链接
//添加节点
ISmartArtNode newnode = nodes.get(5).getChildNodes().addNode();
newnode.getTextFrame().setText("新添加的节点内容");
//nodes[0].ChildNodes[3].ChildNodes.RemoveNodeByPosition(0);//删除节点
//保存文档
ppt.saveToFile("output.pptx", FileFormat.PPTX_2013);
ppt.dispose();
}
}
测试文档:

编辑结果如下,添加的超链接效果需要在幻灯片播放中查看:

Spire.Doc 8.10.4已发布。该版本支持转换SmartArt到其他文档格式,比如PDF/Image/XPS,新增了内容控件设置“内容不能被编辑”的功能,并且增强了Word转换到PDF的功能。详情请阅读以下内容。
新功能:
StructureDocumentTagInline sdt = new StructureDocumentTagInline(document);
sdt.SDTProperties.LockSettings = LockSettingsType.ContentLocked;
问题修复: