page 130

C#: Convert HTML to Excel

2024-08-09 08:19:00 Written by Koohji

HTML (HyperText Markup Language) is primarily used for structuring content on web pages. While it excels at presenting information visually on the web, it lacks the robust analytical capabilities and data manipulation features found in spreadsheet software like Excel. By converting HTML data to Excel, users can leverage Excel's advanced functionalities like formulas, charts, tables, and macros to organize and analyze data efficiently. In this article, we will explain how to convert HTML to Excel in C# using Spire.XLS for .NET.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Convert HTML to Excel in C#

Spire.XLS for .NET offers the Workbook.LoadFromHtml() method to load an HTML file. After loading the HTML file, you can easily save it in Excel format using the Workbook.SaveToFile() method. The detailed steps are as follows.

  • Create an object of the Workbook class.
  • Load an HTML file using the Workbook.LoadFromHtml() method.
  • Save the HTML file in Excel format using the Workbook.SaveToFile() method.
  • C#
using Spire.Xls;

namespace ConvertHtmlToExcel
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Specify the input HTML file path
            string filePath = @"C:\Users\Administrator\Desktop\Sample.html";

            // Create an object of the workbook class
            Workbook workbook = new Workbook();

            // Load the HTML file
            workbook.LoadFromHtml(filePath);

            // Save the HTML file in Excel XLSX format
            string result = @"C:\Users\Administrator\Desktop\ToExcel.xlsx";
            workbook.SaveToFile(result, ExcelVersion.Version2013);

            workbook.Dispose();
        }
    }
}

C#: Convert HTML to Excel

Insert HTML String to Excel in C#

In addition to converting HTML files to Excel, Spire.XLS for .NET also allows you to insert HTML strings into Excel cells by using the CellRange.HtmlString property. The detailed steps are as follows.

  • Create an object of the Workbook class.
  • Get a specific worksheet by its index (0-based) using the Workbook.Worksheets[index] property.
  • Get the cell that you want to add an HTML string to using the Worksheet.Range[] property.
  • Add an HTML sting to the cell using the CellRange.HtmlString property.
  • Save the resulting workbook to a new file using the Workbook.SaveToFile() method.
  • C#
using Spire.Xls;

namespace InsertHtmlStringInExcel
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create an object of the workbook class
            Workbook workbook = new Workbook();
            // Get the first sheet
            Worksheet sheet = workbook.Worksheets[0];

            // Specify the HTML string
            string htmlCode = "<p><font size='12'>This is a <b>paragraph</b> with <span style='color: red;'>colored text</span>.</font></p>";

            // Get the cell that you want to add the HTML string to
            CellRange range = sheet.Range["A1"];
            // Add the HTML string to the cell
            range.HtmlString = htmlCode;

            // Auto-adjust the width of the first column based on its content
            sheet.AutoFitColumn(1);

            // Save the resulting workbook to a new file
            string result = @"C:\Users\Administrator\Desktop\InsertHtmlStringIntoCell.xlsx";
            workbook.SaveToFile(result, ExcelVersion.Version2013);

            workbook.Dispose();
        }
    }
}

C#: Convert HTML to Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

This article demonstrates how to set and get slide title in a PowerPoint document using Spire.Presentation for Java.

Set slide title

import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;

public class SetSlideTitle {
    public static void main(String[] args) throws Exception {
        //Create a Presentation instance
        Presentation ppt = new Presentation();
        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);
        
        //Set title for the slide
        slide.setTitle("Tile Text");
        
        //Save the result document
        ppt.saveToFile("SetTitle.pptx", FileFormat.PPTX_2013);
    }
}

Set and Get Slide Title in PowerPoint in Java

Get slide title

import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;

public class GetSlideTitle {
    public static void main(String[] args) throws Exception {
        //Create a Presentation instance
        Presentation ppt = new Presentation();
        //Load a PowerPoint document
        ppt.loadFromFile("SetTitle.pptx");

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);

        //Print out the title of the slide
        String tile = slide.getTitle();
        System.out.println(tile);
    }
}

Set and Get Slide Title in PowerPoint in Java

Java: Hide or Show Gridlines in Excel

2022-06-09 09:27:00 Written by Koohji

Gridlines are horizontal and vertical faint lines that differentiate between cells in a worksheet. All Excel worksheets have gridlines by default, but sometimes you may need to remove the gridlines as they might interfere with your work. In this article, you will learn how to programmatically show or hide/remove gridlines in an Excel worksheet using Spire.XLS for Java.

Install Spire.XLS for Java

First, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls</artifactId>
        <version>15.11.3</version>
    </dependency>
</dependencies>

Hide or Show Gridlines in Excel

The detailed steps are as follows.

  • Create a Workbook object.
  • Load a sample Excel document using Workbook.loadFromFile() method.
  • Get a specified worksheet using Workbook.getWorksheets().get() method.
  • Hide or show gridlines in the specified worksheet using Worksheet.setGridLinesVisible() method.
  • Save the result file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class HideOrShowGridlines {

    public static void main(String[] args) {

        //Create a Workbook object
        Workbook workbook = new Workbook();

        //Load a sample Excel document
        workbook.loadFromFile("E:\\Files\\Test.xlsx");

        //Get the first worksheet
        Worksheet worksheet = workbook.getWorksheets().get(0);

        //Hide gridlines
        worksheet.setGridLinesVisible(false);

        ////Show gridlines
        //worksheet.setGridLinesVisible(true);

        //Save the document
        workbook.saveToFile("HideGridlines.xlsx", ExcelVersion.Version2016);
    }
}

Java: Hide or Show Gridlines in Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

page 130

Coupon Code Copied!

Christmas Sale

Celebrate the season with exclusive savings

Save 10% Sitewide

Use Code:

View Campaign Details