Esempio n. 1
0
        /// <summary>
        /// Définit la valeur à l'emplacement spécifié d'une cellule.
        /// </summary>
        /// <param name="part">La feuille.</param>
        /// <param name="cellReference">La référence vers la cellule.</param>
        /// <param name="dataType">Le type de la donnée.</param>
        /// <param name="data">La donnée.</param>
        public void SetCellValue(WorksheetPart part, CellReference cellReference, CellContent content)
        {
            var cell = InsertCellInWorksheet(part, cellReference);

            FormatCellContent(cell, content);
        }
Esempio n. 2
0
        /// <summary>
        /// Ajoute une image dans une feuille.
        /// </summary>
        /// <param name="worksheetPart">La feuille.</param>
        /// <param name="image">Les données de l'image.</param>
        /// <param name="imageType">Le type de l'image.</param>
        /// <param name="filename">Le nom du fichier.</param>
        /// <param name="pictureName">Le nom de l'image.</param>
        /// <param name="dimensions">Les dimensions de l'image, en pixels.</param>
        /// <param name="position">La position de l'image dans la feuille.</param>
        /// <returns>Le nombre de ligne que l'image a prise.</returns>
        public uint AddImage(WorksheetPart worksheetPart, byte[] image, ImagePartType imageType, string filename, string pictureName, System.Windows.Size dimensions, CellReference position)
        {
            if (worksheetPart.DrawingsPart == null)
            {
                worksheetPart.AddNewPart <DrawingsPart>();
            }

            var imageId = "rId" + Guid.NewGuid().ToString("N");

            ImagePart imgp = worksheetPart.DrawingsPart.AddImagePart(imageType, imageId);

            using (var ms = new MemoryStream(image))
            {
                imgp.FeedData(ms);
            }

            uint fromCol       = position.ColumnIndex - 1;
            int  fromColOffset = 0;
            uint fromRow       = position.RowIndex - 1;
            int  fromRowOffset = 0;

            int widthEmu  = PixelsToEmus(dimensions.Width);
            int heightEmu = PixelsToEmus(dimensions.Height);

            string oneCellAnchor = @"<xdr:oneCellAnchor xmlns:xdr='http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing' xmlns:a='http://schemas.openxmlformats.org/drawingml/2006/main'>
    <xdr:from>
      <xdr:col>" + fromCol + @"</xdr:col>
      <xdr:colOff>" + fromColOffset + @"</xdr:colOff>
      <xdr:row>" + fromRow + @"</xdr:row>
      <xdr:rowOff>" + fromRowOffset + @"</xdr:rowOff>
    </xdr:from>    
    <xdr:ext cx='" + widthEmu + "' cy='" + heightEmu + @"' />
    <xdr:pic>
      <xdr:nvPicPr>
        <xdr:cNvPr id='4' name='" + pictureName + @"'/>
        <xdr:cNvPicPr>
          <a:picLocks noChangeAspect='1'/>
        </xdr:cNvPicPr>
      </xdr:nvPicPr>
      <xdr:blipFill>
        <a:blip xmlns:r='http://schemas.openxmlformats.org/officeDocument/2006/relationships' r:embed='" + imageId + @"' cstate='print'>
          <a:extLst>
            <a:ext uri='{28A0092B-C50C-407E-A947-70E740481C1C}'>
              <a14:useLocalDpi xmlns:a14='http://schemas.microsoft.com/office/drawing/2010/main' val='0'/>
            </a:ext>
          </a:extLst>
        </a:blip>
        <a:stretch>
          <a:fillRect/>
        </a:stretch>
      </xdr:blipFill>
      <xdr:spPr>
        <a:xfrm>
          <a:off x='0' y='0'/>
          <a:ext cx='0' cy='0'/>
        </a:xfrm>
        <a:prstGeom prst='rect'>
          <a:avLst/>
        </a:prstGeom>
      </xdr:spPr>
    </xdr:pic>
    <xdr:clientData/>
  </xdr:oneCellAnchor>";

            if (worksheetPart.DrawingsPart.WorksheetDrawing == null)
            {
                worksheetPart.DrawingsPart.WorksheetDrawing = new Xdr.WorksheetDrawing();
            }

            worksheetPart.DrawingsPart.WorksheetDrawing.Append(new Xdr.OneCellAnchor(oneCellAnchor));

            var drawingsPartId = worksheetPart.GetIdOfPart(worksheetPart.DrawingsPart);

            if (!worksheetPart.Worksheet.Elements <Drawing>().Any(d => d.Id == drawingsPartId))
            {
                worksheetPart.Worksheet.Append(new Drawing()
                {
                    Id = drawingsPartId
                });
            }

            var rows = (uint)Math.Ceiling(dimensions.Height / DefaultRowHeightInPixels);

            return(rows);
        }
Esempio n. 3
0
        /// <summary>
        /// Ajoute des données dans la feuille.
        /// </summary>
        /// <param name="part">La feuille.</param>
        /// <param name="headers">Les entêtes.</param>
        /// <param name="data">Les données.</param>
        /// <param name="startCellReference">La référence de la cellule où poser les données.</param>
        public void AddTable(WorksheetPart part, ColumnFormat[] headers, CellContent[][] data, CellReference startCellReference)
        {
            Assertion.NotNull(part.Worksheet, "part.Worksheet");

            var sheetData = part.Worksheet.GetFirstChild <SheetData>();

            // Ajouter les entêtes
            if (headers != null)
            {
                var rowHeaders = new Row()
                {
                    RowIndex = startCellReference.RowIndex
                };
                foreach (var header in headers)
                {
                    var cell = new Cell()
                    {
                        CellReference = startCellReference.Reference,
                    };
                    FormatCellContent(cell, header.Header);

                    startCellReference.MoveRight();
                    rowHeaders.Append(cell);
                }
                sheetData.Append(rowHeaders);
                startCellReference.NewLine();
            }

            // Ajouter les valeurs
            int columnsLength = headers.Length;

            foreach (var datarow in data)
            {
                var row = new Row()
                {
                    RowIndex = startCellReference.RowIndex
                };

                for (int i = 0; i < columnsLength; i++)
                {
                    var format = headers[i];
                    var cell   = new Cell()
                    {
                        CellReference = startCellReference.Reference,
                    };
                    FormatCellContent(cell, datarow[i]);

                    startCellReference.MoveRight();
                    row.Append(cell);
                }

                sheetData.Append(row);

                startCellReference.NewLine();
            }

            var sheetDimension = new SheetDimension()
            {
                Reference = startCellReference.Dimensions
            };
        }