//------------------------------------------------------------------------- /* Get workbook from opened workbook */ private void ddbOpened_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e) { int idx = ddbOpened.DropDownItems.IndexOf(e.ClickedItem) - 2; if (idx < 0) { return; } try { if (string.IsNullOrEmpty(openedBooks[idx].Item2)) { tbNameXls.Text = openedBooks[idx].Item1; } else { tbNameXls.Text = Path.Combine(openedBooks[idx].Item2, openedBooks[idx].Item1); } CurrWs = null; Excel.Workbook wb = ExcelProc.GetWorkbook(openedBooks[idx].Item1, openedBooks[idx].Item2, true, true); //ShowWorkbook(wb); SetForegroundWindow((ParentView as Form).Handle); FillSheetsList(wb); CurrWs = ExcelProc.GetSheet(wb, cboxSheets.Text); ExcelProc.ReleaseCom(ref wb); } catch (Exception ex) { OnError(ex); } }
//------------------------------------------------------------------------- /* Get data from sheet */ void GetExcelData(bool fieldsOnly = false) { Excel.Workbook wb = null; Excel.Worksheet ws = null; Excel.Range rg = null; try { wb = ExcelProc.GetWorkbook(Path.GetFileName(Filename), Path.GetDirectoryName(Filename), true, false); ws = ExcelProc.GetSheet(wb, Sheet); if (ws == null) { throw new Exception(Parent.Name + ": excel sheet \"" + Sheet + "\" not exist"); } rg = GetRange(ws); SheetToTable(ws, rg, fieldsOnly); if (Parent.DT != null) { Parent.DT.TableName = Parent.Name; } } finally { if (wb != null && !wb.Application.Visible) { wb.Application.Quit(); } ExcelProc.ReleaseCom(ref wb); ExcelProc.ReleaseCom(ref ws); ExcelProc.ReleaseCom(ref rg); } }
//------------------------------------------------------------------------- /* Get workbook from file */ private void SelectExcelFromFile(object sender, EventArgs e) { if (Command == null || !(bool)Command("SelectFileExcel", null)) { return; } ((Form)ParentView).Cursor = Cursors.WaitCursor; try { CurrWs = null; Excel.Workbook wb = ExcelProc.GetWorkbook(Path.GetFileName(tbNameXls.Text), Path.GetDirectoryName(tbNameXls.Text), false, false); FillSheetsList(wb); if (wb != null && !wb.Application.Visible) { wb.Application.Quit(); } ExcelProc.ReleaseCom(ref wb); } catch (Exception ex) { OnError(ex); } finally { ((Form)ParentView).Cursor = Cursors.Default; } }
//------------------------------------------------------------------------- private void bExcel_Click(object sender, EventArgs e) { try { ExcelProc.GridToExcel(tabs.SelectedTab == tabResult ? dgResult : dgFields, maxExcelRows); } catch (Exception ex) { ShowMess(ex.ToString(), true); } }
//------------------------------------------------------------------------- private void cmsResult_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { if (e.ClickedItem == miExcel) { try { ExcelProc.GridToExcel(dgResult, ROWS_TO_XLS); } catch (Exception ex) { MessageBox.Show(string.Format("Send to Excel error: {0}", ex.Message), "Excel error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
//------------------------------------------------------------------------- /* Open workbook */ private void bOpenXls_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(tbNameXls.Text)) { return; } try { CurrWs = null; Excel.Workbook wb = ExcelProc.GetWorkbook(Path.GetFileName(tbNameXls.Text), Path.GetDirectoryName(tbNameXls.Text), true, true); ShowWorkbook(wb); FillSheetsList(wb); CurrWs = ExcelProc.GetSheet(wb, cboxSheets.Text); ExcelProc.ReleaseCom(ref wb); } catch (Exception ex) { OnError(ex); } }
//------------------------------------------------------------------------- /* Change sheet */ private void cboxSheets_SelectedIndexChanged(object sender, EventArgs e) { if (!canConnectToSheet) { return; } try { CurrWs = null; Excel.Workbook wb = ExcelProc.GetOpenedWorkbook(Path.GetFileName(tbNameXls.Text), Path.GetDirectoryName(tbNameXls.Text)); if (wb != null) { CurrWs = ExcelProc.GetSheet(wb, cboxSheets.Text); ExcelProc.ReleaseCom(ref wb); } } catch (Exception ex) { OnError(ex); } }
//------------------------------------------------------------------------- /* List of opened workbooks */ public void ddbOpened_DropDownOpening(object sender, EventArgs e) { openedBooks.Clear(); ddbOpened.DropDownItems.Clear(); ddbOpened.DropDownItems.Add("From file...", Properties.Resources.selectfile, SelectExcelFromFile); try { List <Tuple <string, string> > wbooks = ExcelProc.GetOpenedXlsNames(); if (wbooks.Count > 0) { ddbOpened.DropDownItems.Add(new ToolStripSeparator()); for (int i = 0; i < wbooks.Count; i++) { openedBooks.Add(wbooks[i]); ddbOpened.DropDownItems.Add(wbooks[i].Item1 + (string.IsNullOrEmpty(wbooks[i].Item2) ? "" : " (" + wbooks[i].Item2 + ")")); } } } catch (Exception ex) { OnError(ex); } }