Spread Windows Forms 17
Spread Windows Forms 17.0 Product Documentation / Developer's Guide / File Operations / Saving Data to a File / Saving to an HTML File
In This Topic
    Saving to an HTML File
    In This Topic

    HTML (Hypertext Markup Language) files can be accessed with a web browser. Using the SaveAs method, you can save cell data of a workbook or a worksheet in HTML file format. This method accepts the path and the file name to save, and any additional parameters depending on the method call.

    Export workbook to HTML

    To save the workbook, you can use the SaveAs method of the IWorkbook interface. Refer to the following sample code that saves the cell data from a workbook to an HTML file.

    C#
    Copy Code
    fpSpread1.AsWorkbook().SaveAs("Book1.htm", GrapeCity.Spreadsheet.IO.FileFormat.Html);
    
    VB
    Copy Code
    FpSpread1.AsWorkbook().SaveAs("Book1.htm", GrapeCity.Spreadsheet.IO.FileFormat.Html)
    

    You can also set export options of the workbook by using the ExportOptions property of the ExportContext class.

    Export worksheet to HTML

    To save a single worksheet, you can use the SaveAs method of the IWorksheet interface. Refer to the following sample code that saves the cell data from a worksheet to an HTML file.

    C#
    Copy Code
    ActiveSheet.SaveAs("Sheet1.htm", GrapeCity.Spreadsheet.IO.FileFormat.Html);
    
    VB
    Copy Code
    ActiveSheet.SaveAs("Sheet1.htm", GrapeCity.Spreadsheet.IO.FileFormat.Html)
    

    Additionally, you can use the ExportOptions enumeration of the OpenXml namespace to set options for exporting data.

    Limitation

    Example

    The following example shows how to save cell data and formatting a workbook or worksheet to HTML file format.

    C#
    Copy Code
    // Initialize workbook and worksheet
     GrapeCity.Spreadsheet.IWorkbook TestWorkBook = fpSpread1.AsWorkbook();
     GrapeCity.Spreadsheet.IWorksheet TestActiveSheet = fpSpread1.AsWorkbook().ActiveSheet;
    // Apply formatting
    fpSpread1.Sheets.Count = 5;
    for (int i = 0; i < 5; i++)
    {
     TestWorkBook.Worksheets[i].RowHeader.Cells[0, 0].Text = "row";
     TestWorkBook.Worksheets[i].ColumnHeader.Cells[0, i].Text = "test";
     TestWorkBook.Worksheets[i].Cells[0, i].Borders.Color = GrapeCity.Spreadsheet.Color.FromKnownColor(GrapeCity.Core.KnownColor.Red);
     TestWorkBook.Worksheets[i].Cells[0, i].Borders.LineStyle = BorderLineStyle.DashDotDot;
     TestWorkBook.Worksheets[i].Cells[0, i].Interior.Color = GrapeCity.Spreadsheet.Color.FromKnownColor(GrapeCity.Core.KnownColor.Green);
     TestWorkBook.Worksheets[i].Cells[0, i].Value = 11;
    }
    ExportContext ct = new ExportContext();
    ct.Options = ExportOptions.RowHeader;
    // Export workbook to HTML without options
    TestWorkBook.SaveAs("SpreadToHTML.htm", GrapeCity.Spreadsheet.IO.FileFormat.Html).ToString();
    // Export workbook to HTML with options
    TestWorkBook.SaveAs("SpreadToHTML-Option.htm", GrapeCity.Spreadsheet.IO.FileFormat.Html, context: ct);
    // Export worksheet to HTML using stream without options
    System.IO.FileStream stream = new System.IO.FileStream(@"WorksheetToStream.htm", System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite);
    TestActiveSheet.SaveAs(stream, GrapeCity.Spreadsheet.IO.FileFormat.Html);
    stream.Close();
    // Export worksheet to HTML with options
    TestActiveSheet.SaveAs("WorksheetToHTML-Option.htm", GrapeCity.Spreadsheet.IO.FileFormat.Html, options: ExportOptions.ColumnHeader);
    
    VB
    Copy Code
    ' Initialize workbook and worksheet
    Dim TestWorkBook As GrapeCity.Spreadsheet.IWorkbook = FpSpread1.AsWorkbook()
    Dim TestActiveSheet As GrapeCity.Spreadsheet.IWorksheet = FpSpread1.AsWorkbook().ActiveSheet
    ' Apply formatting
    FpSpread1.Sheets.Count = 5
    For i = 0 To 4
      TestWorkBook.Worksheets(i).RowHeader.Cells(0, 0).Text = "row"
      TestWorkBook.Worksheets(i).ColumnHeader.Cells(0, i).Text = "test"
      TestWorkBook.Worksheets(i).Cells(0, i).Borders.Color = GrapeCity.Spreadsheet.Color.FromKnownColor(GrapeCity.Core.KnownColor.Red)
      TestWorkBook.Worksheets(i).Cells(0, i).Borders.LineStyle = BorderLineStyle.DashDotDot
      TestWorkBook.Worksheets(i).Cells(0, i).Interior.Color = GrapeCity.Spreadsheet.Color.FromKnownColor(GrapeCity.Core.KnownColor.Green)
      TestWorkBook.Worksheets(i).Cells(0, i).Value = 11
    Next
    Dim ct As ExportContext = New ExportContext()
    ct.Options = ExportOptions.RowHeader
    ' Export workbook to HTML without options
    TestWorkBook.SaveAs("SpreadToHTML.htm", GrapeCity.Spreadsheet.IO.FileFormat.Html).ToString()
    ' Export workbook to HTML with options
    TestWorkBook.SaveAs("SpreadToHTML-Option.htm", GrapeCity.Spreadsheet.IO.FileFormat.Html, context:=ct)
    ' Export worksheet to HTML using stream without options
    Dim stream As FileStream = New FileStream("WorksheetToStream.htm", FileMode.Create, FileAccess.ReadWrite)
    TestActiveSheet.SaveAs(stream, GrapeCity.Spreadsheet.IO.FileFormat.Html)
    stream.Close()
    ' Export worksheet to HTML with options
    TestActiveSheet.SaveAs("WorksheetToHTML-Option.htm", GrapeCity.Spreadsheet.IO.FileFormat.Html, options:=ExportOptions.ColumnHeader)