|

楼主 |
发表于 2012-7-27 21:05
|
显示全部楼层
接 细品RibbonX(56):使用Visual Studio开发Excel商务应用程序(2)
执行更丰富的计算
对话框启动器dialogBoxLauncher仅有一个onAction属性,因此任何计算的开始点都是该属性指向的方法DisplayRedundantCalc。当然,由于每个等式都不相同,因此都需要采用某种方式单独实现调用。
- Public Sub DisplayRedundantCalc(ByVal control As Office.IRibbonControl)
- ' 选择正确的过程.
- Select Case CalcType
- Case "Loan"
- PerformLoanRangeCalc()
- Case "Annuity"
- PerformAnnuityRangeCalc()
- Case "Effective Rate"
- PerformEffectiveRateRangeCalc()
- End Select
- End Sub
复制代码
代码根据所选择的项目不同,调用不同的对话框计算程序。
- Private Sub PerformLoanRangeCalc()
- ' 创建对话框.
- Dim ThisSelection As LoanRangeSelection = New LoanRangeSelection()
-
- ' 在对话框中添加已存在的变量.
- ThisSelection.txtIntBeg.Text = Rate.ToString()
- ThisSelection.txtIntEnd.Text = Rate.ToString()
- ThisSelection.txtIntInc.Text = "1"
- ThisSelection.cbTermBeg.Text = Term.ToString()
- ThisSelection.cbTermEnd.Text = Term.ToString()
- ThisSelection.txtLoanAmt.Text = Amount.ToString()
-
- ' 显示对话框并且如果用户单击确定则处理数据.
- If ThisSelection.ShowDialog() = DialogResult.OK Then
- ' 转换数据值为Int32egers.
- Rate = Int32.Parse(ThisSelection.txtIntBeg.Text)
- Term = Int32.Parse(ThisSelection.cbTermBeg.Text)
- Amount = Int32.Parse(ThisSelection.txtLoanAmt.Text)
-
- ' 创建本地变量,包括计算数据.
- Dim EndRate As Int32 = Int32.Parse(ThisSelection.txtIntEnd.Text)
- Dim IncRate As Int32 = Int32.Parse(ThisSelection.txtIntInc.Text)
- Dim EndTerm As Int32 = Int32.Parse(ThisSelection.cbTermEnd.Text)
-
- ' 更新功能区中的值.
- ribbon.InvalidateControl("Rate")
- ribbon.InvalidateControl("Term")
- ribbon.InvalidateControl("Amount")
-
- ' 添加初始标题.
- Globals.ThisAddIn.SetHeading("利息", 1, 1)
-
- ' 执行计算.
- Dim i As Int32
- For i = Rate To EndRate
-
- ' 计算X和Y的位置值.
- Dim X As Int32 = i + 2 - Rate
- Dim Y As Int32 = 2
-
- ' 打印Int32erest利率.
- Globals.ThisAddIn.SetHeading(i.ToString() + "%", X, 1)
-
- ' 使用一系列if语句确定年设置.
- If Term = 10 And EndTerm >= 10 Then
- ' 执行计算.
- Globals.ThisAddIn.CalculatePMT(i, 10, Amount, X, Y)
-
- ' 打印标题.
- Globals.ThisAddIn.SetHeading("10年", 1, Y)
-
- ' 如果已经使用则增加Y.
- Y = Y + 1
- End If
-
- If Term <= 15 And EndTerm >= 15 Then
- ' 执行计算.
- Globals.ThisAddIn.CalculatePMT(i, 15, Amount, X, Y)
-
- ' 打印标题.
- Globals.ThisAddIn.SetHeading("15年", 1, Y)
-
- ' 如果已经使用则增加Y.
- Y = Y + 1
- End If
-
- If Term <= 20 And EndTerm >= 20 Then
- ' 执行计算.
- Globals.ThisAddIn.CalculatePMT(i, 20, Amount, X, Y)
-
- ' 打印标题.
- Globals.ThisAddIn.SetHeading("20年", 1, Y)
-
- ' 如果已经使用则增加Y.
- Y = Y + 1
- End If
-
- If Term <= 30 And EndTerm >= 30 Then
- ' 执行计算.
- Globals.ThisAddIn.CalculatePMT(i, 30, Amount, X, Y)
-
- ' 打印标题.
- Globals.ThisAddIn.SetHeading("30年", 1, Y)
-
- ' 如果已经使用则增加Y.
- Y = Y + 1
- End If
- Next
- End If
- End Sub
-
- Private Sub PerformAnnuityRangeCalc()
- ' 创建对话框.
- Dim ThisSelection As AnnuityRangeSelection = New AnnuityRangeSelection()
-
- ' 在对话框中添加已存在的变量.
- ThisSelection.txtIntBeg.Text = Rate.ToString()
- ThisSelection.txtIntEnd.Text = Rate.ToString()
- ThisSelection.txtIntInc.Text = "1"
- ThisSelection.cbTermBeg.Text = Term.ToString()
- ThisSelection.cbTermEnd.Text = Term.ToString()
- ThisSelection.txtLoanAmt.Text = Amount.ToString()
- ThisSelection.txtPayment.Text = Payment.ToString()
-
- ' 显示对话框并且如果用户单击确定则处理数据.
- If ThisSelection.ShowDialog() = DialogResult.OK Then
-
- ' 转换数据值为Int32egers.
- Rate = Int32.Parse(ThisSelection.txtIntBeg.Text)
- Term = Int32.Parse(ThisSelection.cbTermBeg.Text)
- Amount = Int32.Parse(ThisSelection.txtLoanAmt.Text)
- Payment = Int32.Parse(ThisSelection.txtPayment.Text)
-
- ' 创建本地变量以包含计算数据.
- Dim EndRate As Int32 = Int32.Parse(ThisSelection.txtIntEnd.Text)
- Dim IncRate As Int32 = Int32.Parse(ThisSelection.txtIntInc.Text)
- Dim EndTerm As Int32 = Int32.Parse(ThisSelection.cbTermEnd.Text)
-
- ' 更新功能区中的值.
- ribbon.InvalidateControl("Rate")
- ribbon.InvalidateControl("Term")
- ribbon.InvalidateControl("Amount")
- ribbon.InvalidateControl("Payment")
-
- ' 添加初始标题.
- Globals.ThisAddIn.SetHeading("利息", 1, 1)
-
- ' 执行计算.
- Dim i As Int32
- For i = Rate To EndRate
-
- ' 计算X和Y的位置值.
- Dim X As Int32 = i + 2 - Rate
- Dim Y As Int32 = 2
-
- ' 打印Int32erest利率.
- Globals.ThisAddIn.SetHeading(i.ToString() + "%", X, 1)
-
- ' 使用一系列if语句决定年设置.
- If Term = 5 And EndTerm >= 5 Then
- ' 执行计算.
- Globals.ThisAddIn.CalculateFV(i, 5, Amount, Payment, X, Y)
-
- ' 打印标题.
- Globals.ThisAddIn.SetHeading("5年", 1, Y)
-
- ' 如果已经使用则增加Y.
- Y = Y + 1
- End If
-
- If Term <= 7 And EndTerm >= 7 Then
- ' 执行计算.
- Globals.ThisAddIn.CalculateFV(i, 7, Amount, Payment, X, Y)
-
- ' 打印标题.
- Globals.ThisAddIn.SetHeading("7年", 1, Y)
-
- ' 如果已经使用则增加Y.
- Y = Y + 1
- End If
-
- If Term <= 10 And EndTerm >= 10 Then
- ' 执行计算.
- Globals.ThisAddIn.CalculateFV(i, 10, Amount, Payment, X, Y)
-
- ' 打印标题.
- Globals.ThisAddIn.SetHeading("10年", 1, Y)
-
- ' 如果已经使用则增加Y.
- Y = Y + 1
- End If
-
- If Term <= 15 And EndTerm >= 15 Then
- ' 执行计算.
- Globals.ThisAddIn.CalculateFV(i, 15, Amount, Payment, X, Y)
-
- ' 打印标题.
- Globals.ThisAddIn.SetHeading("15年", 1, Y)
-
- ' 如果已经使用则增加Y.
- Y = Y + 1
- End If
-
- If Term <= 20 And EndTerm >= 20 Then
- ' 执行计算.
- Globals.ThisAddIn.CalculateFV(i, 20, Amount, Payment, X, Y)
-
- ' 打印标题.
- Globals.ThisAddIn.SetHeading("20年", 1, Y)
-
- ' 如果已经使用则增加Y.
- Y = Y + 1
- End If
- Next
- End If
- End Sub
-
- Private Sub PerformEffectiveRateRangeCalc()
- ' 创建对话框.
- Dim ThisSelection As EffectiveRateRangeSelection = New EffectiveRateRangeSelection()
-
- ' 在对话框中添加已存在的变量.
- ThisSelection.txtIntBeg.Text = Rate.ToString()
- ThisSelection.txtIntEnd.Text = Rate.ToString()
- ThisSelection.txtIntInc.Text = "1"
-
- ' 显示对话框并且如果用户单击确定则处理数据.
- If ThisSelection.ShowDialog() = DialogResult.OK Then
-
- ' 转换数据值为Int32egers.
- Rate = Int32.Parse(ThisSelection.txtIntBeg.Text)
-
- ' 创建本地变量以包含计算数据.
- Dim EndRate As Int32 = Int32.Parse(ThisSelection.txtIntEnd.Text)
- Dim IncRate As Int32 = Int32.Parse(ThisSelection.txtIntInc.Text)
-
- ' 更新功能区中的值.
- ribbon.InvalidateControl("Rate")
-
- ' 添加初始标题.
- Globals.ThisAddIn.SetHeading("利息", 1, 1)
- Globals.ThisAddIn.SetHeading("有效利率", 1, 2)
-
- ' 执行计算.
- Dim i As Int32
- For i = Rate To EndRate
- ' 计算X和Y位置值.
- Dim X As Int32 = i + 2 - Rate
-
- ' 打印Int32erest利率.
- Globals.ThisAddIn.SetHeading(i.ToString() + "%", X, 1)
-
- ' 执行计算.
- Globals.ThisAddIn.CalculateEFFECT(i, X, 2)
- Next
- End If
- End Sub
复制代码
考虑数据识别需求
对输出结果添加有意义的标题。当执行多重计算时,为使数据意义明确,必须提供标题。代码如下:
- Public Sub SetHeading(ByVal Heading As String, ByVal X As Int32, ByVal Y As Int32)
- ' 添加所需要的标题.
- Application.ActiveWindow.ActiveCell.Cells(X, Y) = Heading
- End Sub
复制代码
现在,一切准备就绪。运行代码,在窗体中选择区域,输入相应数据,得到的输出结果如下图所示。

|
评分
-
1
查看全部评分
-
|