Drawing Objects
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:
- Use the property
Cell.Comment to return
a Comment object.
ExcelApplication xla = new ExcelApplication();
Workbook wb = xla.Create();
Worksheet ws = wb.Worksheets[0];
Cell cellB5 = ws.Cells["B5"];
Comment comm = cellB5.Comment;
- Add text to the comment.
comm.Text = "Figures for March are projected.";
- Specify the author of the comment.
comm.Author = "John Doe";
- 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.
- To modify the comment's display area, return its
Shape object and set Shape properties,
for example:
Shape commShape = comm.Shape;
commShape.FillColor = oColor;
commShape.FitToText = true;
 |
A comment is a note attached to a cell - or group
of merged cells - that is separate from other cell content. |
Creating a Picture

Pictures in a worksheet are stored in the Pictures collection.
To add a picture to a worksheet:
- Return a
Pictures collection.
ExcelApplication xla = new ExcelApplication();
Workbook wb = xla.Create();
Worksheet ws = wb.Worksheets[0];
Pictures pics = ws.Pictures;
- 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.
string imgPath = @"c:\images\owlogo.jpg";
Anchor anch = ws.CreateAnchor(0,7,0,0);
Picture pic = pics.CreatePicture(imgPath, anch);
- To modify the size, position, or formatting of a
Picture,
return its Shape object and set Shape properties,
for example:
Shape picShape = pic.Shape;
picShape.Rotation = 45;
Creating an AutoShape

Excel's ready-made AutoShapes are represented by the Shape
class. To create an AutoShape:
- Return a
Shapes collection.
ExcelApplication xla = new ExcelApplication();
Workbook wb = xla.Create();
Worksheet ws = wb.Worksheets[0];
Shapes shps = ws.Shapes;
- Create a shape of a specified type and insert it at an anchor in the
worksheet.
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.

Copyright 2007 © SoftArtisans, Inc. All Rights Reserved.
|