我的主要代码如下:
Mtb.Application MtbApp = new Mtb.Application();
MtbApp.UserInterface.Visible = true;
MtbApp.UserInterface.DisplayAlerts = false;
Mtb.Project MtbProj = MtbApp.ActiveProject;
Mtb.Columns MtbColumns;
Mtb.Column MtbColumn1;
Double[] data1;
Hashtable htSingleColumn;
List<double> listSingleColumn;
int i = 1 ;
foreach (DictionaryEntry de in htDataTable)
{
htSingleColumn = (Hashtable)de.Value;
listSingleColumn = (List<double>)htSingleColumn["listSingleData"];
data1 = listSingleColumn.ToArray();
MtbColumns = MtbProj.ActiveWorksheet.Columns;
MtbColumn1 = MtbColumns.Add(null, null, i);
MtbColumn1.SetData(data1);
// strLowlim and strUpplim have no influence on this issue here
strCommand = "Capa C" + i+" 1;" + ((strLowlim == "NA") ? "" : (" Lspec " + strLowlim + ";")) +((strUpplim == "NA") ? "" : (" Uspec " + strUpplim + ";"))+ " Pooled; AMR; UnBiased; OBiased; Toler 6; Within; Percent; CStat.";
// The program is crashing here as a result of the columns not being created sequentially
MtbProj.ExecuteCommand(strCommand);
Mtb.Graph MtbGraph = MtbProj.Commands.Item(i).Outputs.Item(1).Graph;
MtbGraph.SaveAs("C:\\MyGraph" + DateTime.Now.ToString("yyyy-MM-dd HHmmss"), true, Mtb.MtbGraphFileTypes.GFPNGHighColor);
i++;
}
MtbApp.Quit();
运行此代码时(崩溃部分已注释掉),我得到以下输出:
它应该是这样的:
我对这个结果感到很困惑。变量i
是正确的,但影响列号的是什么?
我无法在网上找到有关Minitab的更多信息。我刚刚在这里阅读了start guide。
答案 0 :(得分:3)
这一行是问题所在。
MtbColumn1 = MtbColumns.Add(null, null, i);
第三个参数 Quantity 指定要添加的列数。在循环的第一次迭代中,添加i = 1
列,但在循环的第二次迭代中添加i = 2
列。循环的每次迭代都会添加一个额外的i
列,而您真正想要的是每次添加一列。
将行更改为:
MtbColumn1 = MtbColumns.Add();