Formatting Excel cell with Microsoft Interop
I generate some Excel file with Microsoft Interop, no problem, I can create files, sheet, file, pass开发者_高级运维word protect. But I'd like :
- for a specific range allow only numbers
- for an another specific range allow only numbers but only 0 or 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. 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.
You can also play with the Validation object further to create custom Error Messages etc.
Hope this helps.
If you want to validate an entry into the Cell, look at the Validation.Add method.
MSDN Example
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.
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);
}
}
精彩评论