冰蓝科技
|
028-81705109
|
|
微信扫一扫
|

Spire.Cloud 纯前端文档控件

超链接是 Excel 中一个强有力的工具,它允许用户连接 Excel 文件的不同部分或链接到外部资源,例如网站、电子邮件地址或其他文件。它们为用户提供了一种在工作表内或不同工作表之间导航的快捷方式。除了便于导航之外,超链接还可以提供与文件中的数据相关的附加上下文或资源。例如,您可以链接到一个网站,该网站提供了有关工作表中所列出的特定产品的更多信息,可以帮助读者可以更深入地了解该产品。在本文中,我们将探讨如何向 Excel 添加超链接,特别是如何使用 Spire.XLS for C++ 在 C++ 中向 Excel 文件添加文本超链接和图像超链接。

安装 Spire.XLS for C++

有两种方法可以将 Spire.XLS for C++ 集成到您的应用程序中。一种方法是通过 NuGet 安装它,另一种方法是从我们的网站下载包并将库复制到您的程序中。通过 NuGet 安装更简单,更推荐使用。您可以通过访问以下链接找到更多详细信息。

如何将 Spire.XLS for C++ 集成到 C++ 程序中

在 Excel 中添加文本超链接

Excel 中的文本超链接是一个单词或短语,您可以单击它跳到特定位置,例如 Excel 文件的不同部分、电子邮件地址、网页或外部文件。以下步骤说明如何使用 Spire.XLS for C++ 将文本超链接添加到 Excel 文件:

  • C++
#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();
}

C++ 向 Excel 添加超链接

在 Excel 中添加图像超链接

与文本超链接类似,图像超链接是可以让您单击以导航到特定位置的图像。以下步骤说明了如何使用 Spire.XLS for C++ 将图像超链接添加到 Excel 文件:

  • C++
#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();
}

C++ 向 Excel 添加超链接

申请临时 License

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。

Spire.XLS 13.5.6 已发布。该版本新增实现chartsheet和worksheet之间可以互相移动位置的方法,并且新增了Worksheet.DeleteRows(int[] rows)方法来优化删除行的速度。同时,该版本还增强了 Excel 到 HTML 和图片的转换。许多已知问题也在该版本中被成功修复,如:修复了添加文本框时设置置于顶层功能不生效的问题。详情请阅读以下内容。

新功能:

问题修复:


下载 Spire.XLS 13.5.6请点击:

https://www.e-iceblue.cn/Downloads/Spire-XLS-NET.html

Spire.Doc 11.5.12 已发布。改版本增强了 Word 到 PDF 和 HTML 的转换。同时,该版本还修复了许多已知问题,如加载文档时抛异常"System.InvalidCastException"的问题。详情请阅读以下内容。

问题修复:


获取Spire.Doc 11.5.12请点击:

https://www.e-iceblue.cn/Downloads/Spire-Doc-NET.html

如果你想让段落更容易导航和阅读,将它们按字母、数字甚至项目符号顺序重新排列,读者可以快速找到他们想要的内容,并立即在列表中搜索。在本文中,您将学习如何使用 Spire.Doc for C++ 在 Word 文档中创建编号列表、项目符号列表和多级列表。

安装 Spire.Doc for C++

有两种方法可以将 Spire.Doc for C++ 集成到您的应用程序中。一种方法是通过 NuGet 安装它,另一种方法是从我们的网站下载包并将库复制到您的程序中。通过 NuGet 安装更简单,更推荐使用。您可以通过访问以下链接找到更多详细信息。

如何将 Spire.Doc for C++ 集成到 C++ 程序中

在 Word 中创建编号列表

Spire.Doc for C++ 提供了 ListStyle 类,您可以使用该类创建编号列表样式或项目符号样式。然后,可以使用 Paragraph->GetListFormat()->ApplyStyle() 方法将列表样式应用于段落。创建编号列表的步骤如下。

  • C++
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();
}

C++ 在 Word 文档中创建列表

在 Word 中创建项目符号列表

创建项目符号列表的过程与创建编号列表的过程类似。不同之处在于,创建列表样式时,必须将列表类型指定为“项目符号”,并为其设置项目符号。以下是详细步骤。

  • C++
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();
}

C++ 在 Word 文档中创建列表

在 Word 中创建多级编号列表

多级列表至少由两个不同的级别组成。嵌套列表的每个级别都可以使用 ListStyle ->GetListRef()->GetLevels()->GetItem(index) 方法进行访问。通过 ListLevel 对象,您可以设置某个级别的编号类型和前缀。以下是在 Word 中创建多级编号列表的步骤。

  • C++
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();
}

C++ 在 Word 文档中创建列表

在 Word 中创建多级混合类型列表

多级列表可以是编号列表和项目符号列表的组合。要创建混合类型列表,只需要创建编号列表样式和项目符号列表样式,并将它们应用于不同的段落。具体步骤如下。

  • C++
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();
}

C++ 在 Word 文档中创建列表

申请临时 License

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。

创建或编辑 Excel 文件时,用户可能需要操作工作表以使文件符合其特定需求。例如,他们可能需要添加新的工作表来记录不同类别的数据,移动或重新排列工作表以确保数据按逻辑顺序显示,或者删除包含不必要信息或错误的工作表。在本文中,我们将阐述如何使用 Spire.XLS for C++ 在 Excel 中添加、移动或删除工作表。

安装 Spire.XLS for C++

有两种方法可以将 Spire.XLS for C++ 集成到您的应用程序中。一种方法是通过 NuGet 安装它,另一种方法是从我们的网站下载包并将库复制到您的程序中。通过 NuGet 安装更简单,更推荐使用。您可以通过访问以下链接找到更多详细信息。

如何将 Spire.XLS for C++ 集成到 C++ 程序中

将工作表添加到 Excel 文件

您可以使用 Workbook->GetWorksheets()->Add(LPCWSTR_S name) 方法将新工作表添加到 Excel 文件中。具体步骤如下:

  • C++
#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();
}

C++ 在 Excel 中添加、移动或删除工作表

将工作表移动到 Excel 文件中的另一个位置

Spire.XLS for C++ 提供了 XlsWorksheet->MoveWorksheet(int destIndex) 方法,该方法可以让您轻松地将工作表从 Excel 文件中的一个位置移动到另一个位置。具体步骤如下:

  • C++
#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();
}

C++ 在 Excel 中添加、移动或删除工作表

从 Excel 文件中删除工作表

从 Excel 文件中删除工作表也很简单,只需调用 Workbook->GetWorksheets()->RemoveAt(int index) 方法即可。具体步骤如下:

  • C++
#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();
}

C++ 在 Excel 中添加、移动或删除工作表

申请临时 License

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该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


Spire.Doc for Java

问题修复:


Spire.PDF for Java

新功能:

问题修复:


Spire.XLS for Java

问题修复:


Spire. Presentation for Java

问题修复:

Spire.Presentation for Java 8.5.2 已发布。本次更新增强了 PPT 到图片的转换功能。此外,该版本还修复了一些已知问题,如获取文字的光晕颜色失败的问题。详情请阅读以下内容。

问题修复:


获取Spire.Presentation for Java 8.5.2请点击:

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.Doc for Java 11.5.5请点击:

https://www.e-iceblue.cn/Downloads/Spire-Doc-JAVA.html

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。

版本信息如下:

获取Spire.Office 8.5.3请点击:

https://www.e-iceblue.cn/Downloads/Spire-Office-NET.html


Spire.Doc

新功能:


Spire.XLS

新功能:

问题修复:


Spire.PDF

新功能:

问题修复:


Spire.Email

问题修复:

Excel 中的数据验证控制可以将哪些类型的信息输入到单元格中。使用它,您可以仅将特定的数据类型(如数字或日期)限制在单元格中,或者将数字限制在特定范围内,并将文本限制在特定长度内。此外,它还允许您在下拉菜单中显示预定义值的列表,供用户选择。在本文中,您将学习如何使用 Spire.XLS for C++ 在 Excel 中应用或删除数据验证。

安装 Spire.XLS for C++

有两种方法可以将 Spire.XLS for C++ 集成到您的应用程序中。一种方法是通过 NuGet 安装它,另一种方法是从我们的网站下载包并将库复制到您的程序中。通过 NuGet 安装更简单,更推荐使用。您可以通过访问以下链接找到更多详细信息。

如何将 Spire.XLS for C++ 集成到 C++ 程序中

将数据验证应用于 Excel 单元格

Spire.XLS for C++ 允许您为数字、日期、文本值、列表等创建验证规则。以下是将不同数据验证类型应用于 Excel 中指定单元格的步骤。

  • C++
#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();
}

C++ 在 Excel 中应用或删除数据验证

从 Excel 单元格中删除数据验证

要删除应用于单元格的数据验证,Spire.XLS for C++ 提供了 Worksheet->GetDVTable()->Remove() 方法。详细步骤如下。

  • C++
#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();
}

C++ 在 Excel 中应用或删除数据验证

申请临时 License

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。