简体   繁体   English

使用Microsoft Interop格式化Excel单元格

[英]Formatting Excel cell with Microsoft Interop

I generate some Excel file with Microsoft Interop, no problem, I can create files, sheet, file, password protect. 我用Microsoft Interop生成了一些Excel文件,没问题,我可以创建文件,表格,文件,密码保护。 But I'd like : 但我想:

  • for a specific range allow only numbers 在特定范围内仅允许数字
  • for an another specific range allow only numbers but only 0 or 1 对于另一个特定范围,仅允许数字,但仅允许0或1

Do you have an idea how to do this ? 你有一个想法如何做到这一点?

Thanks, 谢谢,

took a while but I think I got it. 花了一段时间,但我想我明白了。 I am assuming you're using Excel 2007. I am also assuming you have a reference to a range already. 我假设您正在使用Excel2007。我还假设您已经对一个范围进行了引用。 Here is a quick example. 这是一个简单的例子。

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

This will add a validation of number between 0 and 1 for a specific range in this case between A1 and A5 . 在这种情况下,这将为A1A5之间的特定范围添加一个介于01之间的数字验证。

You can also play with the Validation object further to create custom Error Messages etc. 您还可以进一步使用Validation对象来创建自定义错误消息等。

Hope this helps. 希望这可以帮助。

If you want to validate an entry into the Cell, look at the Validation.Add method. 如果要验证单元格中的条目,请查看Validation.Add方法。

MSDN Example MSDN示例

Your second one is something like: 您的第二个是这样的:

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

Thought I'd post a bit of code that may help, including the MS namespaces needed. 以为我会发布一些可能有帮助的代码,包括所需的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