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 中指定单元格的步骤。
- 创建 Workbook 对象。
- 使用 Workbook->GetWorksheets()->Get() 方法获取指定的工作表。
- 使用 Worksheet->GetRange() 方法获取特定单元格。
- 使用 CellRange->GetDataValidation()->SetAllowType() 方法设置单元格中允许的数据类型。您可以选择不同的数据类型,如十进制、时间、日期、文本长度和整数。
- 使用 CellRange->GetDataValidation()->SetCompareOperator() 方法设置比较运算符。比较运算符包括 Between(介于)、NotBetween(未介于)、Less(小于)、Greater(大于)和 Equal(等于)。
- 使用 CellRange->GetDataValidation()->SetFormula1() 和 CellRange->GetDataValidation()->SetFormula2() 方法为数据验证设置一个或两个公式。
- 使用 CellRange->GetDataValidation()->SetInputMessage() 方法设置输入提示。
- 使用 CellRange->GetDataValidation()->SetErrorMessage() 方法设置错误消息。
- 设置为显示错误警告,并在输入无效数据时设置其警告样式。
- 使用 Workbook->SaveToFile() 方法保存结果文档。
- 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();
}
从 Excel 单元格中删除数据验证
要删除应用于单元格的数据验证,Spire.XLS for C++ 提供了 Worksheet->GetDVTable()->Remove() 方法。详细步骤如下。
- 创建 Workbook 对象。
- 使用 Workbook->LoadFromFile() 方法加载包含数据验证的示例 Excel 文档。
- 使用 Workbook->GetWorksheets()->Get() 方法获取指定的工作表。
- 创建一个矩形数组,用于定位将删除验证的单元格。
- 使用 Worksheet->GetDVTable()->Remove() 方法从所选单元格中删除数据验证。
- 使用 Workbook->SaveToFile() 方法保存结果文档。
- 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();
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。