Ejemplo n.º 1
0
 /// <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));
         }
     }
 }
Ejemplo n.º 2
0
 /// <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);
             }
         }
     }
 }
Ejemplo n.º 3
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, 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);
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 4
0
        /// <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);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Customization for Pasting the data to grid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void gridControl1_ClipboardCanPaste(object sender, GridCutPasteEventArgs e)
        {
            IDataObject dataObject = Clipboard.GetDataObject();

            if (dataObject != null)
            {
                if (dataObject.GetDataPresent("Biff", true))
                {
                    ExcelEngine  xlEngine = new ExcelEngine();
                    IApplication app      = xlEngine.Excel;
                    IWorkbooks   workBook = app.Workbooks;
                    workBook.PasteWorkbook();
                    IWorkbook  book      = workBook[0];
                    IWorksheet worksheet = book.Worksheets[0];

                    IRange ur = worksheet.UsedRange;

                    IRange[] ranges = ur.Cells;//worksheet.UsedCells;
                    System.Text.StringBuilder str = new System.Text.StringBuilder();



                    int lastCol = ranges[0].Column, lastRow = ranges[0].Row;
                    int rc = ur.LastRow - ur.Row + 1;
                    int cc = ur.LastColumn - ur.Column + 1;

                    GridExcelConverterControl converter = new GridExcelConverterControl();
                    GridModel model = new GridModel();

                    model.ColCount = cc;
                    model.RowCount = rc;
                    ur.MoveTo(worksheet.Range[1, 1, rc, cc]);
                    ranges = worksheet.Range[1, 1, rc, cc].Cells;
                    foreach (IRange cell in ranges)
                    {
                        converter.ConvertExcelRangeToGrid(cell, model);
                    }

                    GridData data = new GridData();
                    data.InsertRows(1, rc);
                    data.InsertCols(1, cc);

                    for (int i = 1; i <= model.Data.RowCount; i++)
                    {
                        for (int j = 1; j <= model.ColCount; j++)
                        {
                            if (model.Data[i, j] != null)
                            {
                                data[i - 1, j - 1] = model.Data[i, j].Clone() as GridStyleInfoStore;
                            }
                        }
                    }

                    //Clipboard.Clear();
                    DataObject newDataObject = new DataObject(data);
                    Clipboard.SetDataObject(newDataObject, true);
                    xlEngine.ThrowNotSavedOnDestroy = false;
                    xlEngine.Dispose();
                }
            }
        }
Ejemplo n.º 6
0
        public static IWorkbook Open(this IWorkbooks workbook, string fileName)
        {
            FileStream f = File.Open(fileName, FileMode.Open);

            return(workbook.Open(f));
        }