page 107

To extract values from an Excel document, you can copy and paste cell data. Alternatively, you can obtain it automatically by utilizing Java code, which will not only save time and improve efficiency, but ensure there will be no errors. In this tutorial, you will learn how to extract the value of a specified cell by its name 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.12.15</version>
    </dependency>
</dependencies>

Get Cell Values by Cell Names in Excel

Spire.XLS for Java offers Worksheet.getRange().get() method to specify a cell in Excel by its name, and CellRange.getValue() method to obtain the cell value. The detailed steps are listed as below.

  • Create a Workbook instance.
  • Load an Excel sample document using Workbook.loadFromFile() method.
  • Get a specified worksheet using Workbook.getWorksheets().get() method.
  • Get a specific cell by its name using Worksheet.getRange().get() method.
  • Create a StringBulider instance.
  • Obtain the cell value using CellRange.getValue() method, and then append the value to the StringBuilder instance using StringBuilder.append() method.
  • Java
import com.spire.xls.*;

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

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

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

        //Get the specified cell by its name
        CellRange cell = sheet.getRange().get("D6");
       
        //Create a StringBuilder instance
        StringBuilder content = new StringBuilder();

        //Get value of the cell "D6" 
        content.append("The value of cell D6 is: " + cell.getValue()+"\n");
 
//Output the result
        System.out.println(content);
    }
}

The input Excel:

Java: Get Cell Values by Cell Names in Excel

The output result:

Java: Get Cell Values by Cell Names 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.

Spire.Doc for Java allows developers to convert Word documents to password-protected PDF documents by using the Document.saveToFile(String, ToPdfParameterList) method. The ToPdfParameterList parameter controls how a Word document will be converted to PDF, for example, whether to encrypt the document while converting.

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>14.1.3</version>
    </dependency>
</dependencies>

Convert Word to Password-Protected PDF

The following are the steps to convert a Word document to password-protected PDF:

  • Create a Document instance.
  • Load a Word document using Document.loadFromFile() method.
  • Create a ToPdfParameterList instance.
  • Set open password and permission password for PDF using ToPdfParameterList.getPdfSecurity().encrypt() method.
  • Save the Word document to PDF with password using Document.saveToFile(String, ToPdfParameterList) method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.ToPdfParameterList;
import com.spire.pdf.security.PdfEncryptionKeySize;
import com.spire.pdf.security.PdfPermissionsFlags;

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

        //Create a Document instance
        Document document = new Document(false);
        //Load a Word document
        document.loadFromFile("Sample.docx");

        //Create a ToPdfParameterList instance
        ToPdfParameterList toPdf = new ToPdfParameterList();
        //Set open password and permission password for PDF
        String password = "password";
        toPdf.getPdfSecurity().encrypt(password, password, PdfPermissionsFlags.None, PdfEncryptionKeySize.Key_128_Bit);

        //Save the Word document to PDF with password
        document.saveToFile("ToPdfWithPassword.pdf", toPdf);
    }
}

Java: Convert Word to Password-Protected PDF

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.

Spire.Doc offers Table.applyVerticalMerge() method to merge table cells vertically and Table.applyHorizontalMerge() method to merge table cells horizontally. By default, the merged cells will have repeated values if the cells to be merged contain the same value. This article will demonstrate how to remove repeated values in the merged cells using a customized method with 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>14.1.3</version>
    </dependency>
</dependencies>

Remove Duplicate Values When Merging Cells

The following are the steps to remove the duplicate values in the merged cells in a Word table.

  • Create an object of Document class and load the sample document using Document.loadFromFile() method.
  • Use Document.getSections() method to get the section collection, and then get the specific section using SectionCollection.get() method.
  • Use Section.getTables() method to get the table collection, and then get the desired table using TableCollection.get() method
  • Invoke mergeCell(Table table, boolean isHorizontalMerge, int index, int start, int end) method to merge table cells vertically or horizontally. This method will determine whether the cells to be merged have the same value, and will preserve only one value in the merged cell.
  • Save the document to file using Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.interfaces.ITable;

    public class MergeCells {
    public static void main(String[] args) throws Exception {

        //Create an object of Document class and load the sample document.
        Document document = new Document();
        document.loadFromFile("Sample.docx");

        //Get the first section
        Section section = document.getSections().get(0);

        //Get the first table
        Table table = section.getTables().get(0);

        //Invoike mergeCell()method to merge cells vertically
        mergeCell(table, false, 0, 1, 3);

        //Invoike mergeCell()method to merge cell horizontally
        mergeCell(table, true, 0, 4, 5);

        //Save the document to file
        document.saveToFile("MergeTable.docx",FileFormat.Docx_2013);
}

        //Customize a mergeCell() method to remove the duplicate values while merging cells
        public static void mergeCell(Table table, boolean isHorizontalMerge, int index, int start, int end) {
        
        if (isHorizontalMerge) {
            //Get a cell from table
            TableCell firstCell = table.get(index, start);
            //Invoke getCellText() method to get the cell’s text
            String firstCellText = getCellText(firstCell);
            for (int i = start + 1; i <= end; i++) {
                TableCell cell1 = table.get(index, i);
                //Check if the text is the same as the first cell                
        if (firstCellText.equals(getCellText(cell1))) {
                    //If yes, clear all the paragraphs in the cell
                    cell1.getParagraphs().clear();
                }
            }
            //Merge cells horizontally
            table.applyHorizontalMerge(index, start, end);

        } 
            else {
            TableCell firstCell = table.get(start, index);
            String firstCellText = getCellText(firstCell);
            for (int i = start + 1; i <= end; i++) {
                TableCell cell1 = table.get(i, index);
                if (firstCellText.equals(getCellText(cell1))) {
                    cell1.getParagraphs().clear();
                }
            }
            //Merge cells vertically
            table.applyVerticalMerge(index, start, end);
        }
    }
        public static String getCellText(TableCell cell) {

        StringBuilder text = new StringBuilder();
        //Traverse all the paragraphs of a cell
        for (int i = 0; i < cell.getParagraphs().getCount(); i++) {
            //Get every paragraph’s text and append it to StringBuilder
            text.append(cell.getParagraphs().get(i).getText().trim());
        }
        return text.toString();
    }
}

Java: Remove Duplicate Values When Merging Cells in Word

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 107