我使用Microsoft Interop生成一些Excel文件,沒問題,我可以創建文件,工作表,文件,密碼保護。但我想:使用Microsoft Interop格式化Excel單元
- 特定範圍內只允許數字
- 爲其他特定的範圍,只允許數字,但只有0或1
你有一個想法如何做到這一點?
感謝,
我使用Microsoft Interop生成一些Excel文件,沒問題,我可以創建文件,工作表,文件,密碼保護。但我想:使用Microsoft Interop格式化Excel單元
你有一個想法如何做到這一點?
感謝,
了一段時間,但我認爲我得到了它。我假設你正在使用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);
這將在這種情況下添加數的確認和之間,用於在特定的範圍A1和A5之間。
您也可以與驗證發揮進一步對象來創建自定義錯誤消息等
希望這有助於。
如果您想驗證的進入細胞,看Validation.Add方法。
你的第二個是一樣的東西:
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);
}
}
我會試試這個。但是我爲Office 2002使用interop,它是我的客戶 – 2009-10-13 17:38:04
@Kris的「標準」,這應該可以工作。讓我知道如果沒有,我會看看我能否得到2002年的工作。 – 2009-10-13 20:46:54
我試過用Visual Studio 2005和Excel 2003,它可以工作。我無法得到的一件事是如何不通過可選參數。根據你使用Type.Missing的文檔,但是這個編譯它的結果是「Exception from HRESULT:0x800A03EC」。 – Mark 2009-10-13 22:37:14