本文介绍如何根据现有Excel数据在PowerPoint中创建图表。该方案需引用Spire.Office.jar,请下载最新版本并在您的项目中添加为依赖。
Excel文档截图:

import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideSizeType;
import com.spire.presentation.charts.ChartStyle;
import com.spire.presentation.charts.ChartType;
import com.spire.presentation.charts.IChart;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import java.awt.geom.Rectangle2D;
public class CreateChartFromExcelData {
public static void main(String[] args) throws Exception {
//创建Presentation对象
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
//添加柱状图
Rectangle2D rect = new Rectangle2D.Float(200, 100, 550, 320);
IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.COLUMN_CLUSTERED,rect);
//清除默认图表数据
chart.getChartData().clear(0,0,5,5 );
//创建Workbook对象并加载Excel文档
Workbook wb = new Workbook();
wb.loadFromFile("C:\\Users\\Administrator\\Desktop\\data.xlsx");
//获取第一个工作表
Worksheet sheet = wb.getWorksheets().get(0);
//将Excel中的数据导入图表数据表
for (int r = 0; r < sheet.getAllocatedRange().getRowCount(); r++)
{
for (int c = 0; c < sheet.getAllocatedRange().getColumnCount(); c++)
{
chart.getChartData().get(r,c).setValue(sheet.getCellRange(r+1, c+1).getValue2());
}
}
//添加标题
chart.getChartTitle().getTextProperties().setText("男性女性成员分布");
chart.getChartTitle().getTextProperties().isCentered(true);
chart.getChartTitle().setHeight(25f);
chart.hasTitle(true);
//设置系列标签
chart.getSeries().setSeriesLabel(chart.getChartData().get("B1","C1"));
//设置分类标签
chart.getCategories().setCategoryLabels(chart.getChartData().get("A2","A5"));
//设置系列数据
chart.getSeries().get(0).setValues(chart.getChartData().get("B2","B5"));
chart.getSeries().get(1).setValues(chart.getChartData().get("C2", "C5"));
//应用内置样式
chart.setChartStyle(ChartStyle.STYLE_11);
//设置系列重叠
chart.setOverLap(-50);
//设置分类间距
chart.setGapWidth(200);
//保存文档
presentation.saveToFile("output/Chart-CN.pptx", FileFormat.PPTX_2013);
}
}

XML(EXtensible Markup Language) 指可扩展标记语言,用以传输和存储数据。使用 Spire.XLS for Java 可实现将 XML 文件转为 Excel 格式,如 .xlsx / .xls。本文,将通过 Java 应用程序介绍如何来实现转换,下面是详细步骤及方法。
首先,您需要在 Java 程序中添加 Spire.XLS for Java 文件作为依赖项。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.xls</artifactId>
<version>15.11.3</version>
</dependency>
</dependencies>
下面是实现转换的代码步骤:
import com.spire.xls.*;
public class xmltoexcel {
public static void main(String[] args) {
//创建Workbook类的对象
Workbook wb = new Workbook();
//加载XML文档
wb.loadFromXml("test.xml");
//转为xlsx格式的Excel
wb.saveToFile("xmltoExcel.xlsx",FileFormat.Version2013);
//转为xls格式的Excel
wb.saveToFile("xmltoExcel.xls",FileFormat.Version97to2003);
}
}转换结果:

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。 获取有效期 30 天的临时许可证。
Spire.Doc 9.4.1已正式发布。该版本主要修复了将Word转为PDF时出现的问题。详情如下。
问题修复:
Spire.Presentation 6.4.1已发布。该版本增强了PPTX到PDF/图片的功能,并且修复了修改SmartArt背景色不生效的问题。详情请阅读以下内容。
问题修复:
https://www.e-iceblue.cn/Downloads/Spire-Presentation-NET.html
前面我们介绍了如何 查找和高亮word文档中的文本。该文将介绍如何在Java应用程序中使用正则表达式查找并高亮、查找和替换Word 文档中的文本。
请查看示例文档:

以下示例展示如何使用document.replace() 方法替换所有查找到的以 # 开头的文本。
import com.spire.doc.*;
import java.util.regex.Pattern;
public class WordDemo {
public static void main(String[] args) throws Exception {
//加载示例文档
Document document = new Document();
document.loadFromFile("https://cdn.e-iceblue.cn/Sample.docx");
//匹配以#开头,数字结尾的字符并用Spire.Doc替换
Pattern c = Pattern.compile ("^#(.*?)\\d$");
document.replace(c,"Spire.Doc");
//保存文档
document.saveToFile("Result.docx", FileFormat.Docx_2013);
}
}
效果图:

以下示例展示如何使用document.findAllPattern()方法查找【】内的文本/跨段落文本并高亮显示。
import com.spire.doc.*;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.TextRange;
import java.awt.*;
import java.util.regex.Pattern;
public class WordDemo {
public static void main(String[] args) throws Exception {
//加载示例文档
Document document = new Document();
document.loadFromFile("https://cdn.e-iceblue.cn/Sample.docx");
Pattern c = Pattern.compile("【[\\s\\S]*】");
//匹配【】内的字符
TextSelection[] textSelections = document.findAllPattern(c, true); //true表示高级查找
//设置高亮颜色
for (TextSelection selection : textSelections) {
TextRange[] results = selection.getAsRange();
for (TextRange result : results) {
result.getCharacterFormat().setHighlightColor(Color.yellow);
}
}
document.saveToFile("Result2.docx", FileFormat.Docx_2013);
}
}

Spire.PDF for Java 4.3.4已发布。本次更新主要修复了转换PDF到Word时,内容重叠的问题。详情请阅读以下内容。
问题修复:
前面介绍了如何使用Spire.Doc for .NET实现 Word查找、替换和高亮显示功能。该文将介绍如何使用正则表达式查找和替换Word 文档中的文本。
请查看示例文档:

using Spire.Doc;
using System.Text.RegularExpressions;
using System.Drawing;
namespace WordDemo
{
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
doc.LoadFromFile("https://cdn.e-iceblue.cn/Sample.docx");
//替换以#开头的字符
Regex regex = new Regex(@"\#\w+\b");
doc.Replace(regex, "Spire.Doc");
//替换[]内的字符
Regex regex1 = new Regex(@"[[\s\S]*]");
doc.Replace(regex1, "Spire.Doc for .NET");
//保存文档
doc.SaveToFile("Result.docx", FileFormat.Docx2013);
}
}
}
Imports Spire.Doc
Imports System.Text.RegularExpressions
Imports System.Drawing
Namespace WordDemo
Class Program
Private Shared Sub Main(ByVal args() As String)
Dim doc As Document = New Document
doc.LoadFromFile("https://cdn.e-iceblue.cn/Sample.docx")
'替换以#开头的字符
Dim regex As Regex = New Regex("\#\w+\b")
doc.Replace(regex, "Spire.Doc")
'替换[]内的字符
Dim regex1 As Regex = New Regex("[[\s\S]*]")
doc.Replace(regex1, "Spire.Doc for .NET")
'保存文档
doc.SaveToFile("Result.docx", FileFormat.Docx2013)
End Sub
End Class
End Namespace
效果图:

本文介绍使用Spire.XLS for Java给Excel添加数据条类型的条件格式的方法。
import com.spire.xls.*;
import java.awt.*;
public class databar {
public static void main(String[] args) {
//加载Excel工作簿
Workbook wb = new Workbook();
wb.loadFromFile("sample.xlsx");
//获取第二个工作表
Worksheet sheet2 = wb.getWorksheets().get(1);
//获取应用条件格式的数据源范围
CellRange range = sheet2.getCellRange("B2:D7");
//添加条件格式类型为data bars(数据条)
ConditionalFormatWrapper format = range.getConditionalFormats().addCondition();
format.setFormatType(ConditionalFormatType.DataBar);
format.getDataBar().setBarColor(new Color(39,201,102));
format.getDataBar().setShowValue(true);
//保存文档
wb.saveToFile("databar.xlsx", ExcelVersion.Version2013);
wb.dispose();
}
}
添加效果:

数字签名是具有加密信息的电子签名,有助于验证消息、软件和数字文档的真实性。它们通常用于软件分发、金融交易、合同管理软件以及其他需要伪造或篡改检测的情况。在生成 Excel 报告时,您可能需要添加数字签名以使其看起来更加正式和更具真实性。 在本文中,您将学习如何使用 Spire.XLS for Java 在 Excel 中添加或删除数字签名。
首先,您需要在 Java 程序中添加 Spire.Xls.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.xls</artifactId>
<version>15.11.3</version>
</dependency>
</dependencies>
您可以添加数字签名来保护 Excel 文件的完整性。添加数字签名后,文件将变为只读,以阻止进一步编辑。如果有人对文件进行更改,数字签名将立即失效。
Spire.XLS for Java 提供了 Workbook 类的 addDigitalSignature 方法来向 Excel 文件添加数字签名。详细步骤如下:
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.digital.CertificateAndPrivateKey;
import java.util.Date;
public class AddDigitalSignature {
public static void main(String []args) throws Exception {
//创建一个 Workbook 实例
Workbook workbook=new Workbook();
//加载 Excel 文件
workbook.loadFromFile("Test.xlsx");
//向文件添加数字签名
CertificateAndPrivateKey cap = new CertificateAndPrivateKey("Gary.pfx","e-iceblue");
workbook.addDigitalSignature(cap, "e-iceblue",new Date());
//保存结果文件
workbook.saveToFile("添加数字签名.xlsx", ExcelVersion.Version2013);
}
}
Spire.XLS for Java 提供了 Workbook 类的 removeAllDigitalSignatures 方法,供开发人员从 Excel 文件中删除数字签名。 详细步骤如下:
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
public class DeleteDigitalSignature {
public static void main(String []args) throws Exception {
//创建一个 Workbook 实例
Workbook workbook = new Workbook();
//加载 Excel 文件
workbook.loadFromFile("添加数字签名.xlsx");
//从文件中删除所有数字签名
workbook.removeAllDigitalSignatures();
//保存结果文件
workbook.saveToFile("删除数字签名.xlsx", ExcelVersion.Version2013);
}
}
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
本文介绍如何使用Spire.Presentation for Java设置幻灯片中的表格的对齐方式。
import com.spire.presentation.FileFormat;
import com.spire.presentation.ITable;
import com.spire.presentation.Presentation;
import com.spire.presentation.ShapeAlignmentEnum;
public class AlignTable {
public static void main(String[] args) throws Exception {
//创建Presentation对象
Presentation presentation = new Presentation();
//加载示例文档
presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pptx");
//声明ITable变量
ITable table = null;
//遍历第一个幻灯片中的图形
for (Object shape: presentation.getSlides().get(0).getShapes()
) {
//判断图形是否为ITable对象
if (shape instanceof ITable)
{
//将图形转换为ITable对象
table =(ITable)shape;
}
}
//将表格水平居中
table.setShapeAlignment(ShapeAlignmentEnum.ShapeAlignment.AlignCenter);
//将表格垂直居中
table.setShapeAlignment(ShapeAlignmentEnum.ShapeAlignment.AlignMiddle);
//保存文档
presentation.saveToFile("AlignTable.pptx", FileFormat.PPTX_2013);
}
}
