Excel for .NET
画像

You can use images to add visual cues to static content in excel worksheets. Adding images enhances the overall look of the worksheet and makes it more engaging for the end-users. Excel provides various ways for adding images which are discussed as follows.

The following image showcases an image added in the Excel worksheet.

You can add an image to a cell by setting its Value property to the image that you want to apply. Using this method, the image is added to the sheet with its original size and the upper left corner of the image coincides with the upper left corner of the specified cell.

The following code demonstrates how to add an image to a cell by setting its Value property.

C#
コードのコピー
Image img = Image.FromFile("MyImage.bmp");
c1XLBook1.Sheets[0][0, 0].Value = img;

The above mentioned method restricts you to change the size of the image. However, if you want to customize the image by specifying its size, rotation angle, brightness, contrast, border, and more, you can use the XLPictureShape class to set the required properties of the image.

The following code demonstrates how to customize an image using the XLPictureShape class and its properties. In this example, we set the rotation, line color, and line width of the image and add it to the first cell of a worksheet.

C#
コードのコピー
Image img = Image.FromFile("MyImage.bmp");
XLPictureShape pic = new XLPictureShape(img, 1500, 1500);
pic.Rotation = 30.0f;
pic.LineColor = Color.DarkRed;
pic.LineWidth = 100;
// 指定したシートの最初のセルに画像を割り当てます。
c1XLBook1.Sheets[0][0, 0].Value = pic;

Besides adding images to a specific cell, you can also add images to an entire sheet by adding it directly to the ShapeCollection class. The following code demonstrates how to add an image to a sheet by specifying image boundaries in sheet's coordinates.

C#
コードのコピー
Image img = Image.FromFile("MyImage.bmp");
XLPictureShape pic = new XLPictureShape(img, 3000, 3500, 2500, 900);
pic.Rotation = 30.0f;
pic.LineColor = Color.DarkRed;
pic.LineWidth = 100;
// 指定したシートの ShapeCollection に画像を追加します。
c1XLBook1.Sheets[0].Shapes.Add(pic);