public void SetVariables() { findText = textBox1.Text; replaceText = textBox2.Text; lookat = (comboBox1.SelectedIndex == 0) ? Excel.XlLookAt.xlPart : Excel.XlLookAt.xlWhole; searchOrder = (comboBox2.SelectedIndex == 0) ? Excel.XlSearchOrder.xlByRows : Excel.XlSearchOrder.xlByColumns; matchCase = checkBox1.Checked; }
/// <summary> /// 构造函数 /// </summary> /// <param name="PointTag">文档中要进行提取的测点标签</param> /// <param name="Offset">测点所对应的数据距离测点单元格的水平偏移位置。 /// 如数据单元格是在测点标签单元格的左边且紧靠标签单元格,则Offset的值为+1。</param> /// <param name="SearchOrder">进行搜索的方向:按行或者按列,即下一个搜索单元格是按行还是按列的方向前进。</param> /// <remarks></remarks> public PointsInfoForExport(string PointTag, int Offset, Microsoft.Office.Interop.Excel.XlSearchOrder SearchOrder) { this.SearchOrder = SearchOrder; this.PointTag = PointTag; this.Offset = (byte)Offset; // this.ColNumToBeWritten = cstColNum_FirstData; this.RowNumToBeWritten = cstRowNum_FirstData; this.ColumnsCountToBeAdd = cstColumnsCountToBeAdded; }
/// <summary> /// Find last row or column in worksheet /// </summary> /// <param name="excelWorksheet"> /// Worksheet for analuze /// </param> /// <param name="searchOrder"> /// By Row or by Column /// </param> /// <returns> /// Cell /// </returns> private dynamic GetCount(ExcelObject.Worksheet excelWorksheet, ExcelObject.XlSearchOrder searchOrder) { return(excelWorksheet.Cells.Find( "*", System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, searchOrder, ExcelObject.XlSearchDirection.xlPrevious, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value )); }
private Dictionary <Worksheet, PointsInfoForExport> SearchPointsInfo(Microsoft.Office.Interop.Excel.Workbook wkbk) { Dictionary <Worksheet, PointsInfoForExport> listRangeInfo = new Dictionary <Worksheet, PointsInfoForExport>(); string strTestRange = ""; // //记录DataGridView控件中所有数据的数组 try { int RowsCount = this.MyDataGridView1.Rows.Count; for (int RowIndex = 0; RowIndex <= RowsCount - 2; RowIndex++) { DataGridViewRow RowObject = MyDataGridView1.Rows[RowIndex]; //要进行提取的测点标签 string strPointName = RowObject.Cells[0].Value.ToString(); //设置与测点标签对应的excel工作表对象,并为其命名 Worksheet sht = null; try { sht = wkbk.Worksheets.Item[strPointName] as Worksheet; } catch (Exception) { //表示工作簿中没有这一工作表 sht = wkbk.Worksheets.Add() as Worksheet; //为新创建的工作表命名 bool blnNameOk = false; var shtName = strPointName; do { try { sht.Name = shtName; blnNameOk = true; } catch (Exception) { //表示此名称已经在工作簿中被使用了 shtName = shtName + "1"; } } while (!blnNameOk); } //测点数据距离测点标签的偏移位置 byte Offset = byte.Parse(RowObject.Cells[1].Value.ToString()); //搜索的方向:按行或者是按列 Microsoft.Office.Interop.Excel.XlSearchOrder SearchDirection = default(Microsoft.Office.Interop.Excel.XlSearchOrder); DataGridViewComboBoxCell comboBox = (DataGridViewComboBoxCell)(RowObject.Cells[2]); if ((string)comboBox.Value == "按行") { SearchDirection = XlSearchOrder.xlByRows; } else if ((string)comboBox.Value == "按列") { SearchDirection = XlSearchOrder.xlByColumns; } else { MessageBox.Show("请先输入搜索方向", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(default(Dictionary <Worksheet, PointsInfoForExport>)); } PointsInfoForExport RangeInfo = new PointsInfoForExport(strPointName, Offset, SearchDirection); listRangeInfo.Add(sht, RangeInfo); } } catch (Exception) { MessageBox.Show("数据的格式出错 : " + "\r\n" + strTestRange + ",请重新输入", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(default(Dictionary <Worksheet, PointsInfoForExport>)); } return(listRangeInfo); }