Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Microsoft Interop格式化Excel单元格_C#_.net_Excel_Interop_Formatting - Fatal编程技术网

C# 使用Microsoft Interop格式化Excel单元格

C# 使用Microsoft Interop格式化Excel单元格,c#,.net,excel,interop,formatting,C#,.net,Excel,Interop,Formatting,我用Microsoft Interop生成一些Excel文件,没问题,我可以创建文件、工作表、文件、密码保护。但我想: 对于特定范围,仅允许数字 对于另一个特定范围,仅允许数字,但仅允许0或1 你知道怎么做吗 谢谢,如果要验证单元格中的条目,请查看该方法 第二个类似于: aRange.Validation.Add(XlDVType.xlValidateWholeNumber, XlDVAlertStyle.xlValidAlertStop, XlFormatConditionOperato

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

  • 对于特定范围,仅允许数字
  • 对于另一个特定范围,仅允许数字,但仅允许0或1
你知道怎么做吗


谢谢,

如果要验证单元格中的条目,请查看该方法

第二个类似于:

aRange.Validation.Add(XlDVType.xlValidateWholeNumber, XlDVAlertStyle.xlValidAlertStop, XlFormatConditionOperator.xlBetween, 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之间的数字验证

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


希望这能有所帮助。

我想我会发布一些可能有用的代码,包括所需的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);
  }
}
使用系统;
使用Excel=Microsoft.Office.Interop.Excel;
使用Microsoft.Office.Interop.Excel;
/// 
///设置此单元格以验证(并报告错误)为十进制值输入
/// 
无效SetupCellValidation\u十进制(Excel.Range单元格)
{
尝试
{
//删除以前的任何验证
cell.Validation.Delete();
//添加允许任何十进制值的验证
cell.Validation.Add(Excel.XlDVType.xlValidateDecimal、Excel.XlDVAlertStyle.xlValidAlertStop、,
Excel.XlFormatConditionOperator.xlBetween、decimal.MinValue、decimal.MaxValue);
cell.Validation.IgnoreBlank=true;//允许空白条目
cell.Validation.ErrorTitle=“无效条目”;
cell.Validation.ErrorMessage=“您必须输入一个有效的数字”;
}
捕获(例外情况除外)
{
System.Windows.Forms.MessageBox.Show(“验证错误:+ex.Message”);
}
}
/// 
/// 
/// 
void exampleCellValidator(Excel.Range单元格)
{
尝试
{
//删除以前的任何验证
cell.Validation.Delete();
//对于整数:
cell.Validation.Add(Excel.XlDVType.xlValidateWholeNumber、Excel.XlDVAlertStyle.xlValidAlertStop、,
Excel.XlFormatConditionOperator.xlBetween,0,120);
//对于十进制:
cell.Validation.Add(Excel.XlDVType.xlValidateDecimal、Excel.XlDVAlertStyle.xlValidAlertStop、,
Excel.XlFormatConditionOperator.xlBetween、decimal.MinValue、decimal.MaxValue);
cell.Validation.IgnoreBlank=true;
//错误消息
cell.Validation.ErrorMessage=“条目不是有效数字”;
cell.Validation.ErrorTitle=“错误-无效条目”;
//如果要在用户每次激活此单元格时显示消息,请使用这些选项
cell.Validation.InputTitle=“输入规则”;//消息框标题
cell.Validation.InputMessage=“您必须输入一个有效的数字”;//指示用户做什么的消息
}
捕获(例外情况除外)
{
System.Windows.Forms.MessageBox.Show(“验证错误:+ex.Message”);
}
}

我试试这个。但我使用Office2002的互操作,它是我的customer@Kris,这两种方法都应该有效。如果没有,请告诉我,我会看看是否能让2002工作。我用VisualStudio2005和Excel2003试用了它,效果很好。有一件事我无法理解,那就是如何不传递可选参数。根据您使用的文档类型。缺少,但编译时会导致“来自HRESULT的异常:0x800A03EC”。