|
11-1 单元格字体格式设置
Excel.Font rng = this.Range["A1"].Font;
rng.Name = "宋体";
rng.FontStyle = "Bold";
rng.Size = 18;
rng.ColorIndex = 3;
rng.Underline = 2;
11-2 设置单元格内部格式
Excel.Interior rng = this.Range["A1"].Interior;
rng.ColorIndex = 3;
rng.Pattern = Excel.XlPattern.xlPatternCrissCross;
rng.PatternColorIndex = 6;
11-3 为单元格区域添加边框
Excel.Borders rng = this.Range["B4:G10"].Borders;
rng.LineStyle = Excel.XlLineStyle.xlContinuous;
rng.Weight = Excel.XlBorderWeight.xlThin;
rng.ColorIndex = 5;
Excel.XlColorIndex col = (Excel.XlColorIndex)5;
this.Range["B4:G10"].BorderAround2(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium,col);
11-3 为单元格区域应用多种边框格式
Excel.XlColorIndex col = (Excel.XlColorIndex)5;
Excel.Border rng = this.Range["B4:G10"].Borders[Excel.XlBordersIndex.xlInsideHorizontal];
Excel.Border Rng = this.Range["B4:G10"].Borders[Excel.XlBordersIndex.xlInsideVertical];
rng.LineStyle = Excel.XlLineStyle.xlDot;
rng.Weight = Excel.XlBorderWeight.xlThin;
rng.ColorIndex = col;
Rng.LineStyle = Excel.XlLineStyle.xlContinuous;
Rng.Weight = Excel.XlBorderWeight.xlThin;
Rng.ColorIndex = col;
this.Range["B4:G10"].BorderAround2(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, col);
11-4 靈活設置單元格的行高列寬
Excel.Range rng1 = this.Range["A1"];
Excel.Range rng2 = this.Range["B1"];
rng1.RowHeight = this.Application.CentimetersToPoints(2);
rng1.ColumnWidth = this.Application.CentimetersToPoints(1.5);
rng2.RowHeight = this.Application.CentimetersToPoints(1.2);
rng2.ColumnWidth = this.Application.CentimetersToPoints(0.3);
12-1 單元格中建立數據有效性
Excel.Range rng = this.Range["A1:A10"];
rng.Validation.Delete();
rng.Validation.Add(
Excel.XlDVType.xlValidateList,
Excel.XlDVAlertStyle.xlValidAlertStop,
Excel.XlFormatConditionOperator.xlBetween,
"1,2,3,4,5,6,7,8");
12-2 判斷單元格是否存在數據有效性
try
{
if (this.Range["A12"].Validation.Type >= 0)
{
MessageBox.Show("單元格中有數據有效性!");
}
}
catch
{
MessageBox.Show("單元格中沒有數據有效性!");
}
12-3 動態的數據有效性
void 工作表1_SelectionChange(Excel.Range Target)
{
if (Target.Column == 1 && Target.Count == 1 && Target.Row > 1)
{
Target.Validation.Delete();
Target.Validation.Add(
Excel.XlDVType.xlValidateList,
Excel.XlDVAlertStyle.xlValidAlertStop,
Excel.XlFormatConditionOperator.xlBetween,
"主機,顯示器");
}
}
12-4 自動展開數據有效性下拉列表
this.Application.SendKeys("%{down}");
13-1 在單元格中寫入公式
this.Range["C1:C10"].Formula="=sum(A1,B1)";
13-1 寫入單元格區域數組公式
this.Range["C1"].FormulaArray = "=A1:A2*B1:B2";
13-2 檢查單元格是否含有公式
Excel.Range rng = this.Application.Selection;
if (Convert.IsDBNull(rng.HasFormula))
{
MessageBox.Show("公式區域為:" + rng.SpecialCells(Excel.XlCellType.xlCellTypeFormulas, 23).Address[0, 0]);
}
else if(rng.HasFormula)
{
MessageBox.Show("全部單元格為公式!");
}
else
{
MessageBox.Show("全部單元格不為公式!");
}
注:因為HasFormula返回的是一個dynamic類型的值,C#的swith貌似并不支持.
13-3 判斷單元格公式是否存在錯誤
未研究出來,如何調用VBA.IsError,用了Excel自帶函數來處理的.
Excel.Range rng = this.Range["A1"].Offset[0, 1];
rng.Formula = "=iserror(A1)";
if (rng.Value)
{
MessageBox.Show("A1單元格錯誤類型為:" + this.Range["A1"].Text);
}
else
{
MessageBox.Show("A1單元格結果為:" + this.Range["A1"].Text);
}
13-4 取得單元格中公式的引用單元格
Excel.Range rng = this.Range["C1"].Precedents;
MessageBox.Show("公式所引用的單元格有:" + rng.Address);
13-5 將單元格中的公式轉換為數值
Excel.Range rng = this.Range["C1:C10"];
rng.Formula = "=sum(A1:B1)";
rng.Value = rng.Value;
14-1 判斷單元格是否存在指注
if (this.Range["A1"].Comment == null)
{
MessageBox.Show("A1單元格中沒有批注");
}
else
{
MessageBox.Show("A1單元格中批注內容為:" + "\n" + this.Range["A1"].Comment.Text());
}
14-2 為單元格添加批注
Excel.Range rng = this.Range["A1"];
if (rng.Comment == null)
{
rng.AddComment(rng.Text);
rng.Comment.Visible = true;
}
14-3 刪除單元格中的批注
Excel.Range rng = this.Range["A1"];
if (rng.Comment != null)
{
rng.Comment.Delete();
}
15-1 判斷單元格區域是否存在合并單元格
Excel.Range rng = this.Application.Selection;
if (Convert.IsDBNull(rng.MergeCells))
{
MessageBox.Show("區域中包含合并單元格!");
}
else if (rng.MergeCells)
{
MessageBox.Show("區域中全部為合并單元格!");
}
else
{
MessageBox.Show("區域中沒有合并單元格!");
}
15-2 合并單元格時連接每個單元格的文本
Excel.Range rng = this.Application.Selection;
string s = string.Empty;
foreach(Excel.Range Rng in rng)
{
s = s + Rng.Text;
}
this.Application.DisplayAlerts = false;
rng.Merge();
rng.Value = s;
this.Application.DisplayAlerts = true;
15-3 合并內容相同的連續單元格
int rEnd = this.Range["A65535"].End[Excel.XlDirection.xlUp].Row;
this.Application.DisplayAlerts = false;
for (int i = rEnd; i >= 2; i--)
{
Excel.Range rng = this.Cells[i, 1];
if (rng.Value == rng.Offset[-1, 0].Value)
{
this.Application.Union(rng, rng.Offset[-1, 0]).Merge();
}
}
15-4 取消合并單元格時在每個單元格中保留內容
int rEnd = this.Range["A65535"].End[Excel.XlDirection.xlUp].Row;
int m = this.Cells[rEnd, 1].MergeArea.Count-1;
this.Range[Cells[1, 1], Cells[rEnd, 1]].UnMerge();
this.Application.DisplayAlerts = false;
for (int i = 1; i < rEnd+m; i++)
{
Excel.Range rng = this.Cells[i, 1];
if (rng.Offset[1, 0].Text == string.Empty)
{
rng.Offset[1, 0].Value = rng.Value;
}
}
|
|