C# code of the Images demo
using System;
using System.Drawing;
using System.IO;
using ExpertXls.ExcelLib;
namespace ExcelLibraryDemos
{
/// <summary>
/// Adds pictures to a worksheet: at real size, scaled, stretched over a range of cells and loaded directly from a
/// file. The cells covered by each picture are read back from the ExcelPicture object.
/// </summary>
public static class ImagesDemo
{
/// <summary>The name of the generated file, without extension.</summary>
public const string OutputName = "Images";
/// <summary>
/// Creates the workbook in the given format and saves it to the output stream.
/// </summary>
/// <param name="format">Excel 97-2003 (.xls) or Excel 2007+ (.xlsx).</param>
/// <param name="inputFolder">The folder that contains the Images folder of the sample.</param>
/// <param name="output">The stream that receives the workbook.</param>
public static void CreateWorkbook(ExcelWorkbookFormat format, string inputFolder, Stream output)
{
string imagesFolder = Path.Combine(inputFolder, "Images");
ExcelWorkbook workbook = new ExcelWorkbook(format);
// workbook.LicenseKey = "your license key";
workbook.DocumentProperties.Subject = "Images in a worksheet";
workbook.DocumentProperties.Comments = "Created with ExpertXls Excel Library for .NET";
Image logo = Image.FromFile(Path.Combine(imagesFolder, "logo.jpg"));
Image demoImage = Image.FromFile(Path.Combine(imagesFolder, "demo_250x300.jpg"));
try
{
ExcelCellStyle titleStyle = DemoLayout.AddTitleStyle(workbook);
ExcelCellStyle sectionStyle = DemoLayout.AddSectionStyle(workbook);
ExcelWorksheet worksheet = workbook.Worksheets[0];
worksheet.Name = "Images";
DemoLayout.SetupPage(worksheet, logo, ExcelPageOrientation.Portrait);
DemoLayout.WriteTitle(worksheet, "A2:G3", "Adding Images to an Excel Worksheet", titleStyle);
// 1. at real size: only the top left cell is given (column, row, one based)
DemoLayout.WriteSection(worksheet, "A5:G5", "An image at real size", sectionStyle);
ExcelPicture picture = worksheet.Pictures.AddPicture(1, 7, demoImage);
int nextRow = WritePictureInfo(worksheet, picture);
// 2. scaled: the width and height scale are percentages of the real size
DemoLayout.WriteSection(worksheet, String.Format("A{0}:G{0}", nextRow), "An image scaled to 75%", sectionStyle);
picture = worksheet.Pictures.AddPicture(1, nextRow + 2, demoImage, 75, 75);
nextRow = WritePictureInfo(worksheet, picture);
// 3. stretched over a range of cells: the aspect ratio is not preserved
DemoLayout.WriteSection(worksheet, String.Format("A{0}:G{0}", nextRow),
"An image stretched over the range A" + (nextRow + 2) + ":E" + (nextRow + 24), sectionStyle);
picture = worksheet.Pictures.AddPicture(1, nextRow + 2, 5, nextRow + 24, demoImage);
nextRow = WritePictureInfo(worksheet, picture);
// 4. from a file: the library loads the image itself
DemoLayout.WriteSection(worksheet, String.Format("A{0}:G{0}", nextRow), "An image loaded from a file", sectionStyle);
picture = worksheet.Pictures.AddPicture(1, nextRow + 2, Path.Combine(imagesFolder, "logo.jpg"));
// the picture moves with the cells but keeps its size when the rows or columns are resized
picture.PicturePlacement = ExcelPicturePlacement.MoveDontSizeWithCells;
WritePictureInfo(worksheet, picture);
workbook.Save(output);
}
finally
{
workbook.Close();
logo.Dispose();
demoImage.Dispose();
}
}
// writes the cells covered by the picture below it and returns the row of the next section
private static int WritePictureInfo(ExcelWorksheet worksheet, ExcelPicture picture)
{
int infoRow = picture.BottomRowIndex + 1;
ExcelRange infoRange = worksheet[infoRow, 1, infoRow, 7];
infoRange.Merge();
worksheet[infoRow, 1].Value = String.Format("Left column: {0}, top row: {1}, right column: {2}, bottom row: {3}, size: {4} x {5} points",
picture.LeftColumnIndex, picture.TopRowIndex, picture.RightColumnIndex, picture.BottomRowIndex,
Math.Round(picture.PictureWidthPoints), Math.Round(picture.PictureHeightPoints));
infoRange.Style.Font.Bold = true;
infoRange.Style.Fill.FillType = ExcelCellFillType.SolidFill;
infoRange.Style.Fill.SolidFillOptions.BackColor = Color.LightBlue;
return infoRow + 3;
}
}
}
Create, edit and read Excel workbooks in a Blazor application: the workbooks are created on the server and downloaded by the browser. The code of each workbook is in the Features folder.