#include "CApplication.h"
#include "CWorksheets.h"
#include "CWorksheet.h"
#include "CWorkbooks.h"
#include "CWorkbook.h"
#include "CValidation.h"
#include "CRange.h"
#include "CRanges.h"
#include "CFont0.h"
//
typedef enum {
xlValidateInputOnly = 0x00,
xlValidateWholeNumber = 0x01,
xlValidateDecimal = 0x02,
xlValidateList = 0x03,
xlValidateDate = 0x04,
xlValidateTime = 0x05,
xlValidateTextLength = 0x06,
xlValidateCustom = 0x07
}XIDVTYPE;
typedef enum {
xlValidAlertStop = 0x01,
xlValidAlertWarning = 0x02,
xlValidAlertInformation = 0x03
}XIDVAlertStyle;
typedef enum {
xlBetween = 0x01,
xlNotBetween = 0x02,
xlEqual = 0x03,
xlNotEqual = 0x04,
xlGreater = 0x05,
xlGreaterEqual = 0x07,
xlLess = 0x06,
xlLessEqual = 0x08
}XIFormatConditionOperatorType;
//
//如下为设置序列的数据源。此数据源在表‘数据项’的A2:A4中。作为参数传入。
sDataSource = _T("=INDIRECT(数据项!$A$2:$A$4)");
m_ExcelInterface.SetCellFormatFormulaSequence(xlValidateList,sDataSource,2,1000,3);
/*
Function:SetCellFormatFormulaSequence
Discription:设置某一列的格式有效性条件为序列
Input: NONE
return:无
*/
void CExcelInterface::SetCellFormatFormulaSequence(XIDVTYPE nXIDVType, CString sFormula1, int startRow, int endRow, int nCol)//设置单元格公式格式
{
LPDISPATCH lpValidRange;
//首先获取CELL有范围。
m_range = m_Worksheet.get_Range(COleVariant(IndexToString(startRow,nCol)),COleVariant(IndexToString(endRow,nCol)));
//其次获取有效性的CELL。
lpValidRange = m_range.get_Validation();
//再其次CValidation m_WorkRangeValid;绑定CELL范围。
m_WorkRangeValid.AttachDispatch(lpValidRange);
//下一步,先删除此CELL范围的有效性
m_WorkRangeValid.Delete();
//最后,修改CELL范围的有效性为xlValidateList。
老大们,就是这里执行不过去啊。这里MODIFY中的Formula1为数据源。Formula2可以略掉。也帮我看看。
m_WorkRangeValid.Modify(COleVariant((long)nXIDVType),COleVariant((long)xlValidAlertStop),COleVariant((long)xlBetween),COleVariant(sFormula1),COleVariant(sFormula1));
}