/// <summary> /// Retruns nid only if name exists in the database /// </summary> /// <param name="name"></param> /// <param name="parentNid"></param> /// <param name="type"></param> /// <returns></returns> private int GetNidByName(string name, int parentNid, ICType type) { int RetVal = 0; string SqlQuery = string.Empty; DataTable TempTable; DataRow[] Rows; try { SqlQuery = this.DBQueries.IndicatorClassification.GetIC(FilterFieldType.Name, "'" + name + "'", type, FieldSelection.NId); TempTable = this.DBConnection.ExecuteDataTable(SqlQuery); if (TempTable.Rows.Count > 0) { Rows = TempTable.Select(IndicatorClassifications.ICParent_NId + "=" + parentNid); RetVal = Convert.ToInt32(Rows[0][IndicatorClassifications.ICNId]); } } catch (Exception) { RetVal = 0; } return(RetVal); }
/// <summary> /// 刷新当前域 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void UpdateOneLink_Click(object sender, RibbonControlEventArgs e) { WordApp = Globals.ThisAddIn.Application; WordDoc = WordApp.ActiveDocument; Word.Field TempField; Word.Table TempTable; int WordRows = 0; int WordColumns = 0; int ExcelRows = 0; int ExcelColumns = 0; string TempStr; //如果选中的区域不包含表格则退出 if (WordApp.Selection.Tables.Count == 0) { return; } //如果选中的区域不在域中则退出 if (!WordApp.Selection.Information[Word.WdInformation.wdInFieldResult]) { return; } WordApp.ScreenUpdating = false;//关闭屏幕刷新 //选中整个表格 TempTable = WordApp.Selection.Tables[1]; TempTable.Select(); //获取Word中表格行列数 WordRows = TempTable.Rows.Count; WordColumns = TempTable.Columns.Count; //获取域 WordApp.Selection.Next(Word.WdUnits.wdWord, 2).Select(); WordApp.Selection.PreviousField(); TempField = WordApp.Selection.Fields[1]; TempStr = FunC.LinkPath(TempField.Code.Text); //检查文件是否存在 if (!File.Exists(TempStr)) { MessageBox.Show("未发现域链接的Excel文件,请检查!"); } Excel.Application ExcelApp = null; Excel.Workbook WBK = null; Excel.Worksheet WST = null; //获取区域的行列数 if (FunC.IsFileInUse(TempStr))//如果目标文件已被打开 { try { ExcelApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application"); WBK = ExcelApp.Workbooks[Path.GetFileName(TempStr)]; } catch { MessageBox.Show("链接的Excel文件已被后台程序打开,请检查并清理后台程序"); return; } //如果没有工作表 if (!FunC.SheetExist(WBK, FunC.LinkSheet(TempField.Code.Text))) { MessageBox.Show("链接的Excel文件中未发现该工作表,请检查"); return; } WST = WBK.Worksheets[FunC.LinkSheet(TempField.Code.Text)]; try { ExcelRows = WST.Range[FunC.LinkArea(TempField.Code.Text)].Rows.Count; ExcelColumns = WST.Range[FunC.LinkArea(TempField.Code.Text)].Columns.Count; } catch { MessageBox.Show("链接的Excel文件中未发现该域的命名区域,请检查"); return; } } else { bool QuitExcel; try { ExcelApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application"); QuitExcel = false; } catch { //弹出窗体提示 DialogResult IsWait = MessageBox.Show("当前版本插件建议打开Excel程序再刷新域" + Environment.NewLine + "否则可能需要清理后台Excel程序,是否继续?", "请选择", MessageBoxButtons.YesNo); if (IsWait != DialogResult.Yes) { WordApp.ScreenUpdating = true; return; } ExcelApp = new Excel.Application(); ExcelApp.Visible = false; QuitExcel = true; } WBK = ExcelApp.Workbooks.Open(TempStr); //如果没有工作表 if (!FunC.SheetExist(WBK, FunC.LinkSheet(TempField.Code.Text))) { MessageBox.Show("链接的Excel文件中未发现该工作表,请检查"); return; } WST = WBK.Worksheets[FunC.LinkSheet(TempField.Code.Text)]; try { ExcelRows = WST.Range[FunC.LinkArea(TempField.Code.Text)].Rows.Count; ExcelColumns = WST.Range[FunC.LinkArea(TempField.Code.Text)].Columns.Count; } catch { MessageBox.Show("链接的Excel文件中未发现该域的命名区域,请检查"); return; } WBK.Close(); WBK = null; WST = null; //退出Excel程序 if (QuitExcel) { ExcelApp.Visible = true; ExcelApp.Quit(); ExcelApp = null; } } int TempInt = WordRows - ExcelRows; //检查行数 if (TempInt < 0) { for (int i = 1; i <= Math.Abs(TempInt); i++) { TempTable.Cell(Math.Max(WordRows - 1, 1), 1).Range.Rows.Add(TempTable.Cell(Math.Max(WordRows - 1, 1), 1).Range.Rows); } } else if (TempInt > 0) { for (int i = TempInt; i >= 1; i--) { TempTable.Cell(Math.Max(WordRows - i, 1), 1).Range.Rows.Delete(); } } TempInt = WordColumns - ExcelColumns; //检查列数 if (TempInt < 0) { for (int i = 1; i <= Math.Abs(TempInt); i++) { TempTable.Cell(ExcelRows, Math.Max(WordColumns - 2, 1)).Range.Columns.Add(TempTable.Cell(ExcelRows, Math.Max(WordColumns - 2, 1)).Range.Columns); } } else if (TempInt > 0) { for (int i = TempInt; i >= 1; i--) { TempTable.Cell(ExcelRows, Math.Max(WordColumns - 1, 1)).Range.Columns.Delete(); } } TempField.Update(); TempField.Select(); GC.Collect(); WordApp.ScreenUpdating = true;//打开屏幕刷新 }