繁体   English   中英

使用Microsoft Interop格式化Excel单元格

[英]Formatting Excel cell with Microsoft Interop

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

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

你有一个想法如何做到这一点?

谢谢,

花了一段时间,但我想我明白了。 我假设您正在使用Excel2007。我还假设您已经对一个范围进行了引用。 这是一个简单的例子。

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);

在这种情况下,这将为A1A5之间的特定范围添加一个介于01之间的数字验证。

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

希望这可以帮助。

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

MSDN示例

您的第二个是这样的:

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

以为我会发布一些可能有帮助的代码,包括所需的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);
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM