Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt

In addition

...

to Charts, ExcelApplication allows you to add the following drawing objects to a worksheet:

  • Comments
    A comment is a note attached to a cell, or group of merged cells, that is separate from other cell content. A comment is represented by a Comment object.

...

  •  
  • Pictures
    A picture is a JPEG, PNG, BMP, or GIF image that is inserted in the worksheet. A picture is represented by a Picture object.

...

  • Shapes
    A shape may be one of Excel's ready-made AutoShapes or the display area of a comment or picture. A shape is represented by a Shape object.

Adding a Comment to a Worksheet

A comment must be associated with a cell. Once created, comments may not be moved between cells. To create a comment:

  1. Use the property Cell.Comment to return a Comment object.

    Code Block
    c#
    languagec#
     ExcelApplication ExcelApplication xla = new ExcelApplication();
    Workbook wb = xla.Create();
    Worksheet ws = wb.Worksheets[0];
    
    Cell cellB5 = ws.Cells["B5"];
    Comment comm = cellB5.Comment;
    
  2. Add text to the comment.

    Code Block
    c#c#
    comm.Text = "Figures for March are projected.";
  3. Specify the author of the comment.

    Code Block
    c#c#
    comm.Author = "John Doe";
  4. Set whether the comment will be visible when the document is opened in Excel. By default, the comment will be hidden, and will be displayed only if the user hovers over the comment's cell.

    Code Block
    c#c#
    comm.Visible = true;
  5. To modify the comment's display area, return its Shape object and set Shape properties, for example:

    Code Block
    c#c#
    
    Shape commShape = comm.Shape;
    commShape.FillColor = oColor;
    commShape.FitToText = true;
    

    Image Modified

Creating a Picture

Pictures in a worksheet are stored in the Pictures collection. To add a picture to a worksheet:

  1. Return a Pictures collection.

    Code Block
    c#c#
    
    ExcelApplication xla = new ExcelApplication();
    Workbook wb = xla.Create();
    Worksheet ws = wb.Worksheets[0];
    Pictures pics = ws.Pictures;
    
  2. Create a Picture object from a JPEG, PNG, BMP, or GIF image and insert it at a specified anchor in the worksheet. The anchor represents the position of the top-left corner of the picture in the worksheet.

    Code Block
    c#c#
    
    string imgPath = @"c:\images\owlogo.jpg";
    Anchor anch = ws.CreateAnchor(0, 7, 0, 0);
    Picture pic = pics.CreatePicture(imgPath, anch);
    
  3. To modify the size, position, or formatting of a Picture, return its Shape object and set Shape properties, for example:

    Code Block
    c#c#
    
    Shape picShape = pic.Shape;
    picShape.Rotation = 45;
    

Creating an AutoShape

Excel's ready-made AutoShapes are represented by the Shapeclass. To create an AutoShape:

  1. Return a Shapes collection.

    Code Block
    c#c#
    
    ExcelApplication xla = new ExcelApplication();
    Workbook wb = xla.Create();
    Worksheet ws = wb.Worksheets[0];
    Shapes shps = ws.Shapes;
    
  2. Create a shape of a specified type and insert it at an anchor in the worksheet.

    Code Block
    c#c#
    
    Anchor anch = ws.CreateAnchor(0, 7, 0, 0);
    Shape shpHeart = shps.CreateShape(ShapeType.Heart, anch);

The ShapeType class contains the Excel AutoShapes supported by ExcelWriter. The specified anchor represents the position of the top-left corner of the shape in the worksheet.

...