超链接是 Excel 中一个强有力的工具,它允许用户连接 Excel 文件的不同部分或链接到外部资源,例如网站、电子邮件地址或其他文件。它们为用户提供了一种在工作表内或不同工作表之间导航的快捷方式。除了便于导航之外,超链接还可以提供与文件中的数据相关的附加上下文或资源。例如,您可以链接到一个网站,该网站提供了有关工作表中所列出的特定产品的更多信息,可以帮助读者可以更深入地了解该产品。在本文中,我们将探讨如何向 Excel 添加超链接,特别是如何使用 Spire.XLS for C++ 在 C++ 中向 Excel 文件添加文本超链接和图像超链接。
有两种方法可以将 Spire.XLS for C++ 集成到您的应用程序中。一种方法是通过 NuGet 安装它,另一种方法是从我们的网站下载包并将库复制到您的程序中。通过 NuGet 安装更简单,更推荐使用。您可以通过访问以下链接找到更多详细信息。
如何将 Spire.XLS for C++ 集成到 C++ 程序中
Excel 中的文本超链接是一个单词或短语,您可以单击它跳到特定位置,例如 Excel 文件的不同部分、电子邮件地址、网页或外部文件。以下步骤说明如何使用 Spire.XLS for C++ 将文本超链接添加到 Excel 文件:
#include "Spire.Xls.o.h";
using namespace Spire::Xls;
int main()
{
//初始化Workbook类的实例
intrusive_ptr<Workbook> workbook = new Workbook();
//获取特定的工作表
intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Get(0));
//添加链接到网页的文本超链接
intrusive_ptr<CellRange> cell1 = dynamic_pointer_cast<CellRange>(sheet->GetRange(L"B3"));
intrusive_ptr<HyperLink> urlLink = sheet->GetHyperLinks()->Add(cell1);
urlLink->SetType(HyperLinkType::Url);
urlLink->SetTextToDisplay(L"链接到网站");
urlLink->SetAddress(L"https://www.e-iceblue.cn/");
//添加链接到电子邮件地址的文本超链接
intrusive_ptr<CellRange> cell2 = dynamic_pointer_cast<CellRange>(sheet->GetRange(L"E3"));
intrusive_ptr<HyperLink> mailLink = sheet->GetHyperLinks()->Add(cell2);
mailLink->SetType(HyperLinkType::Url);
mailLink->SetTextToDisplay(L"链接到邮件地址");
mailLink->SetAddress(L"mailto:support @e-iceblue.com");
//添加链接到外部文件的文本超链接
intrusive_ptr<CellRange> cell3 = dynamic_pointer_cast<CellRange>(sheet->GetRange(L"B7"));
intrusive_ptr<HyperLink> fileLink = sheet->GetHyperLinks()->Add(cell3);
fileLink->SetType(HyperLinkType::File);
fileLink->SetTextToDisplay(L"链接到外部文件");
fileLink->SetAddress(L"C:\\Users\\Administrator\\Desktop\\2022年度报告.xlsx");
//添加链接到工作簿文档指定工作表单元格的文本超链接
intrusive_ptr<CellRange> cell4 = dynamic_pointer_cast<CellRange>(sheet->GetRange(L"E7"));
intrusive_ptr<HyperLink> sheetLink = sheet->GetHyperLinks()->Add(cell4);
sheetLink->SetType(HyperLinkType::Workbook);
sheetLink->SetTextToDisplay(L"链接到工作簿文档的指定工作表单元格");
sheetLink->SetAddress(L"Sheet2!B5");
//添加链接到UNC地址的文本超链接
intrusive_ptr<CellRange> cell5 = dynamic_pointer_cast<CellRange>(sheet->GetRange(L"B11"));
intrusive_ptr<HyperLink> uncLink = sheet->GetHyperLinks()->Add(cell5);
uncLink->SetType(HyperLinkType::Unc);
uncLink->SetTextToDisplay(L"链接到UNC地址");
uncLink->SetAddress(L"\\192.168.0.121");
//自动调整列宽
sheet->AutoFitColumn(2);
sheet->AutoFitColumn(5);
//保存结果文件
workbook->SaveToFile(L"添加文本超链接.xlsx", ExcelVersion::Version2013);
workbook->Dispose();
}
与文本超链接类似,图像超链接是可以让您单击以导航到特定位置的图像。以下步骤说明了如何使用 Spire.XLS for C++ 将图像超链接添加到 Excel 文件:
#include "Spire.Xls.o.h";
using namespace Spire::Xls;
int main()
{
//初始化Workbook类的实例
intrusive_ptr<Workbook> workbook = new Workbook();
//获取特定的工作表
intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Get(0));
//将文本添加到特定单元格
sheet->GetRange(L"A1")->SetText(L"图像超链接");
//将图像添加到特定单元格
intrusive_ptr<ExcelPicture> picture = ExcelPicture::Dynamic_cast<ExcelPicture>(sheet->GetPictures()->Add(2, 1, L"Logo.png"));
//设置图像宽度和高度
picture->SetWidth(100);
picture->SetHeight(100);
//向图像添加超链接
picture->SetHyperLink(L"https://www.e-iceblue.cn/", true);
//设置列宽
sheet->GetColumns()->GetItem(0)->SetColumnWidth(13);
//保存结果文件
workbook->SaveToFile(L"添加图像超链接.xlsx", ExcelVersion::Version2013);
workbook->Dispose();
}
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
Spire.XLS 13.5.6 已发布。该版本新增实现chartsheet和worksheet之间可以互相移动位置的方法,并且新增了Worksheet.DeleteRows(int[] rows)方法来优化删除行的速度。同时,该版本还增强了 Excel 到 HTML 和图片的转换。许多已知问题也在该版本中被成功修复,如:修复了添加文本框时设置置于顶层功能不生效的问题。详情请阅读以下内容。
新功能:
workbook.Chartsheets[int index1].MoveSheet(int index2);
workbook.Chartsheets[int index1].MoveChartsheet(int index2)问题修复:
Spire.Doc 11.5.12 已发布。改版本增强了 Word 到 PDF 和 HTML 的转换。同时,该版本还修复了许多已知问题,如加载文档时抛异常"System.InvalidCastException"的问题。详情请阅读以下内容。
问题修复:
如果你想让段落更容易导航和阅读,将它们按字母、数字甚至项目符号顺序重新排列,读者可以快速找到他们想要的内容,并立即在列表中搜索。在本文中,您将学习如何使用 Spire.Doc for C++ 在 Word 文档中创建编号列表、项目符号列表和多级列表。
有两种方法可以将 Spire.Doc for C++ 集成到您的应用程序中。一种方法是通过 NuGet 安装它,另一种方法是从我们的网站下载包并将库复制到您的程序中。通过 NuGet 安装更简单,更推荐使用。您可以通过访问以下链接找到更多详细信息。
如何将 Spire.Doc for C++ 集成到 C++ 程序中
Spire.Doc for C++ 提供了 ListStyle 类,您可以使用该类创建编号列表样式或项目符号样式。然后,可以使用 Paragraph->GetListFormat()->ApplyStyle() 方法将列表样式应用于段落。创建编号列表的步骤如下。
int main() {
//创建一个Document对象
intrusive_ptr<Document> document = new Document();
//添加一个节
intrusive_ptr<Section> section = document->AddSection();
//创建编号列表样式
intrusive_ptr<ListStyle> listStyle = document->GetStyles()->Add(ListType::Numbered, L"numberList");
listStyle->GetListRef()->GetLevels()->GetItem(0)->SetPatternType(ListPatternType::DecimalEnclosedFullstop);
listStyle->GetListRef()->GetLevels()->GetItem(0)->SetTextPosition(20);
document->GetStyles()->Add(listStyle);
//添加一个段落
intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
paragraph->AppendText(L"必需的Web开发技能:");
paragraph->GetFormat()->SetAfterSpacing(5);
//添加段落并对其应用编号列表样式
paragraph = section->AddParagraph();
paragraph->AppendText(L"HTML");
paragraph->GetListFormat()->ApplyStyle(listStyle);
paragraph->GetListFormat()->SetListLevelNumber(0);
//再添加四个段落,并将编号列表样式应用于特定段落
paragraph = section->AddParagraph();
paragraph->AppendText(L"CSS");
paragraph->GetListFormat()->ApplyStyle(listStyle);
paragraph->GetListFormat()->SetListLevelNumber(0);
paragraph = section->AddParagraph();
paragraph->AppendText(L"C++Script");
paragraph->GetListFormat()->ApplyStyle(listStyle);
paragraph->GetListFormat()->SetListLevelNumber(0);
paragraph = section->AddParagraph();
paragraph->AppendText(L"Python");
paragraph->GetListFormat()->ApplyStyle(listStyle);
paragraph->GetListFormat()->SetListLevelNumber(0);
paragraph = section->AddParagraph();
paragraph->AppendText(L"MySQL");
paragraph->GetListFormat()->ApplyStyle(listStyle);
paragraph->GetListFormat()->SetListLevelNumber(0);
//将文档保存为Word文件
document->SaveToFile(L"编号列表2.docx", FileFormat::Docx2019);
document->Dispose();
}
创建项目符号列表的过程与创建编号列表的过程类似。不同之处在于,创建列表样式时,必须将列表类型指定为“项目符号”,并为其设置项目符号。以下是详细步骤。
int main() {
//创建一个Document对象
intrusive_ptr<Document> document = new Document();
//添加一个节
intrusive_ptr<Section> section = document->AddSection();
//创建项目符号列表样式
intrusive_ptr<ListStyle> listStyle = document->GetStyles()->Add(ListType::Bulleted, L"bulletedList");
listStyle->GetListRef()->GetLevels()->GetItem(0)->SetBulletCharacter(L"\u00B7");
listStyle->GetListRef()->GetLevels()->GetItem(0)->GetCharacterFormat()->SetFontName(L"Symbol");
listStyle->GetListRef()->GetLevels()->GetItem(0)->SetTextPosition(20);
document->GetStyles()->Add(listStyle);
//添加一个段落
intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
paragraph->AppendText(L"计算机科学科目:");
paragraph->GetFormat()->SetAfterSpacing(5);
//添加段落并对其应用项目符号列表样式
paragraph = section->AddParagraph();
paragraph->AppendText(L"数据结构");
paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
paragraph->GetListFormat()->SetListLevelNumber(0);
//再添加五个段落,并将项目符号列表样式应用于特定段落
paragraph = section->AddParagraph();
paragraph->AppendText(L"演算法");
paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
paragraph->GetListFormat()->SetListLevelNumber(0);
paragraph = section->AddParagraph();
paragraph->AppendText(L"计算机网络");
paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
paragraph->GetListFormat()->SetListLevelNumber(0);
paragraph = section->AddParagraph();
paragraph->AppendText(L"操作系统");
paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
paragraph->GetListFormat()->SetListLevelNumber(0);
paragraph = section->AddParagraph();
paragraph->AppendText(L"C语言程序设计");
paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
paragraph->GetListFormat()->SetListLevelNumber(0);
paragraph = section->AddParagraph();
paragraph->AppendText(L"计算理论");
paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
paragraph->GetListFormat()->SetListLevelNumber(0);
//保存结果文档
document->SaveToFile(L"项目符号列表.docx", FileFormat::Docx2019);
document->Dispose();
}
多级列表至少由两个不同的级别组成。嵌套列表的每个级别都可以使用 ListStyle ->GetListRef()->GetLevels()->GetItem(index) 方法进行访问。通过 ListLevel 对象,您可以设置某个级别的编号类型和前缀。以下是在 Word 中创建多级编号列表的步骤。
int main() {
//创建一个Document对象
intrusive_ptr<Document> document = new Document();
//添加一个节
intrusive_ptr<Section> section = document->AddSection();
//创建编号列表样式,指定每个级别的编号前缀和图案类型
intrusive_ptr<ListStyle> listStyle = document->GetStyles()->Add(ListType::Numbered, L"nestedStyle");
listStyle->GetListRef()->GetLevels()->GetItem(0)->SetPatternType(ListPatternType::Arabic);
listStyle->GetListRef()->GetLevels()->GetItem(0)->SetTextPosition(20);
listStyle->GetListRef()->GetLevels()->GetItem(1)->SetNumberPrefix(L"%1.");
listStyle->GetListRef()->GetLevels()->GetItem(1)->SetPatternType(ListPatternType::Arabic);
listStyle->GetListRef()->GetLevels()->GetItem(2)->SetNumberPrefix(L"%1.%2.");
listStyle->GetListRef()->GetLevels()->GetItem(2)->SetPatternType(ListPatternType::Arabic);
document->GetStyles()->Add(listStyle);
//添加一个段落
intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
paragraph->AppendText(L"这是一个多级编号列表:");
paragraph->GetFormat()->SetAfterSpacing(5);
//添加段落并对其应用编号列表样式
paragraph = section->AddParagraph();
paragraph->AppendText(L"第一项");
paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
paragraph->GetListFormat()->SetListLevelNumber(0);
//再添加五个段落,并将编号列表样式应用于特定段落
paragraph = section->AddParagraph();
paragraph->AppendText(L"第二项");
paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
paragraph->GetListFormat()->SetListLevelNumber(0);
paragraph = section->AddParagraph();
paragraph->AppendText(L"第一个子项");
paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
paragraph->GetListFormat()->SetListLevelNumber(1);
paragraph = section->AddParagraph();
paragraph->AppendText(L"第二个子项");
paragraph->GetListFormat()->ContinueListNumbering();
paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
paragraph = section->AddParagraph();
paragraph->AppendText(L"一个子项");
paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
paragraph->GetListFormat()->SetListLevelNumber(2);
paragraph = section->AddParagraph();
paragraph->AppendText(L"第三项");
paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
paragraph->GetListFormat()->SetListLevelNumber(0);
//保存结果文档
document->SaveToFile(L"多级编号列表.docx", FileFormat::Docx2019);
document->Dispose();
}
多级列表可以是编号列表和项目符号列表的组合。要创建混合类型列表,只需要创建编号列表样式和项目符号列表样式,并将它们应用于不同的段落。具体步骤如下。
int main() {
//创建一个Document对象
intrusive_ptr<Document> document = new Document();
//添加一个节
intrusive_ptr<Section> section = document->AddSection();
//创建编号列表样式
intrusive_ptr<ListStyle> numberedListStyle = document->GetStyles()->Add(ListType::Numbered, L"numberedStyle");
numberedListStyle->GetListRef()->GetLevels()->GetItem(0)->SetPatternType(ListPatternType::Arabic);
numberedListStyle->GetListRef()->GetLevels()->GetItem(0)->SetTextPosition(20);
numberedListStyle->GetListRef()->GetLevels()->GetItem(1)->SetPatternType(ListPatternType::LowLetter);
document->GetStyles()->Add(numberedListStyle);
//创建项目符号列表样式
intrusive_ptr<ListStyle> bulletedListStyle = document->GetStyles()->Add(ListType::Numbered, L"bulletedStyle");
bulletedListStyle->GetListRef()->GetLevels()->GetItem(2)->SetBulletCharacter(L"\u002A");
bulletedListStyle->GetListRef()->GetLevels()->GetItem(2)->GetCharacterFormat()->SetFontName(L"Symbol");
document->GetStyles()->Add(bulletedListStyle);
//添加段落
intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
paragraph->AppendText(L"这是一个多级混合列表:");
paragraph->GetFormat()->SetAfterSpacing(5);
//添加段落并对其应用编号列表样式
paragraph = section->AddParagraph();
paragraph->AppendText(L"第一项");
paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");
paragraph->GetListFormat()->SetListLevelNumber(0);
//再添加五个段落,并对其应用不同的列表样式
paragraph = section->AddParagraph();
paragraph->AppendText(L"第一个子项");
paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");
paragraph->GetListFormat()->SetListLevelNumber(1);
paragraph = section->AddParagraph();
paragraph->AppendText(L"第二个子项");
paragraph->GetListFormat()->SetListLevelNumber(1);
paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");
paragraph = section->AddParagraph();
paragraph->AppendText(L"子项1");
paragraph->GetListFormat()->ApplyStyle(L"bulletedStyle");
paragraph->GetListFormat()->SetListLevelNumber(2);
paragraph = section->AddParagraph();
paragraph->AppendText(L"子项2");
paragraph->GetListFormat()->ApplyStyle(L"bulletedStyle");
paragraph->GetListFormat()->SetListLevelNumber(2);
paragraph = section->AddParagraph();
paragraph->AppendText(L"第二项");
paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");
paragraph->GetListFormat()->SetListLevelNumber(0);
//保存结果文档
document->SaveToFile(L"多级混合类型列表.docx", FileFormat::Docx);
document->Dispose();
}
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
创建或编辑 Excel 文件时,用户可能需要操作工作表以使文件符合其特定需求。例如,他们可能需要添加新的工作表来记录不同类别的数据,移动或重新排列工作表以确保数据按逻辑顺序显示,或者删除包含不必要信息或错误的工作表。在本文中,我们将阐述如何使用 Spire.XLS for C++ 在 Excel 中添加、移动或删除工作表。
有两种方法可以将 Spire.XLS for C++ 集成到您的应用程序中。一种方法是通过 NuGet 安装它,另一种方法是从我们的网站下载包并将库复制到您的程序中。通过 NuGet 安装更简单,更推荐使用。您可以通过访问以下链接找到更多详细信息。
如何将 Spire.XLS for C++ 集成到 C++ 程序中
您可以使用 Workbook->GetWorksheets()->Add(LPCWSTR_S name) 方法将新工作表添加到 Excel 文件中。具体步骤如下:
#include "Spire.Xls.o.h";
using namespace Spire::Xls;
int main()
{
//初始化Workbook类的实例
intrusive_ptr<Workbook> workbook = new Workbook();
//加载Excel文件
workbook->LoadFromFile(L"示例文档.xlsx");
//将具有特定名称的新工作表添加到文件中
intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Add(L"新工作表"));
//将文本添加到工作表
dynamic_pointer_cast<CellRange>(sheet->GetRange(L"B1"))->SetText(L"这是一个新的工作表。");
//自动调整第二列的宽度
sheet->AutoFitColumn(2);
//保存结果文件
workbook->SaveToFile(L"添加工作表.xlsx", ExcelVersion::Version2013);
workbook->Dispose();
}
Spire.XLS for C++ 提供了 XlsWorksheet->MoveWorksheet(int destIndex) 方法,该方法可以让您轻松地将工作表从 Excel 文件中的一个位置移动到另一个位置。具体步骤如下:
#include "Spire.Xls.o.h";
using namespace Spire::Xls;
int main()
{
//初始化Workbook类的实例
intrusive_ptr<Workbook> workbook = new Workbook();
//加载Excel文件
workbook->LoadFromFile(L"添加工作表.xlsx");
//获取需要移动的工作表
intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Get(1));
//将工作表移动到Excel文件中的第一个位置
sheet->MoveWorksheet(0);
//保存结果文档
workbook->SaveToFile(L"移动工作表.xlsx", ExcelVersion::Version2013);
workbook->Dispose();
}
从 Excel 文件中删除工作表也很简单,只需调用 Workbook->GetWorksheets()->RemoveAt(int index) 方法即可。具体步骤如下:
#include "Spire.Xls.o.h";
using namespace Spire::Xls;
int main()
{
//初始化Workbook类的实例
intrusive_ptr<Workbook> workbook = new Workbook();
//加载Excel文件
workbook->LoadFromFile(L"添加工作表.xlsx");
//使用工作表索引从Excel文件中删除第二个工作表
workbook->GetWorksheets()->RemoveAt(1);
//保存结果文档
workbook->SaveToFile(L"删除工作表.xlsx", ExcelVersion::Version2013);
workbook->Dispose();
}
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
Spire.Office for Java 8.5.6 已发布。在该版本中,Spire.Doc for Java增强了 Word 到 PDF 和Word 到OFD的转换;Spire.PDF for Java支持了判断 PDF 流文档是否被加密的功能;Spire.XLS for Java增强了 Excel 到 PDF 的转换;Spire.Presentation for Java增强了 PPT 到图片的转换功能。此外,许多已知问题也在该版本中被成功修复。详情请阅读以下内容。
获取 Spire.Office for Java 8.5.6请点击:https://www.e-iceblue.cn/Downloads/Spire-Office-JAVA.html
问题修复:
新功能:
boolean result = PdfDocument.isPasswordProtected(PDFstream);问题修复:
问题修复:
问题修复:
Spire.Presentation for Java 8.5.2 已发布。本次更新增强了 PPT 到图片的转换功能。此外,该版本还修复了一些已知问题,如获取文字的光晕颜色失败的问题。详情请阅读以下内容。
问题修复:
https://www.e-iceblue.cn/Downloads/Spire-Presentation-JAVA.html
Spire.Doc for Java 11.5.5 已发布。该版本增强了 Word 到 PDF 和Word 到OFD的转换。此外,本次更新还修复了许多已知问题,如修复了加载文档时程序报错"Attempted to read past the end of the stream" 的问题。详情请阅读以下内容。
问题修复:
Spire.Office 8.5.3 已发布。在该版本中,Spire.XLS 新增支持 ISO.CEILING 和 WORKDAY.INTL 函数,以及 WPS 中的 EVALUATE() 函数;Spire.Doc 支持了添加图表的功能;Spire.PDF 优化了压缩 PDF 文档的功能。此外,本次更新还成功修复了大量已知问题。详情请阅读以下内容。
该版本涵盖了最新版的 Spire.Doc,Spire.PDF,Spire.XLS,Spire.Email,Spire.DocViewer, Spire.PDFViewer,Spire.Presentation,Spire.Spreadsheet, Spire.OfficeViewer, Spire.DocViewer, Spire.Barcode, Spire.DataExport。
版本信息如下:
https://www.e-iceblue.cn/Downloads/Spire-Office-NET.html
新功能:
//创建word文档
Document document = new Document();
//新建一个节
Section section = document.AddSection();
//创建新段落并附加文本
section.AddParagraph().AppendText("Column chart.");
//创建一个新节以附加柱形图
Paragraph newPara = section.AddParagraph();
ShapeObject shape = newPara.AppendChart(ChartType.Column, 500, 300);
//清除图表的系列数据,以一个干净的图表开始。
Chart chart = shape.Chart;
chart.Series.Clear();
//将自定义系列添加到图表中,其中X轴为类别,Y轴为相应的大数值。
chart.Series.Add("Test Series",
new[] { "Word", "PDF", "Excel", "GoogleDocs", "Office" },
new double[] { 1900000, 850000, 2100000, 600000, 1500000 });
//将Y轴刻度标签的数字格式设置为使用逗号对数字进行分组。
chart.AxisY.NumberFormat.FormatCode = "#,##0";
//保存结果文档
document.SaveToFile("AppendColumnChart.docx", FileFormat.Docx);//创建word文档
Document document = new Document();
//新建一个节
Section section = document.AddSection();
//创建新段落并附加文本
section.AddParagraph().AppendText("Bubble chart.");
//创建一个新段落并打开气泡图
Paragraph newPara = section.AddParagraph();
ShapeObject shape = newPara.AppendChart(ChartType.Bubble, 500, 300);
//清除字符的序列数据,以一个干净的图表开始
Chart chart = shape.Chart;
chart.Series.Clear();
// 添加带有X/Y坐标和每个气泡直径的自定义系列
ChartSeries series = chart.Series.Add("Test Series",
new[] { 2.9, 3.5, 1.1, 4.0, 4.0 },
new[] { 1.9, 8.5, 2.1, 6.0, 1.5 },
new[] { 9.0, 4.5, 2.5, 8.0, 5.0 });
//保存doc文件
document.SaveToFile("AppendBubbleChart.docx", FileFormat.Docx);//创建word文档
Document document = new Document();
//新建一个节
Section section = document.AddSection();
//创建新段落并附加文本
section.AddParagraph().AppendText("Line chart.");
//创建一个新段落以附加折线图
Paragraph newPara = section.AddParagraph();
ShapeObject shape = newPara.AppendChart(ChartType.Line, 500, 300);
//清除折线图的系列数据以从干净的图表开始
Chart chart = shape.Chart;
ChartTitle title = chart.Title;
title.Text = "My Chart";
ChartSeriesCollection seriesColl = chart.Series;
seriesColl.Clear();
//在图表内设置新数据
string[] categories = { "C1", "C2", "C3", "C4", "C5", "C6" };
seriesColl.Add("AW Series 1", categories, new double[] { 1, 2, 2.5, 4, 5, 6 });
seriesColl.Add("AW Series 2", categories, new double[] { 2, 3, 3.5, 6, 6.5, 7 });
//保存doc文件
document.SaveToFile("AppendLineChart.docx", FileFormat.Docx);//创建word文档
Document document = new Document();
//新建一个节
Section section = document.AddSection();
//创建新段落并附加文本
section.AddParagraph().AppendText("Pie chart.");
//创建一个新段落以附加饼图
Paragraph newPara = section.AddParagraph();
ShapeObject shape = newPara.AppendChart(ChartType.Pie, 500, 300);
Chart chart = shape.Chart;
//插入一个自定义图表系列,其中包含每个扇区的类别名称及其频率值。
ChartSeries series = chart.Series.Add("Test Series",
new[] { "Word", "PDF", "Excel" },
new[] { 2.7, 3.2, 0.8 });
//保存到docx文件。
document.SaveToFile("AppendPieChart.docx", FileFormat.Docx);//创建word文档
Document document = new Document();
//新建一个节
Section section = document.AddSection();
//创建新段落并附加文本
section.AddParagraph().AppendText("Scatter chart.");
//创建一个新段落以附加散点图
Paragraph newPara = section.AddParagraph();
ShapeObject shape = newPara.AppendChart(ChartType.Scatter, 450, 300);
Chart chart = shape.Chart;
//清除图表的系列数据,以一个干净的图表开始
chart.Series.Clear();
//插入五个点的X/Y坐标系列
chart.Series.Add("Scatter chart",
new[] { 1.0, 2.0, 3.0, 4.0, 5.0 },
new[] { 1.0, 20.0, 40.0, 80.0, 160.0 });
//保存到docx文件
document.SaveToFile("AppendScatterChart.docx", FileFormat.Docx);//创建word文档
Document document = new Document();
//新建一个节
Section section = document.AddSection();
//创建新段落并附加文本
section.AddParagraph().AppendText("Surface3D chart.");
//创建新段落以附加曲面三维图表
Paragraph newPara = section.AddParagraph();
ShapeObject shape = newPara.AppendChart(ChartType.Surface3D, 500, 300);
//清除其系列数据,以一个干净的图表开始
Chart chart = shape.Chart;
chart.Series.Clear();
chart.Title.Text = "My chart";
//添加三个系列
chart.Series.Add("Series 1",
new string[] { "Word", "PDF", "Excel", "GoogleDocs", "Office" },
new double[] { 1900000, 850000, 2100000, 600000, 1500000 });
chart.Series.Add("Series 2",
new string[] { "Word", "PDF", "Excel", "GoogleDocs", "Office" },
new double[] { 900000, 50000, 1100000, 400000, 2500000 });
chart.Series.Add("Series 3",
new string[] { "Word", "PDF", "Excel", "GoogleDocs", "Office" },
new double[] { 500000, 820000, 1500000, 400000, 100000 });
//保存到docx文件.
document.SaveToFile("AppendSurface3DChart.docx", FileFormat.Docx);//创建word文档
Document document = new Document();
//新建一个节
Section section = document.AddSection();
//创建新段落并附加文本
section.AddParagraph().AppendText("Bar chart.");
//创建一个新段落以附加条形图
Paragraph newPara = section.AddParagraph();
ShapeObject chartShape = newPara.AppendChart(ChartType.Bar, 400, 300);
Chart chart = chartShape.Chart;
//使用“标题”属性为条形图提供一个标题,该标题显示在图表区域的顶部中心。
ChartTitle title = chart.Title;
title.Text = "My Chart";
//将“Show”属性设置为“true”以使标题可见。
title.Show = true;
//将“Overlay”属性设置为“true”。允许其他图表元素与标题重叠,从而给它们更多的空间
title.Overlay = true;
//保存到docx文件
document.SaveToFile("AppendBarChart.docx", FileFormat.Docx);Document document = new Document();
Section section = document.AddSection();
string svgFile = "sample.svg";
Paragraph para = section.AddParagraph();
DocPicture svgPicture = para.AppendPicture(svgFile);
svgPicture.Width = 200;
svgPicture.Height = 200;
String DocxResult = "Result-AddSvg.docx";
document.SaveToFile(DocxResult, FileFormat.Docx2016);Document doc = new Document();
doc.LoadFromFile(inputFile, FileFormat.Docx);
System.Windows.Forms.PrintDialog printDialog = new System.Windows.Forms.PrintDialog();
printDialog.PrinterSettings.PrintToFile = true;
printDialog.PrinterSettings.PrintFileName = "sample-new-4.xps";
doc.PrintDialog = printDialog;
doc.PrintMultipageToOneSheet(PagesPreSheet.FourPages, true);Document doc = new Document();
doc.LoadFromFile(inputFile, FileFormat.Docx);
FixedLayoutDocument layoutDoc = new FixedLayoutDocument(doc);
// 访问第一页的行并打印到控制台。
FixedLayoutLine line = layoutDoc.Pages[0].Columns[0].Lines[0];
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("Line: " + line.Text);
//使用渲染的行,可以返回文档对象模型中的原始段落。
Paragraph para = line.Paragraph;
stringBuilder.AppendLine("Paragraph text: " + para.Text);
// 以纯文本格式(包括页眉和页脚)检索第一页上显示的所有文本。
string pageText = layoutDoc.Pages[0].Text;
stringBuilder.AppendLine(pageText);
// 循环遍历文档中的每一页,并打印每页上显示的行数。
foreach (FixedLayoutPage page in layoutDoc.Pages)
{
LayoutCollection lines = page.GetChildEntities(LayoutElementType.Line, true);
stringBuilder.AppendLine("Page " + page.PageIndex + " has " + lines.Count + " lines.");
}
// 此方法为任何给定节点提供布局实体的反向查找
// (页眉和页脚中的管路和节点除外)。
stringBuilder.AppendLine("The lines of the first paragraph:");
foreach (FixedLayoutLine paragraphLine in layoutDoc.GetLayoutEntitiesOfNode(
((Section)doc.FirstChild).Body.Paragraphs[0]))
{
stringBuilder.AppendLine(paragraphLine.Text.Trim());
stringBuilder.AppendLine(paragraphLine.Rectangle.ToString());
}
File.WriteAllText("page.txt", stringBuilder.ToString()); 新功能:
Workbook workbook = new Workbook();
workbook.Worksheets[0].Range["A1"].Formula = "ISO.CEILING(12.69,2)";
workbook.CalculateAllValue();
workbook.SaveToFile("result.xlsx",ExcelVersion.Version2016);Workbook workbook = new Workbook();
workbook.Worksheets[0].Range["A1"].Formula = "=WORKDAY.INTL(DATE(2023,3,17),25,1,DATE(2023,3,20))";
workbook.CalculateAllValue();
workbook.SaveToFile("result.xlsx",ExcelVersion.Version2016);问题修复:
新功能:
PdfCompressor compressor = new PdfCompressor("input.pdf");
compressor.Options.TextCompressionOptions.UnembedFonts = true;
compressor.Options.ImageCompressionOptions.CompressImage = true;
compressor.Options.ImageCompressionOptions.ResizeImages = true;
compressor.Options.ImageCompressionOptions.ImageQuality = ImageQuality.Low;
compressor.CompressToFile("output.pdf");问题修复:
问题修复:
Excel 中的数据验证控制可以将哪些类型的信息输入到单元格中。使用它,您可以仅将特定的数据类型(如数字或日期)限制在单元格中,或者将数字限制在特定范围内,并将文本限制在特定长度内。此外,它还允许您在下拉菜单中显示预定义值的列表,供用户选择。在本文中,您将学习如何使用 Spire.XLS for C++ 在 Excel 中应用或删除数据验证。
有两种方法可以将 Spire.XLS for C++ 集成到您的应用程序中。一种方法是通过 NuGet 安装它,另一种方法是从我们的网站下载包并将库复制到您的程序中。通过 NuGet 安装更简单,更推荐使用。您可以通过访问以下链接找到更多详细信息。
如何将 Spire.XLS for C++ 集成到 C++ 程序中
Spire.XLS for C++ 允许您为数字、日期、文本值、列表等创建验证规则。以下是将不同数据验证类型应用于 Excel 中指定单元格的步骤。
#include "Spire.Xls.o.h";
using namespace Spire::Xls;
int main() {
//指定输出文件
std::wstring outputFile = L"数据验证.xlsx";
//创建Workbook对象
intrusive_ptr<Workbook> workbook = new Workbook();
//获取指定的工作表
intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Get(0));
//在指定的单元格中插入文本
dynamic_pointer_cast<CellRange>(sheet->GetRange(L"B2"))->SetText(L"编号验证: ");
dynamic_pointer_cast<CellRange>(sheet->GetRange(L"B4"))->SetText(L"日期验证: ");
dynamic_pointer_cast<CellRange>(sheet->GetRange(L"B6"))->SetText(L"文本长度验证: ");
dynamic_pointer_cast<CellRange>(sheet->GetRange(L"B8"))->SetText(L"列表验证: ");
dynamic_pointer_cast<CellRange>(sheet->GetRange(L"B10"))->SetText(L"时间验证: ");
//将数字验证添加到C2
intrusive_ptr<CellRange> rangeNumber = dynamic_pointer_cast<CellRange>(sheet->GetRange(L"C2"));
rangeNumber->GetDataValidation()->SetAllowType(CellDataType::Decimal);
rangeNumber->GetDataValidation()->SetCompareOperator(ValidationComparisonOperator::Between);
rangeNumber->GetDataValidation()->SetFormula1(L"3");
rangeNumber->GetDataValidation()->SetFormula2(L"6");
rangeNumber->GetDataValidation()->SetInputMessage(L"输入一个介于1和10之间的数字");
rangeNumber->GetDataValidation()->SetErrorMessage(L"请输入正确的数字!");
rangeNumber->GetDataValidation()->SetShowError(true);
rangeNumber->GetDataValidation()->SetAlertStyle(AlertStyleType::Warning);
rangeNumber->GetStyle()->SetKnownColor(ExcelColors::Gray25Percent);
//将日期验证添加到C4
intrusive_ptr<CellRange> rangeDate = dynamic_pointer_cast<CellRange>(sheet->GetRange(L"C4"));
rangeDate->GetDataValidation()->SetAllowType(CellDataType::Date);
rangeDate->GetDataValidation()->SetCompareOperator(ValidationComparisonOperator::Between);
rangeDate->GetDataValidation()->SetFormula1(L"1/1/2021");
rangeDate->GetDataValidation()->SetFormula2(L"12/31/2021");
rangeDate->GetDataValidation()->SetInputMessage(L"输入2021年1月1日至2021年12月31日之间的日期");
rangeDate->GetStyle()->SetKnownColor(ExcelColors::Gray25Percent);
//将文本长度验证添加到C6
intrusive_ptr<CellRange> rangeTextLength = dynamic_pointer_cast<CellRange>(sheet->GetRange(L"C6"));
rangeTextLength->GetDataValidation()->SetAllowType(CellDataType::TextLength);
rangeTextLength->GetDataValidation()->SetCompareOperator(ValidationComparisonOperator::LessOrEqual);
rangeTextLength->GetDataValidation()->SetFormula1(L"5");
rangeTextLength->GetDataValidation()->SetErrorMessage(L"请输入有效字符串!");
rangeTextLength->GetDataValidation()->SetShowError(true);
rangeTextLength->GetDataValidation()->SetAlertStyle(AlertStyleType::Stop);
rangeTextLength->GetStyle()->SetKnownColor(ExcelColors::Gray25Percent);
//对C8应用列表验证
intrusive_ptr<CellRange> rangeList = dynamic_pointer_cast<CellRange>(sheet->GetRange(L"C8"));
std::vector<LPCWSTR_S> files = { L"美国", L"加拿大", L"英国" };
rangeList->GetDataValidation()->SetValues(files);
rangeList->GetDataValidation()->SetIsSuppressDropDownArrow(false);
rangeList->GetDataValidation()->SetInputMessage(L"从列表中选择一个项目");
rangeList->GetStyle()->SetKnownColor(ExcelColors::Gray25Percent);
//对C10应用时间验证
intrusive_ptr<CellRange> rangeTime = dynamic_pointer_cast<CellRange>(sheet->GetRange(L"C10"));
rangeTime->GetDataValidation()->SetAllowType(CellDataType::Time);
rangeTime->GetDataValidation()->SetCompareOperator(ValidationComparisonOperator::Between);
rangeTime->GetDataValidation()->SetFormula1(L"9:00");
rangeTime->GetDataValidation()->SetFormula2(L"12:00");
rangeTime->GetDataValidation()->SetInputMessage(L"输入一个介于9:00和12:00之间的时间");
rangeTime->GetStyle()->SetKnownColor(ExcelColors::Gray25Percent);
//自动调整列2的宽度
sheet->AutoFitColumn(2);
//设置列3的宽度
sheet->GetColumns()->GetItem(2)->SetColumnWidth(20);
//保存结果文档
workbook->SaveToFile(outputFile.c_str(), ExcelVersion::Version2016);
workbook->Dispose();
}
要删除应用于单元格的数据验证,Spire.XLS for C++ 提供了 Worksheet->GetDVTable()->Remove() 方法。详细步骤如下。
#include "Spire.Xls.o.h";
using namespace Spire::Xls;
int main() {
//指定输入和输出文件
std::wstring inputFile = L"数据验证.xlsx";
std::wstring outputFile = L"删除数据验证.xlsx";
//创建Workbook对象
intrusive_ptr<Workbook> workbook = new Workbook();
//加载Excel文档示例
workbook->LoadFromFile(inputFile.c_str());
//获取指定的工作表
intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Get(0));
//创建一个矩形数组,用于定位工作表中的区域
std::vector<intrusive_ptr<Spire::Common::Rectangle>> rectangles(1);
//为数组的第一个元素赋值,并指定矩形单元格范围
rectangles[0] = Spire::Common::Rectangle::FromLTRB(0, 0, 2, 9);
//删除由矩形表示的范围内的验证
sheet->GetDVTable()->Remove(rectangles);
//保存结果文档
workbook->SaveToFile(outputFile.c_str(), ExcelVersion::Version2016);
workbook->Dispose();
}
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。