Exemplo n.º 1
0
        /// <summary>
        /// Excelの全てのシートにスタンプを貼付け
        /// </summary>
        /// <param name="ExcelWorkbooks">対象のExcel</param>
        /// <param name="imageFilePath">貼付けるスタンプの画像ファイルパス</param>
        /// <param name="stampWidth">補正する画像の横幅</param>
        /// <param name="stampHeight">補正する画像の縦幅</param>
        private void AddStampPicture(ref Excel.Workbook ExcelWorkbooks, string imageFilePath, float stampWidth, float stampHeight, string stampName)
        {
            foreach (var sheet in ExcelWorkbooks.Sheets)
            {
                Excel.Worksheet workSheet = (Excel.Worksheet)sheet;

                // 画像貼付処理
                Excel.Shapes excelShapes = (Excel.Shapes)workSheet.Shapes;
                Excel.Shape  stampShape  = excelShapes.AddPicture(imageFilePath,
                                                                  Microsoft.Office.Core.MsoTriState.msoFalse,
                                                                  Microsoft.Office.Core.MsoTriState.msoTrue,
                                                                  0,
                                                                  0,
                                                                  (float)stampWidth,
                                                                  (float)stampHeight);

                // 貼付けた画像のオブジェクト名を設定
                stampShape.Name = stampName;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Add a picture file named <paramref name="pictureFileName"/> on <paramref name="cell"/> and name <paramref name="name"/>.
        /// </summary>
        /// <param name="cell">The cell to add a picture.</param>
        /// <param name="pictureFileName">File name of the picture.</param>
        /// <param name="name">Excel name of the picture.</param>
        public static void AddPicture(Excel.Range cell, string pictureFileName, string name)
        {
            cell.Activate();
            dynamic sheet = Globals.ThisAddIn.Application.ActiveSheet;

            Excel.Shapes shapes = sheet.Shapes;
            Excel.Shape  shape  = shapes.AddPicture(
                pictureFileName,
                Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue,
                (float)cell.Left, (float)cell.Top, (float)cell.Width, (float)cell.Height);
            shape.Placement = Excel.XlPlacement.xlMove;
            shape.Name      = name;

            // if you want to fit pictures with each cell, delte the following lines.
            shape.ScaleHeight(1, Office.MsoTriState.msoTrue);
            shape.ScaleWidth(1, Office.MsoTriState.msoTrue);

            var   xyRatioCell  = (float)cell.Width / (float)cell.Height;
            var   xyRatioShape = shape.Width / shape.Height;
            float scale        = 1;

            if (xyRatioCell > xyRatioShape)
            {
                // fit to cell.Height
                scale = (float)cell.Height / shape.Height;
            }
            else
            {
                // fit to cell.Width;
                scale = (float)cell.Width / shape.Width;
            }

            shape.ScaleHeight(scale, Office.MsoTriState.msoTrue);
            shape.ScaleWidth(scale, Office.MsoTriState.msoTrue);
            shape.LockAspectRatio = Office.MsoTriState.msoTrue;
        }
        PopulateAreaWithSubgraphImages
        (
            Range oNameColumnArea,
            Range oSubgraphImageColumnArea,
            SizeF oSubgraphImageSizePt,

            Dictionary <String, Microsoft.Office.Interop.Excel.Shape>
            oOldSubgraphImages,

            TemporaryImages oTemporarySubgraphImages
        )
        {
            Debug.Assert(oNameColumnArea != null);
            Debug.Assert(oSubgraphImageColumnArea != null);
            Debug.Assert(oOldSubgraphImages != null);
            Debug.Assert(oTemporarySubgraphImages != null);

            // Gather some required information.

            Int32 iRows = oNameColumnArea.Rows.Count;

            Debug.Assert(iRows == oSubgraphImageColumnArea.Rows.Count);

            Debug.Assert(oNameColumnArea.Parent is Worksheet);

            Worksheet oWorksheet = (Worksheet)oNameColumnArea.Parent;

            Microsoft.Office.Interop.Excel.Shapes oShapes = oWorksheet.Shapes;

            Object [,] aoNameValues = ExcelUtil.GetRangeValues(oNameColumnArea);

            Dictionary <String, String> oFileNames =
                oTemporarySubgraphImages.FileNames;

            // Set the row heights to fit the images.

            oNameColumnArea.RowHeight =
                oSubgraphImageSizePt.Height + 2 * SubgraphImageMarginPt;

            // Get the first cell in the subgraph image column.

            Range oSubgraphImageCell = (Range)oSubgraphImageColumnArea.Cells[1, 1];

            // Loop through the area's rows.

            for (Int32 iRow = 1; iRow <= iRows; iRow++)
            {
                String sName, sFileName;

                // Check whether the row's name cell has a corresponding file name
                // in the dictionary.

                if (
                    ExcelUtil.TryGetNonEmptyStringFromCell(aoNameValues, iRow, 1,
                                                           out sName)
                    &&
                    oFileNames.TryGetValue(sName, out sFileName)
                    )
                {
                    // Give the picture a name that can be recognized by
                    // GetSubgraphImageDictionary().

                    String sPictureName =
                        VertexTableColumnNames.SubgraphImage + "-" + sName;

                    Microsoft.Office.Interop.Excel.Shape oPicture;

                    // If an old version of the picture remains from a previous
                    // call to this method, delete it.

                    if (oOldSubgraphImages.TryGetValue(sPictureName,
                                                       out oPicture))
                    {
                        oPicture.Delete();
                    }

                    String sFileNameWithPath = Path.Combine(
                        oTemporarySubgraphImages.Folder, sFileName);

                    oPicture = oShapes.AddPicture(sFileNameWithPath,
                                                  MsoTriState.msoFalse, MsoTriState.msoCTrue,

                                                  (Single)(Double)oSubgraphImageCell.Left
                                                  + SubgraphImageMarginPt,

                                                  (Single)(Double)oSubgraphImageCell.Top
                                                  + SubgraphImageMarginPt,

                                                  oSubgraphImageSizePt.Width,
                                                  oSubgraphImageSizePt.Height
                                                  );

                    oPicture.Name = sPictureName;
                }

                // Move down one cell in the image column.

                oSubgraphImageCell = oSubgraphImageCell.get_Offset(1, 0);
            }
        }
Exemplo n.º 4
0
        public void insertDocument(string relativePath, string nodeRef)
        {
            object missingValue = Type.Missing;
            object trueValue    = true;
            object falseValue   = false;

            try
            {
                // Suppress all app events during this process
                m_SuppressAppEvents = true;

                // Create a new document if no document currently open
                if (m_ExcelApplication.ActiveWorkbook == null)
                {
                    m_ExcelApplication.Workbooks.Add(missingValue);
                }

                // WebDAV or CIFS?
                string strFullPath = m_ServerDetails.getFullPath(relativePath, m_ExcelApplication.ActiveWorkbook.FullName, true);
                string strExtn     = Path.GetExtension(relativePath).ToLower();
                string fileName    = Path.GetFileName(relativePath);
                string nativeExtn  = ".xls" + (m_SupportsXlsx ? "x" : "");

                // If we're using WebDAV, then download file locally before inserting
                if (strFullPath.StartsWith("http"))
                {
                    // Need to unescape the the original path to get the filename
                    fileName = Path.GetFileName(Uri.UnescapeDataString(relativePath));
                    string strTempFile = Path.GetTempPath() + fileName;
                    if (File.Exists(strTempFile))
                    {
                        try
                        {
                            File.Delete(strTempFile);
                        }
                        catch (Exception)
                        {
                            strTempFile = Path.GetTempFileName();
                        }
                    }
                    WebClient fileReader = new WebClient();
                    string    url        = m_ServerDetails.WebClientURL + "download/direct/" + nodeRef.Replace(":/", "") + "/" + Path.GetFileName(relativePath);
                    fileReader.Headers.Add("Cookie: " + webBrowser.Document.Cookie);
                    fileReader.DownloadFile(url, strTempFile);
                    strFullPath = strTempFile;
                }

                Excel.Worksheet worksheet = (Excel.Worksheet)m_ExcelApplication.ActiveSheet;
                Excel.Shapes    shapes    = worksheet.Shapes;
                Excel.Range     range     = (Excel.Range)m_ExcelApplication.Selection;
                object          top       = range.Top;
                object          left      = range.Left;

                if (".bmp .gif .jpg .jpeg .png".IndexOf(strExtn) != -1)
                {
                    Excel.Shape picture = shapes.AddPicture(strFullPath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue,
                                                            1, 2, 3, 4);
                    picture.Top  = Convert.ToSingle(range.Top);
                    picture.Left = Convert.ToSingle(range.Left);
                    picture.ScaleWidth(1, Microsoft.Office.Core.MsoTriState.msoTrue, missingValue);
                    picture.ScaleHeight(1, Microsoft.Office.Core.MsoTriState.msoTrue, missingValue);
                }
                else if (nativeExtn.IndexOf(strExtn) != -1)
                {
                    // Workaround for KB210684 if clean, new workbook currently open
                    if (!docHasExtension() && m_ExcelApplication.ActiveWorkbook.Saved)
                    {
                        string workbookName = m_ExcelApplication.ActiveWorkbook.Name;
                        string templateName = Path.GetDirectoryName(strFullPath) + "\\" + workbookName + "." + Path.GetFileNameWithoutExtension(strFullPath);
                        m_ExcelApplication.ActiveWorkbook.Close(falseValue, missingValue, missingValue);
                        File.Move(strFullPath, templateName);
                        m_ExcelApplication.Workbooks.Add(templateName);
                    }
                    else
                    {
                        // Load the workbook
                        object         neverUpdateLinks = 2;
                        object         readOnly         = true;
                        Excel.Workbook insertXls        = m_ExcelApplication.Workbooks.Open(strFullPath, neverUpdateLinks, readOnly, missingValue, missingValue,
                                                                                            missingValue, missingValue, missingValue, missingValue, missingValue, missingValue, missingValue, missingValue,
                                                                                            missingValue, missingValue);

                        if (insertXls != null)
                        {
                            try
                            {
                                // Loop backwards through the worksheets copy-pasting them after the active sone
                                Excel.Worksheet sourceSheet;
                                for (int i = insertXls.Worksheets.Count; i > 0; i--)
                                {
                                    sourceSheet = (Excel.Worksheet)insertXls.Worksheets[i];
                                    sourceSheet.Copy(missingValue, worksheet);
                                }
                            }
                            catch (Exception e)
                            {
                                MessageBox.Show(Properties.Resources.UnableToInsert + ": " + e.Message, Properties.Resources.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                            finally
                            {
                                insertXls.Close(falseValue, missingValue, missingValue);
                            }
                        }
                    }
                }
                else
                {
                    object iconFilename = Type.Missing;
                    object iconIndex    = Type.Missing;
                    object iconLabel    = fileName;
                    string defaultIcon  = Util.DefaultIcon(strExtn);
                    if (defaultIcon.Contains(","))
                    {
                        string[] iconData = defaultIcon.Split(new char[] { ',' });
                        iconFilename = iconData[0];
                        iconIndex    = iconData[1];
                    }
                    object      filename = strFullPath;
                    object      size     = 32;
                    Excel.Shape shape    = shapes.AddOLEObject(missingValue, filename, falseValue, trueValue,
                                                               iconFilename, iconIndex, iconLabel, left, top, size, size);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(Properties.Resources.UnableToInsert + ": " + e.Message, Properties.Resources.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                // Restore app event processing
                m_SuppressAppEvents = false;
            }
        }