簡體   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