使用Microsoft Interop格式化Excel单元格

时间:2009-10-13 16:35:46

标签: c# .net excel interop formatting

我用Microsoft Interop生成一些Excel文件,没问题,我可以创建文件,工作表,文件,密码保护。但我想:

  • 对于特定范围仅允许数字
  • 对于另一个特定范围仅允许数字但仅允许0或1

你知道怎么做吗?

谢谢,

3 个答案:

答案 0 :(得分:1)

花了一段时间,但我想我明白了。我假设你正在使用Excel 2007.我也假设你已经引用了一个范围。这是一个简单的例子。

Excel.Worksheet sheet = this.Application.ActiveSheet as Excel.Worksheet;
Excel.Range range = sheet.get_Range("A1", "A5") as Excel.Range;

 //delete previous validation rules 
 range.Validation.Delete();
 range.Validation.Add(Excel.XlDVType.xlValidateWholeNumber,
                                 Excel.XlDVAlertStyle.xlValidAlertStop,
                                 Excel.XlFormatConditionOperator.xlBetween,
                                 0, 1);

这将在 0 A5 <之间的特定范围内添加 0 1 之间的数字验证/强>

您还可以进一步使用验证对象来创建自定义错误消息等。

希望这有帮助。

答案 1 :(得分:0)

如果要验证单元格中的条目,请查看Validation.Add方法。

MSDN Example

你的第二个是:

aRange.Validation.Add(XlDVType.xlValidateWholeNumber, XlDVAlertStyle.xlValidAlertStop, XlFormatConditionOperator.xlBetween, 0, 1);

答案 2 :(得分:0)

以为我会发布一些可能有用的代码,包括所需的MS命名空间。

using System;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;

/// <summary>
/// setup this cell to validate (and report error) as decimal value input
/// </summary>
void SetupCellValidation_decimal(Excel.Range cell)
{
  try
  {
    // Delete any previous validation
    cell.Validation.Delete();
    // Add validation that allows any decimal value
    cell.Validation.Add(Excel.XlDVType.xlValidateDecimal, Excel.XlDVAlertStyle.xlValidAlertStop,
      Excel.XlFormatConditionOperator.xlBetween, decimal.MinValue, decimal.MaxValue);

    cell.Validation.IgnoreBlank = true; // allow blank entries
    cell.Validation.ErrorTitle = "Invalid Entry";
    cell.Validation.ErrorMessage = "You must enter a valid number";
  }
  catch (Exception ex)
  {
    System.Windows.Forms.MessageBox.Show("validate error: " + ex.Message);
  }
}

/// <summary>
/// 
/// </summary>
void exampleCellValidator(Excel.Range cell)
{
  try
  {
    //Delete any previous validation
    cell.Validation.Delete();

    // for integers:
    cell.Validation.Add(Excel.XlDVType.xlValidateWholeNumber, Excel.XlDVAlertStyle.xlValidAlertStop,
      Excel.XlFormatConditionOperator.xlBetween, 0, 120);

    // for decimal:
    cell.Validation.Add(Excel.XlDVType.xlValidateDecimal, Excel.XlDVAlertStyle.xlValidAlertStop,
      Excel.XlFormatConditionOperator.xlBetween, decimal.MinValue, decimal.MaxValue);

    cell.Validation.IgnoreBlank = true;
    // error messaging
    cell.Validation.ErrorMessage = "Entry is not a valid number";
    cell.Validation.ErrorTitle = "Error - invalid entry";

    // use these if you want to display a message each time user activates this cell
    cell.Validation.InputTitle = "Entry Rule"; // a message box title
    cell.Validation.InputMessage = "You must enter a valid number"; // message to instruct user what to do
  }
  catch (Exception ex)
  {
    System.Windows.Forms.MessageBox.Show("validate error: " + ex.Message);
  }
}