page 102

When working with Excel, you may sometimes need to find the common values between two ranges of cells in a worksheet. For this reason, it’s recommended that Java codes can be used to automatically find the intersection of certain ranges. In this article, you'll learn how to achieve the operation using Spire.XLS for Java.

Install Spire.XLS for Java

First of all, 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>

Get the Intersection of Two Cell Ranges in Excel

The following are detailed steps to get the intersection of two cell ranges in an Excel worksheet.

  • Create a Workbook instance and load a sample Excel file using Workbook.loadFromFile() method.
  • Get a specific worksheet of the file using Workbook.getWorksheets().get() method.
  • Specify two ranges of cells using Worksheet.getRange().get() method and get their intersection using XlsRange.intersect() method.
  • Create a StringBuilder instance.
  • Loop through the intersection and obtain cell values using CellRange.getValue() method.
  • Append the result to the StringBuilder instance using StringBuilder.append() method.
  • Java
import com.spire.xls.*;

public class getIntersectionOfTwoRanges {
    public static void main(String[] args) {
        //Create a Workbook instance
        Workbook workbook = new Workbook();

        //Load a sample Excel file
        workbook.loadFromFile( "C:\\Users\\Test1\\Desktop\\sample.xlsx");

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

        //Specify two cell ranges and get their intersection
        CellRange range = sheet.getRange().get("B2:E7").intersect(sheet.getRange().get("C3:D7"));

        //Create a StringBuilder instance
        StringBuilder content = new StringBuilder();
        content.append("The intersection of the two ranges \"B2:E7\" and \"C3:D7\" is:"+"\n");

        //Loop through the intersection and obtain cell values
        for(CellRange r : range.getCellList())
        {
            content.append(r.getValue()+"\n");
        }

        //Output the result
        System.out.println(content);
    }
}

The input Excel:

Java: Get the Intersection of Two Cell Ranges in Excel

The output result:

Java: Get the Intersection of Two Cell Ranges 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.

Every paragraph in a Word document uses a paragraph style, either intentionally or unintentionally. The paragraph style can be a built-in style, such as Heading 1 and Heading 2, or it can be a customized style. This article introduces how we can extract paragraphs that use a specific style by using Spire.Doc for Java.

The table below lists the style names in MS Word and their corresponding names in Spire.Doc. A very simple rule is that the style name returned by programming does not contain spaces.

Style Name in MS Word Style Name in Spire.Doc
Title Title
Subtitle Subtitle
Heading 1 Heading1
Heading 2 Heading2
Heading 3 Heading3
No Spacing NoSpacing
Quote Quote
Intense Quote IntenseQuote
List Paragraph ListParagraph
Normal Normal
Custom Name CustomName

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.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.doc</artifactId>
        <version>13.11.2</version>
    </dependency>
</dependencies>

Extract Paragraphs that Use a Specific Style

The style name of a specific paragraph can be obtained by Paragraph.getStyleName() method. If a paragraph’s style name is exactly what you want to query, you can get the paragraph content using Paragraph.getText() method. The following are the steps to extract paragraphs that use a specific style.

  • Load a sample Word document while initializing the Document object.
  • Loop through the sections in the document.
  • Get a specific paragraph from a certain section using Section.getParagraphs().get() method.
  • Get the paragraph's style name using Paragraph.getStyleName() method and determine if the style is "Heading 1".
  • If yes, extract the text of the paragraph using Paragraph.getText() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.documents.Paragraph;

public class GetParagraphByStyleName {
    public static void main(String[] args) {

        //Load a sample Word document while initializing the Document object
        Document doc = new Document("C:\\Users\\Administrator\\Desktop\\Styles.docx");

        //Declare a variable
        Paragraph paragraph;

        //Loop through the sections
        for (int i = 0; i < doc.getSections().getCount(); i++) {

            //Loop through the paragraphs of a specific section
            for (int j = 0; j < doc.getSections().get(i).getParagraphs().getCount(); j++) {

                //Get a specific paragraph
                paragraph = doc.getSections().get(i).getParagraphs().get(j);

                //Determine if the paragraph style is "Heading 1"
                if (paragraph.getStyleName().equals("Heading1")) {

                    //Get the text of the paragraph in "Heading 1"
                    System.out.println("Heading 1: " + paragraph.getText() + "\n");
                }

                //Determine if the paragraph style is "My Custom Style"
                if (paragraph.getStyleName().equals("MyCustomStyle")) {

                    //Get the text of the paragraph in "My Custom Style"
                    System.out.println("My Custom Style: " + paragraph.getText());
                }
            }
        }
    }
}

Java: Extract Word Paragraphs that Use a Specific Style

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.

Java: Convert Word to EPUB

2021-11-26 06:18:43 Written by Koohji

EPUB (short for electronic publication) is a popular file format for e-books. EPUB files can be read on numerous e-readers and most smartphones, tablets and computers. In some circumstances, you might need to convert your Word document to EPUB file format to make it readable on various devices. This article will show you how to achieve this task programmatically in Java using Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.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.doc</artifactId>
        <version>13.11.2</version>
    </dependency>
</dependencies>

Convert Word to EPUB

The following are the steps to convert a Word document to EPUB file format:

  • Create a Document instance.
  • Load a Word document using Document.loadFromFile() method.
  • Save the Word document to EPUB using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class ConvertWordToEpub {
    public static void main(String[] args){
        //Create a Document instance
        Document doc = new Document();
        //Load a Word document
        doc.loadFromFile("Sample.docx");

        //Save the Word document to EPUB format
        doc.saveToFile("ToEpub.epub", FileFormat.E_Pub);
    }
}

Java: Convert Word to EPUB

Convert Word to EPUB with a Cover Image

The following are the steps to convert a Word document to EPUB with a cover image:

  • Create a Document instance.
  • Load a Word document using Document.loadFromFile() method.
  • Create a DocPicture instance.
  • Load an image using DocPicture.loadImage() method.
  • Save the Word document to EPUB with cover image using Document.saveToEpub(String, DocPicture) method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.fields.DocPicture;

public class ConvertWordToEpubWithCoverImage {
    public static void main(String[] args){
        //Create a Document instance
        Document doc = new Document();
        //Load a Word document
        doc.loadFromFile("Sample.docx");

        //Create a DocPicture instance
        DocPicture picture = new DocPicture(doc);
        //Load an image
        picture.loadImage("Cover.png");

        //Save the Word document to EPUB with cover image
        doc.saveToEpub("ToEpubWithCoverImage.epub", picture);
    }
}

Java: Convert Word to EPUB

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 102

Coupon Code Copied!

Christmas Sale

Celebrate the season with exclusive savings

Save 10% Sitewide

Use Code:

View Campaign Details