Back to Devexpress

WorksheetExtensions Class

officefileapi-devexpress-dot-spreadsheet-8a5cb5a0.md

latest4.8 KB
Original Source

WorksheetExtensions Class

Defines extension methods for the Worksheet interface.

You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use these methods in production code.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Docs.v25.2.dll

NuGet Package : DevExpress.Document.Processor

Declaration

csharp
public static class WorksheetExtensions
vb
Public Module WorksheetExtensions

Remarks

To enable worksheet extensions, add a reference to the DevExpress.Docs.v25.2.dll assembly and explicitly import the DevExpress.Spreadsheet namespace into the code with the using directive ( Imports in Visual Basic).

You can call extension methods in the same way as instance methods of the Worksheet object.

The following code snippet uses the Import extension method to insert data from a list into a worksheet.

The following code snippet imports data from a list of string objects:

cs
byte[] imageBytes1 = File.ReadAllBytes("images//img.png");
byte[] imageBytes2 = File.ReadAllBytes("images//x-docserver.png");
// ...
    Workbook workbook = new Workbook();
    workbook.BeginUpdate();
    try
    {
        Worksheet worksheet = workbook.Worksheets[0];
        worksheet.Clear(worksheet.GetUsedRange());
        worksheet.Cells["A1"].ColumnWidthInCharacters = 35;
        worksheet.Cells["A1"].Value = "Import data from List vertically:";
        ImportList(worksheet);
    }
    finally
    {
        workbook.EndUpdate();
        workbook.SaveDocument("result.xlsx");
        Process.Start(new ProcessStartInfo("result.xlsx") { UseShellExecute = true });
    }
    workbook.Dispose();
}
// ...
void ImportList(Worksheet worksheet)
{
    // Create a List object containing string values.
    List<string> cities = new List<string>
    {
        "New York",
        "Rome",
        "Beijing",
        "Delhi"
    };

    List<byte[]> imageList = new List<byte[]>();
    {
        imageList.Add(imageBytes1);
        imageList.Add(imageBytes2);
    };

    // Import the list into the worksheet and insert it vertically, starting with cell B1.
    worksheet.Import(cities, 0, 1, true);

    // Import the image list into the worksheet and insert it vertically
    worksheet.Import(imageList, 0, 2, true, new DataImportOptions());
}
vb
Private imageBytes1() As Byte = File.ReadAllBytes("images//img.png")
Private imageBytes2() As Byte = File.ReadAllBytes("images//x-docserver.png")
' ...
    Dim workbook As New Workbook()
    workbook.BeginUpdate()
    Try
        Dim worksheet As Worksheet = workbook.Worksheets(0)
        worksheet.Clear(worksheet.GetUsedRange())
        worksheet.Cells("A1").ColumnWidthInCharacters = 35
        worksheet.Cells("A1").Value = "Import data from List vertically:"
        ImportList(worksheet)
    Finally
        workbook.EndUpdate()
        workbook.SaveDocument("result.xlsx")
        Process.Start(New ProcessStartInfo("result.xlsx") With {.UseShellExecute = True})
    End Try
    workbook.Dispose()
    ' ...
Private Sub ImportList(ByVal worksheet As Worksheet)
    ' Create a List object containing string values.
    Dim cities As New List(Of String) From {"New York", "Rome", "Beijing", "Delhi"}

    Dim images As New List(Of Byte()) From {imageBytes1, imageBytes2}

    ' Import the list into the worksheet and insert it vertically, starting with cell B1.
    worksheet.Import(cities, 0, 1, True)
    worksheet.Import(images, 2, 1)
End Sub

Refer to the following section for more examples on how to use the WorksheetExtensions methods to import data into a worksheet or export data from a worksheet to a data table: Import and Export Data

Inheritance

Object WorksheetExtensions

See Also

WorksheetExtensions Members

DevExpress.Spreadsheet Namespace