Back to Devexpress

Export Cell Content from Spreadsheet Documents

officefileapi-404606-spreadsheet-document-api-cell-basics-cell-content-export.md

latest5.6 KB
Original Source

Export Cell Content from Spreadsheet Documents

  • Oct 26, 2023
  • 3 minutes to read

You can use the Spreadsheet API to export cell content to HTML and RTF.

Export to HTML

Use the following API to export cell content to HTML and specify export options:

GetHtmlContent()Gets cell content as a string in HTML format.GetHtmlContent(HtmlCellContentExportOptions)Gets cell content as a string in HTML format. Allows you to specify export options.HtmlCellContentExportOptionsContains options for cell content export to HTML.FontUnitGets or sets the measurement units used for the font size when exporting cell content to HTML.IgnoreDisplayFormatGets or sets whether to ignore the display format when exporting cell content to HTML.

The following snippet loads a document from a file and exports content from cell B3. The IgnoreDisplayFormat property is set to true and the FontUnit property is set to Pixel. The cell contains conditional formatting.

cs
using DevExpress.Spreadsheet;
using DevExpress.XtraSpreadsheet.Export;
//... 

using (Workbook workbook = new Workbook()) {
    workbook.LoadDocument("Documents\\Document.xlsx", DocumentFormat.Xlsx);
    workbook.Calculate();

    Worksheet worksheet = workbook.Worksheets[0];
    Cell cell = worksheet[4, 1]; //B3
    HtmlCellContentExportOptions htmlExportOptions = new HtmlCellContentExportOptions();
    htmlExportOptions.IgnoreDisplayFormat = true;
    htmlExportOptions.FontUnit = DevExpress.XtraSpreadsheet.Export.Html.HtmlFontUnit.Pixel;
    string htmlContentString = cell.GetHtmlContent(htmlExportOptions);
}
vb
Imports DevExpress.Spreadsheet
Imports DevExpress.XtraSpreadsheet.Export
'... 

Using workbook As New Workbook()
    workbook.LoadDocument("Documents\Document.xlsx", DocumentFormat.Xlsx)
    workbook.Calculate()

    Dim worksheet As Worksheet = workbook.Worksheets(0)
    Dim cell As Cell = worksheet(4, 1) 'B3
    Dim htmlExportOptions As New HtmlCellContentExportOptions()
    htmlExportOptions.IgnoreDisplayFormat = True
    htmlExportOptions.FontUnit = DevExpress.XtraSpreadsheet.Export.Html.HtmlFontUnit.Pixel
    Dim htmlContentString As String = cell.GetHtmlContent(htmlExportOptions)

End Using

The resulting HTML string looks as follows:

ets
<font style="font-family:Calibri;font-size:15px;font-weight:normal;font-style:normal;color:#FFFFFF;">cell with conditional formatting</font>

Export to RTF

Use the following API to export cell content to RTF and specify export options:

GetRtfContent()Gets cell content as a string in RTF format.GetRtfContent(RtfCellContentExportOptions)Gets cell content as a string in RTF format. Allows you to specify export options.RtfCellContentExportOptionsContains options for cell content export to RTF.IgnoreDisplayFormatGets or sets whether to ignore the display format when exporting cell content to RTF.

The following snippet loads a document from a file and exports content from cell B3. The IgnoreDisplayFormat property is set to true:

cs
using DevExpress.Spreadsheet;
using DevExpress.XtraSpreadsheet.Export;
//... 

using (Workbook workbook = new Workbook()) {
    workbook.LoadDocument("Documents\\Document.xlsx", DocumentFormat.Xlsx);
    workbook.Calculate();
    Worksheet worksheet = workbook.Worksheets[0];

    Cell cell = worksheet[4, 1]; //B3
    RtfCellContentExportOptions rtfExportOptions = new RtfCellContentExportOptions();
    rtfExportOptions.IgnoreDisplayFormat = true;
    string rtfContentString = cell.GetRtfContent(rtfExportOptions);
}
vb
Imports DevExpress.Spreadsheet
Imports DevExpress.XtraSpreadsheet.Export
'... 

Using workbook As New Workbook()
    workbook.LoadDocument("Documents\Document.xlsx", DocumentFormat.Xlsx)
    workbook.Calculate()
    Dim worksheet As Worksheet = workbook.Worksheets(0)

    Dim cell As Cell = worksheet(4, 1) 'B3
    Dim rtfExportOptions As New RtfCellContentExportOptions()
    rtfExportOptions.IgnoreDisplayFormat = True
    Dim rtfContentString As String = cell.GetRtfContent(rtfExportOptions)
End Using

The resulting HTML string looks as follows:

ets
{\rtf1\deff0{\fonttbl{\f0 Calibri;}}{\colortbl;}{\fs22 cell with conditional formatting}}