/// <summary> /// Call this to get the running Excel application, or create a new instance /// </summary> /// <returns>IExcelApplication</returns> private static IExcelApplication GetOrCreateExcelApplication() { IExcelApplication excelApplication = COMWrapper.GetOrCreateInstance <IExcelApplication>(); InitializeVariables(excelApplication); return(excelApplication); }
/// <summary> /// Insert image from supplied tmp file into the give excel workbook /// </summary> /// <param name="workbookName"></param> /// <param name="tmpFile"></param> /// <param name="imageSize"></param> public static void InsertIntoExistingWorkbook(string workbookName, string tmpFile, Size imageSize) { using (IExcelApplication excelApplication = GetExcelApplication()) { if (excelApplication == null) { return; } using (IWorkbooks workbooks = excelApplication.Workbooks) { for (int i = 1; i <= workbooks.Count; i++) { using (IWorkbook workbook = workbooks[i]) { if (workbook != null && workbook.Name.StartsWith(workbookName)) { InsertIntoExistingWorkbook(workbook, tmpFile, imageSize); } } } } int hWnd = excelApplication.Hwnd; if (hWnd > 0) { WindowDetails.ToForeground(new IntPtr(hWnd)); } } }
private static void InsertIntoNewWorkbook(IExcelApplication excelApplication, string tmpFile) { LOG.Debug("No workbook, creating a new workbook"); object template = Missing.Value; IWorkbook workbook = excelApplication.Workbooks.Add(template); InsertIntoExistingWorkbook(workbook, tmpFile); }
public static void InsertIntoNewWorkbook(string tmpFile) { using (IExcelApplication excelApplication = COMWrapper.GetOrCreateInstance <IExcelApplication>()) { if (excelApplication != null) { excelApplication.Visible = true; object template = Missing.Value; IWorkbook workbook = excelApplication.Workbooks.Add(template); InsertIntoExistingWorkbook(workbook, tmpFile); } } }
/// <summary> /// Add an image-file to a newly created workbook /// </summary> /// <param name="tmpFile"></param> /// <param name="imageSize"></param> public static void InsertIntoNewWorkbook(string tmpFile, Size imageSize) { using (IExcelApplication excelApplication = GetOrCreateExcelApplication()) { if (excelApplication != null) { excelApplication.Visible = true; object template = Missing.Value; using (IWorkbooks workbooks = excelApplication.Workbooks) { IWorkbook workbook = workbooks.Add(template); InsertIntoExistingWorkbook(workbook, tmpFile, imageSize); } } } }
/// <summary> /// Initialize static outlook variables like version and currentuser /// </summary> /// <param name="excelApplication"></param> private static void InitializeVariables(IExcelApplication excelApplication) { if (excelApplication == null || _excelVersion != null) { return; } try { _excelVersion = new Version(excelApplication.Version); Log.InfoFormat("Using Excel {0}", _excelVersion); } catch (Exception exVersion) { Log.Error(exVersion); Log.Warn("Assuming Excel version 1997."); _excelVersion = new Version((int)OfficeVersion.OFFICE_97, 0, 0, 0); } }
/// <summary> /// Insert image from supplied tmp file into the give excel workbook /// </summary> /// <param name="workbookName"></param> /// <param name="tmpFile"></param> public static void InsertIntoExistingWorkbook(string workbookName, string tmpFile) { using (IExcelApplication excelApplication = COMWrapper.GetInstance <IExcelApplication>()) { if (excelApplication != null) { for (int i = 1; i <= excelApplication.Workbooks.Count; i++) { IWorkbook workbook = excelApplication.Workbooks[i]; if (workbook != null && workbook.Name.StartsWith(workbookName)) { InsertIntoExistingWorkbook(workbook, tmpFile); } } } } }
public static void ExportToExcel(string tmpFile) { using (IExcelApplication excelApplication = ExcelApplication()) { if (excelApplication != null) { excelApplication.Visible = true; if (excelApplication.ActiveWorkbook != null) { InsertIntoExistingWorkbook(excelApplication.ActiveWorkbook, tmpFile); } else { InsertIntoNewWorkbook(excelApplication, tmpFile); } return; } } }
public static List <string> GetWorkbooks() { List <string> currentWorkbooks = new List <string>(); using (IExcelApplication excelApplication = COMWrapper.GetInstance <IExcelApplication>()) { if (excelApplication != null) { for (int i = 1; i <= excelApplication.Workbooks.Count; i++) { IWorkbook workbook = excelApplication.Workbooks[i]; if (workbook != null) { currentWorkbooks.Add(workbook.Name); } } } } return(currentWorkbooks); }
/// <summary> /// Insert image from supplied tmp file into the give excel workbook /// </summary> /// <param name="workbookName"></param> /// <param name="tmpFile"></param> public static void InsertIntoExistingWorkbook(string workbookName, string tmpFile, Size imageSize) { using (IExcelApplication excelApplication = GetExcelApplication()) { if (excelApplication == null) { return; } using (IWorkbooks workbooks = excelApplication.Workbooks) { for (int i = 1; i <= workbooks.Count; i++) { using (IWorkbook workbook = workbooks[i]) { if (workbook != null && workbook.Name.StartsWith(workbookName)) { InsertIntoExistingWorkbook(workbook, tmpFile, imageSize); } } } } } }
/// <summary> /// Get all currently opened workbooks /// </summary> /// <returns>List of string with names of the workbooks</returns> public static List <string> GetWorkbooks() { List <string> currentWorkbooks = new List <string>(); using (IExcelApplication excelApplication = GetExcelApplication()) { if (excelApplication == null) { return(currentWorkbooks); } using (IWorkbooks workbooks = excelApplication.Workbooks) { for (int i = 1; i <= workbooks.Count; i++) { using (IWorkbook workbook = workbooks[i]) { if (workbook != null) { currentWorkbooks.Add(workbook.Name); } } } } } currentWorkbooks.Sort(); return(currentWorkbooks); }
private static void InsertIntoNewWorkbook(IExcelApplication excelApplication, string tmpFile) { LOG.Debug("No workbook, creating a new workbook"); object template = Missing.Value; IWorkbook workbook = excelApplication.Workbooks.Add(template); InsertIntoExistingWorkbook(workbook, tmpFile); }
/// <summary> /// Initialize static outlook variables like version and currentuser /// </summary> /// <param name="excelApplication"></param> private static void InitializeVariables(IExcelApplication excelApplication) { if (excelApplication == null || excelVersion != null) { return; } try { excelVersion = new Version(excelApplication.Version); LOG.InfoFormat("Using Excel {0}", excelVersion); } catch (Exception exVersion) { LOG.Error(exVersion); LOG.Warn("Assuming Excel version 1997."); excelVersion = new Version((int)OfficeVersion.OFFICE_97, 0, 0, 0); } }