如何使用Spreadsheetgear - C#.NET为excel中的单元格添加数据验证

时间:2016-05-02 21:02:06

标签: c# .net excel validation spreadsheetgear

我想为excel中的单元格添加数据验证,以仅允许数字值。

我的代码执行以下操作,

SpreadSheetGearHelper hlpr = new SpreadSheetGearHelper(excelFilePath);
cells = workbook.Worksheets[0].Cells;
hlpr.WorkSheet(0).Cells[string.Format("{0}:{0}", colName)].Validation.Add(SpreadsheetGear.ValidationType.WholeNumber, ValidationAlertStyle.Stop,
ValidationOperator.Between, "-9999999", "9999999");
hlpr.WorkSheet(0).Cells[string.Format("{0}:{0}", colName)].NumberFormat = "@";
hlpr.WorkSheet(0).Cells[string.Format("{0}:{0}", colName)].Validation.ErrorMessage = "Please enter a number";

但是当我在excel范围内输入有效数字时,它仍然会显示“请输入一个数字”。

有人可以帮我解决这个问题吗

3 个答案:

答案 0 :(得分:1)

您目前正在使用ValidationType。WholeNumber,它只允许整数,如1,2,3,而不是小数,如1.23。如果您需要允许所有数值而不仅仅是整数,则需要指定ValidationType。十进制。例如:

using SpreadsheetGear;
...
IWorkbook workbook = Factory.GetWorkbook();
IWorksheet worksheet = workbook.ActiveWorksheet;
IRange cells = worksheet.Cells;
cells["A1"].Validation.Add(ValidationType.Decimal, ValidationAlertStyle.Stop, 
    ValidationOperator.Between, "-9999999", "9999999");

答案 1 :(得分:1)

看看这个:Apply-Data-Validation-to-Excel-Cells-in-Csharp - CodeProject

        sheet.Range["C9"].DataValidation.AllowType = CellDataType.Decimal;
        sheet.Range["C9"].DataValidation.Formula1 = "-9999999";
        sheet.Range["C9"].DataValidation.Formula2 = "9999999";
        sheet.Range["C9"].DataValidation.CompareOperator = ValidationComparisonOperator.Between;
        sheet.Range["C9"].DataValidation.InputMessage = "Type a number between -9999999-9999999 in this cell.";

我不熟悉SpreadsheetGear,但本文中的解决方案在我身边运行良好。

答案 2 :(得分:0)

修正了它。

单元格的数字格式设置为文本,这就是为什么每次输入数字时它都会给我一个错误(即使数据验证设置正确)。因此,我添加了以下代码行,将其更改为" General"

import UIKit

class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

    var collectionView: UICollectionView!
    var ImageNames: [String] = ["photo", "photo1", "photo2", "photo3"]

    override func viewDidLoad() {
        super.viewDidLoad()

        let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
        layout.itemSize = CGSize(width: 90, height: 120)

        collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
        collectionView.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(collectionView)
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ImageNames.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
        let imageView = UIImageView(image: UIImage(named: ImageNames[indexPath.row]))
        cell.backgroundView = imageView
        return cell
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let cell = collectionView.cellForItemAtIndexPath(indexPath)
        cell?.backgroundView = nil
    }

}

谢谢你们的时间和回应。