Skip to content

Commit ae5659d

Browse files
khamlyukDevExpressExampleBot
andauthored
Readme (#1)
* readme * README auto update [skip ci] * fix invisible columns * Update README.md * README auto update [skip ci] * apply suggestions from Vova * README auto update [skip ci] --------- Co-authored-by: DevExpressExampleBot <[email protected]>
1 parent 669b85f commit ae5659d

File tree

6 files changed

+39
-34
lines changed

6 files changed

+39
-34
lines changed

CS/ExportImagesAndRichText/Components/App.razor

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
<meta charset="utf-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<base href="/" />
7-
<link href="_content/DevExpress.Blazor.Themes/blazing-berry.bs5.min.css" rel="stylesheet" asp-append-version="true" />
87

98
@DxResourceManager.RegisterScripts()
9+
@DxResourceManager.RegisterTheme(Themes.Fluent)
10+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-LN+7fdVzj6u52u30Kp6M/trliBMCMKTyK833zpbD+pXdCLuTusPj697FH4R/5mcr" crossorigin="anonymous">
1011
<link href="css/site.css" rel="stylesheet" />
1112
<link href="ExportImagesAndRichText.styles.css" rel="stylesheet" />
1213
<HeadOutlet />

CS/ExportImagesAndRichText/Components/Layout/NavMenu.razor

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,6 @@
2020
<div class="menu-button" onclick="document.getElementById('sidebar').classList.toggle('expanded')"></div>
2121
</Template>
2222
</DxGridLayoutItem>
23-
<DxGridLayoutItem Area="navigation" CssClass="layout-item">
24-
<Template>
25-
<div onclick="document.getElementById('sidebar').classList.remove('expanded')">
26-
<DxMenu Orientation="@Orientation.Vertical" CssClass="menu">
27-
<Items>
28-
<DxMenuItem NavigateUrl="/" Text="Home" CssClass="menu-item" IconCssClass="icon home-icon"></DxMenuItem>
29-
</Items>
30-
</DxMenu>
31-
</div>
32-
</Template>
33-
</DxGridLayoutItem>
3423
<DxGridLayoutItem Area="footer" CssClass="footer">
3524
<Template>
3625
<NavLink href="https://docs.devexpress.com/Blazor/400725/blazor-components" class="button-link">

CS/ExportImagesAndRichText/Components/Pages/Index.razor

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<style>
1616
.myGrid {
17-
height: 700px;
17+
height: 660px;
1818
}
1919
</style>
2020

@@ -24,12 +24,15 @@
2424
<br/>
2525
<br/>
2626

27-
<DxGrid @ref="Grid" Data="@Employees" TextWrapEnabled="true" PageSize="pageSize"
27+
<DxGrid @ref="Grid"
28+
Data="@Employees"
29+
TextWrapEnabled="true"
30+
PageSize="pageSize"
2831
CssClass="myGrid">
2932
<Columns>
3033
<DxGridDataColumn FieldName="EmployeeID" Caption="ID" VisibleIndex="0" Visible="false"/>
31-
<DxGridDataColumn FieldName="FirstName" Caption="First Name" VisibleIndex="1"/>
32-
<DxGridDataColumn FieldName="LastName" Caption="Last Name" VisibleIndex="2"/>
34+
<DxGridDataColumn FieldName="FirstName" Caption="First Name" Width="85" VisibleIndex="1"/>
35+
<DxGridDataColumn FieldName="LastName" Caption="Last Name" Width="85" VisibleIndex="2"/>
3336
<DxGridDataColumn FieldName="NotesHtml" Caption="Notes" VisibleIndex="3">
3437
<CellDisplayTemplate>
3538
<div style="display: flex; justify-content: center; align-items: center; height: 300px; overflow: auto">
@@ -67,7 +70,7 @@
6770
Workbook workbook = new Workbook();
6871
workbook.Unit = DocumentUnit.Centimeter;
6972
var worksheet = workbook.Worksheets.ActiveWorksheet;
70-
var columns = Grid.GetDataColumns().OrderBy(c => c.VisibleIndex).ToList();
73+
var columns = Grid.GetVisibleColumns().Where(c => c is DxGridDataColumn).Cast<DxGridDataColumn>().ToList();
7174

7275
workbook.BeginUpdate();
7376
for (int i = 0; i < columns.Count(); i++) {
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22
<PropertyGroup>
33
<TargetFramework>net8.0</TargetFramework>
44
<Nullable>enable</Nullable>
55
<ImplicitUsings>enable</ImplicitUsings>
66
</PropertyGroup>
77
<ItemGroup>
8-
<PackageReference Include="DevExpress.Blazor" Version="25.1.3" />
9-
<PackageReference Include="DevExpress.Document.Processor" Version="25.1.3" />
10-
</ItemGroup>
11-
<ItemGroup>
8+
<PackageReference Include="DevExpress.Blazor" Version="25.1.5" />
9+
<PackageReference Include="DevExpress.Document.Processor" Version="25.1.5" />
1210
<Folder Include="Images\" />
1311
</ItemGroup>
1412
</Project>

README.md

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
11
<!-- default badges list -->
2-
![](https://img.shields.io/endpoint?url=https://codecentral.devexpress.com/api/v1/VersionRange/1029081391/25.1.0%2B)
32
[![](https://img.shields.io/badge/Open_in_DevExpress_Support_Center-FF7200?style=flat-square&logo=DevExpress&logoColor=white)](https://supportcenter.devexpress.com/ticket/details/T1302036)
43
[![](https://img.shields.io/badge/📖_How_to_use_DevExpress_Examples-e9f6fc?style=flat-square)](https://docs.devexpress.com/GeneralInformation/403183)
54
[![](https://img.shields.io/badge/💬_Leave_Feedback-feecdd?style=flat-square)](#does-this-example-address-your-development-requirementsobjectives)
65
<!-- default badges end -->
7-
# Blazor Grid - How to export images and rich text using Spreadsheet Document API
6+
# Blazor Grid - Export Images/Rich Text Using Spreadsheet Document APIs
87

9-
This example demonstrates how you can use Spreadsheet Document API to create a custom export mechanism to export images and rich text.
8+
This example uses Spreadsheet Document APIs to implement an export mechanism for cells that contain images and rich text.
109

11-
## Overview
10+
DevExpress Blazor Grid ships with [built-in methods](https://docs.devexpress.com/Blazor/404338/components/grid/export) that can export Grid data to a spreadsheet. You need a manual implementation described below only if you use cell templates. Two common data types that require templates are rich text and images. To export such cells, you need to transfer data to spreadsheet cells manually. This example shows how you can do it using [DevExpress Office File API](https://docs.devexpress.com/Blazor/404576/components/office-file-api) - a standalone library that helps you read/write documents, spreadsheets, presentations, and PDF files.
1211

13-
Create a [Workbook](https://docs.devexpress.com/OfficeFileAPI/DevExpress.Spreadsheet.Workbook) instance. Iterate through Grid's data columns (you can obtain them through the [GetDataColumns](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid.GetRowValue(System.Int32-System.String)) method) and create corresponding header cells in the Workbook's active worksheet. You can use the [BeginUpdateFormatting](https://docs.devexpress.com/OfficeFileAPI/DevExpress.Spreadsheet.CellRange.BeginUpdateFormatting?p=netframework) and [EndUpdateFormatting](https://docs.devexpress.com/OfficeFileAPI/DevExpress.Spreadsheet.CellRange.EndUpdateFormatting(DevExpress.Spreadsheet.Formatting)) methods to format the header as required.
12+
> [!IMPORTANT]
13+
> You need a license to the [DevExpress Office File API Subscription](https://www.devexpress.com/products/net/office-file-api/) or [DevExpress Universal Subscription](https://www.devexpress.com/subscriptions/universal.xml) to use these capabilities in production code.
1414
15-
Then, use Grid's [GetVisibleRowCount](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid.GetVisibleRowCount) and [GetRowValue](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid.GetRowValue(System.Int32-System.String)) methods to iterate through its visible rows and obtain field values from each row. Use those values to populate the worksheet. To set a Cell's value, you can use the [SetValue](https://docs.devexpress.com/OfficeFileAPI/DevExpress.Spreadsheet.CellRange.SetValue(System.Object)?p=netframework) method. To set cell value to an image, you can pass the image in the form of a stream or a byte array as a parameter to the **SetValue** method.
15+
![DevExpress Blazor - Export Images and Rich Text](blazor-export-images-rich-text.png)
1616

17-
To place rich text into a cell, you can use a similar approach to the one demonstrated in our [WinForms Spreadsheet Control - How to Edit Rich Text in SpreadsheetControl](https://github.com/DevExpress-Examples/winforms-spreadsheet-how-to-edit-rich-text/tree/19.1.4%2B) example.
17+
## Implementation Details
1818

19-
After populating the worksheet, create a [table](https://docs.devexpress.com/OfficeFileAPI/403308/spreadsheet-document-api/spreadsheet-tables#create-a-table) to enable filtering and sorting of the dataset in the document.
19+
### Set Up a Grid Component
2020

21-
To download the document in Excel format, use the [SaveDocument{Async}](https://docs.devexpress.com/OfficeFileAPI/DevExpress.Spreadsheet.Workbook.SaveDocument.overloads?p=netframework) method to save it to a stream or byte array and use standard Blazor techniques to send the byte array on the client.
21+
Create a DevExpress Blazor Grid and populate it with data. Use [CellDisplayTemplate](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGridDataColumn.CellDisplayTemplate) to display [images](./CS/ExportImagesAndRichText/Components/Pages/Index.razor#L43) or [rich text](./CS/ExportImagesAndRichText/Components/Pages/Index.razor#L35) in cells.
2222

23-
To downlaod the document in PDF format, use the [ExportToPdf{Async}](https://docs.devexpress.com/OfficeFileAPI/DevExpress.Spreadsheet.Workbook.ExportToPdf.overloads) method. You can specify [PrintOptions](https://docs.devexpress.com/OfficeFileAPI/DevExpress.Spreadsheet.Worksheet.PrintOptions?p=netframework) to customize the appearance of the document (e.g., the number of pages in the resulting document).
23+
### Implement an Export Engine
24+
25+
To implement image/rich text export, you must:
26+
27+
1. Create a [Workbook](https://docs.devexpress.com/OfficeFileAPI/DevExpress.Spreadsheet.Workbook) instance and access the active worksheet.
28+
1. Iterate through Grid data columns (using the [GetVisibleColumns](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid.GetVisibleColumns) method) and create corresponding header cells in the worksheet. You can call [BeginUpdateFormatting](https://docs.devexpress.com/OfficeFileAPI/DevExpress.Spreadsheet.CellRange.BeginUpdateFormatting?p=netframework) and [EndUpdateFormatting](https://docs.devexpress.com/OfficeFileAPI/DevExpress.Spreadsheet.CellRange.EndUpdateFormatting(DevExpress.Spreadsheet.Formatting)) methods to apply header formatting.
29+
1. Call [DxGrid.GetVisibleRowCount](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid.GetVisibleRowCount) and [DxGrid.GetRowValue](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid.GetRowValue(System.Int32-System.String)) methods to iterate through visible rows and obtain cell values from each row.
30+
1. Populate the worksheet with cell values using the [SetValue](https://docs.devexpress.com/OfficeFileAPI/DevExpress.Spreadsheet.CellRange.SetValue(System.Object)?p=netframework) method.
31+
1. _Optional._ Create a [table](https://docs.devexpress.com/OfficeFileAPI/403308/spreadsheet-document-api/spreadsheet-tables#create-a-table) to enable data filter and sorting capabilities in the exported document.
32+
1. Call the [SaveDocument{Async}](https://docs.devexpress.com/OfficeFileAPI/DevExpress.Spreadsheet.Workbook.SaveDocument.overloads?p=netframework)/[ExportToPdf{Async}](https://docs.devexpress.com/OfficeFileAPI/DevExpress.Spreadsheet.Workbook.ExportToPdf.overloads) method to export the resulting document.
33+
1. _Optional._ Specify [PrintOptions](https://docs.devexpress.com/OfficeFileAPI/DevExpress.Spreadsheet.Worksheet.PrintOptions?p=netframework) to customize document appearance.
2434

2535
## Files to Review
26-
- [Index.razor](https://github.com/DevExpress-Examples/blazor-grid-how-to-export-images-and-rich-text-using-spreadsheet-document-api/blob/25.1.0%2B/CS/ExportImagesAndRichText/Components/Pages/Index.razor)
27-
- [CustomDocumentVisitor.cs](https://github.com/DevExpress-Examples/blazor-grid-how-to-export-images-and-rich-text-using-spreadsheet-document-api/blob/25.1.0%2B/CS/ExportImagesAndRichText/Models/CustomDocumentVisitor.cs)
36+
37+
* [Index.razor](https://github.com/DevExpress-Examples/blazor-grid-how-to-export-images-and-rich-text-using-spreadsheet-document-api/blob/25.1.0%2B/CS/ExportImagesAndRichText/Components/Pages/Index.razor)
38+
* [CustomDocumentVisitor.cs](https://github.com/DevExpress-Examples/blazor-grid-how-to-export-images-and-rich-text-using-spreadsheet-document-api/blob/25.1.0%2B/CS/ExportImagesAndRichText/Models/CustomDocumentVisitor.cs)
2839

2940
## Documentation
41+
3042
- [Spreadsheet Document API Examples](https://docs.devexpress.com/OfficeFileAPI/12074/spreadsheet-document-api/examples)
43+
- [Blazor Grid](https://docs.devexpress.com/Blazor/403143/components/grid)
44+
3145
<!-- feedback -->
3246
## Does this example address your development requirements/objectives?
3347

blazor-export-images-rich-text.png

206 KB
Loading

0 commit comments

Comments
 (0)