Insert Cell Values

This module uses the DevExpress Spreadsheet API to create an Excel document, populate cells with values, and change text formatting.

Use controls in the Cell Values section to enter text, select numeric, and date values. Specify formatting styles. Note that a predefined image will also be inserted into the document.

Use the Create a Spreadsheet (XLSX) dropdown button to select an output format, generate the spreadsheet with the specified content and formatting, and download the result.




using DevExpress.Drawing;
using DevExpress.Spreadsheet;
using Microsoft.AspNetCore.Mvc;

Stream InsertCellValues(string textCellValue, double numericCellValue, DateTime dateCellValue,
    Stream imageStream, ExcelNumberFormat numberFormat, ExcelNumberFormat dateFormat, DocumentFormat outputFormat) {

    using Workbook workbook = new Workbook();
    workbook.Unit = DevExpress.Office.DocumentUnit.Inch;

    Worksheet firstWorksheet = workbook.Worksheets.First();
    firstWorksheet.Name = "Inserted Cell Values";

    firstWorksheet["A1"].Value = "Text Value";
    firstWorksheet["B1"].Value = "Numeric Value";
    firstWorksheet["C1"].Value = "Date Value";
    firstWorksheet["D1"].Value = "Image";

    // Make header text bold
    var formatting = firstWorksheet["A1:D1"].BeginUpdateFormatting();
    formatting.Font.Bold = true;
    firstWorksheet["A1:D1"].EndUpdateFormatting(formatting);

    // Set cell values
    firstWorksheet["A2"].Value = textCellValue;

    firstWorksheet["B2"].Value = numericCellValue;
    firstWorksheet["B2"].NumberFormat = ExcelCellFormatHelper.GetNumberFormatString(numberFormat);

    firstWorksheet["C2"].Value = dateCellValue;
    firstWorksheet["C2"].NumberFormat = ExcelCellFormatHelper.GetDateFormatString(dateFormat);

    firstWorksheet["D2"].Value = DXImage.FromStream(imageStream);

    firstWorksheet.Columns.AutoFit(0, 4);

    // Set the image column width to 1 inch
    firstWorksheet.Columns["D"].Width = 1;

    // Create output stream
    var outputStream = new MemoryStream();

    // Save document in target format
    workbook.SaveDocument(outputStream, outputFormat);

    outputStream.Seek(0, SeekOrigin.Begin);

    return outputStream;
}