In MS Excel, rows arrange objects from left to right and are identified by row numbers. While columns, on the contrary, arrange objects from top to bottom and are identified by column headers. When processing data in Excel, sometimes you may need to insert additional columns and rows in the middle of your data table, or delete unwanted columns and rows. In this article, you will learn how to complete the below tasks programmatically 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>16.4.1</version>
    </dependency>
</dependencies>

Insert a Row and a Column in Excel

Below are the steps to insert a blank row and a blank column in an Excel worksheet.

  • Create a Workbook instance.
  • Load a sample Excel file using Workbook.loadFromFile() method.
  • Get a specified worksheet using Workbook.getWorksheets().get() method.
  • Insert a row into the worksheet using Worksheet.insertRow(int rowIndex) method.
  • Insert a column into the worksheet using Worksheet.insertColumn(int columnIndex) method.
  • Save the result file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.*;

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

        //Load a sample Excel file
        workbook.loadFromFile("Sample.xlsx");

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

        //Insert a row into the worksheet
        worksheet.insertRow(4);

        //Insert a column into the worksheet
        worksheet.insertColumn(4);

        //Save the result file
        workbook.saveToFile("InsertRowAndColumn.xlsx", ExcelVersion.Version2013);
    }
}

Java: Insert or Delete Rows and Columns in Excel

Insert Multiple Rows and Columns in Excel

Below are the steps to insert multiple blank rows and columns in an Excel worksheet.

  • Create a Workbook instance.
  • Load a sample Excel file using Workbook.loadFromFile() method.
  • Get a specified worksheet using Workbook.getWorksheets().get() method.
  • Insert multiple rows into the worksheet using Worksheet.insertRow((int rowIndex, int rowCount) method.
  • Insert multiple columns into the worksheet using Worksheet.insertColumn(int columnIndex, int columnCount) method.
  • Save the result file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.*;

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

        //Load a sample Excel file
        workbook.loadFromFile("Sample.xlsx");

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

        //Insert multiple rows into the worksheet.
        worksheet.insertRow(5, 3);

        //Insert multiple columns into the worksheet.
        worksheet.insertColumn(4, 2);
        
        //Save the result file
        workbook.saveToFile("InsertRowsAndColumns.xlsx", ExcelVersion.Version2013);
    }
}

Java: Insert or Delete Rows and Columns in Excel

Delete a Specific Row and Column in Excel

Below are the steps to delete a specific row and column in an Excel worksheet.

  • Create a Workbook instance.
  • Load a sample Excel file using Workbook.loadFromFile() method.
  • Get a specified worksheet using Workbook.getWorksheets().get() method.
  • Delete a specific row from the worksheet using Worksheet.deleteRow(int index) method.
  • Delete a specific column from the worksheet using Worksheet.deleteColumn(int columnIndex) method.
  • Save the result file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.*;

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

        //Load the sample Excel file
        workbook.loadFromFile("Sample.xlsx");

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

        //Delete a specific row from the worksheet
        worksheet.deleteRow(4);

        //Delete a specific column from the worksheet
        worksheet.deleteColumn(1);

        //Save to file.
        workbook.saveToFile("DeleteRowAndColumn.xlsx", ExcelVersion.Version2013);

    }
}

Java: Insert or Delete Rows and Columns in Excel

Delete Multiple Rows and Columns in Excel

Below are the steps to delete multiple rows and columns in an Excel worksheet.

  • Create a Workbook instance.
  • Load a sample Excel file using Workbook.loadFromFile() method.
  • Get a specified worksheet using Workbook.getWorksheets().get() method.
  • Delete multiple rows from the worksheet using Worksheet.deleteRow(int index, int count) method.
  • Delete multiple columns from the worksheet using Worksheet.deleteColumn(int columnIndex, int count) method.
  • Save the result file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.*;

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

        //Load the sample Excel file
        workbook.loadFromFile("E:\\Files\\Sample.xlsx");

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

        //Delete multiple rows from the worksheet
        worksheet.deleteRow(5, 3);

        //Delete multiple columns from the worksheet
        worksheet.deleteColumn(5, 2);

        //Save to file.
        workbook.saveToFile("DeleteRowsAndColumns.xlsx", ExcelVersion.Version2013);

    }
}

Java: Insert or Delete Rows and Columns 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.

Java: Add Hyperlinks to Excel

2022-03-15 09:15:00 Written by Koohji

Hyperlinks can direct readers from one file to a web address, an email address or another file. A hyperlink can be added to text or an image. In this article, we will introduce how to add hyperlinks to Excel in Java using Spire.XLS for Java library.

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

Add Text Hyperlinks to Excel in Java

The following are the steps to add a text hyperlink to Excel in Java:

  • Create an instance of Workbook class.
  • Get the desired worksheet using Workbook.getWorksheets().get() method.
  • Access the specific cell that you want to add hyperlink to using Worksheet.getRange().get() method.
  • Add a hyperlink to the cell using Worksheet.getHyperLinks().add() method.
  • Set the type, display text and address for the hyperlink using XlsHyperLink.setType(), XlsHyperLink.setTextToDisplay() and XlsHyperLink.setAddress() methods.
  • Autofit column width using XlsWorksheet.autoFitColumn() method.
  • Save the result file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.*;

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

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

        //Add a text hyperlink that leads to a website
        CellRange cell1 = sheet.getRange().get("B3");
        HyperLink urlLink = sheet.getHyperLinks().add(cell1);
        urlLink.setType(HyperLinkType.Url);
        urlLink.setTextToDisplay("Link to a website");
        urlLink.setAddress("https://www.google.com/");

        //Add a text hyperlink that leads to an email address
        CellRange cell2 = sheet.getRange().get("E3");
        HyperLink mailLink = sheet.getHyperLinks().add(cell2);
        mailLink.setType(HyperLinkType.Url);
        mailLink.setTextToDisplay("Link to an email address");
        mailLink.setAddress("mailto:abc@outlook.com");

        //Add a text hyperlink that leads to an external file
        CellRange cell3 = sheet.getRange().get("B7");
        HyperLink fileLink = sheet.getHyperLinks().add(cell3);
        fileLink.setType(HyperLinkType.File);
        fileLink.setTextToDisplay("Link to an external file");
        fileLink.setAddress("C:\\Users\\Administrator\\Desktop\\Report.xlsx");

        //Add a text hyperlink that leads to a cell in another sheet
        CellRange cell4 = sheet.getRange().get("E7");
        HyperLink linkToSheet = sheet.getHyperLinks().add(cell4);
        linkToSheet.setType(HyperLinkType.Workbook);
        linkToSheet.setTextToDisplay("Link to a cell in sheet2");
        linkToSheet.setAddress("Sheet2!B5");

        //Add a text hyperlink that leads to a UNC address
        CellRange cell5 = sheet.getRange().get("B11");
        HyperLink uncLink = sheet.getHyperLinks().add(cell5);
        uncLink.setType(HyperLinkType.Unc);
        uncLink.setTextToDisplay("Link to a UNC address");
        uncLink.setAddress("\\\\192.168.0.121");

        //Autofit column width
        sheet.autoFitColumn(2);
        sheet.autoFitColumn(5);

        //Save to file
        workbook.saveToFile("AddTextHyperlinks.xlsx", ExcelVersion.Version2013);
    }
}

Java: Add Hyperlinks to Excel

Add Image Hyperlinks to Excel in Java

The following are the steps to add an image hyperlink to Excel in Java

  • Create an instance of Workbook class.
  • Get the desired worksheet using Workbook.getWorksheets().get() method.
  • Insert an image into the worksheet using Worksheet.getPictures().add() method and set column width and row height.
  • Add a hyperlink to the image using XlsBitmapShape.setHyperLink() method.
  • Save the result file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.ExcelPicture;
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class AddImageHyperlinks {
    public static void main(String []args){
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Insert an image into the worksheet
        ExcelPicture picture = sheet.getPictures().add(5, 3, "Logo.png");
        sheet.setRowHeight(5,60);
        sheet.setColumnWidth(3,11);

        //Add a hyperlink to the image
        picture.setHyperLink("https://www.e-iceblue.com", true);

        //Save the result file
        workbook.saveToFile("AddImageHyperlink.xlsx", ExcelVersion.Version2013);
    }
}

Java: Add Hyperlinks 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.

Java: Replace or Extract Images in Excel

2025-01-13 01:22:00 Written by Koohji

When it comes to managing images in Excel, the ability to replace or extract them efficiently is important. If there are outdated or incorrect images in Excel, replacing them ensures that your data remains accurate and up-to-date. While extracting images from Excel files can be useful when you need to reuse them in other documents or presentations. In this article, you will learn how to replace or extract images in Excel in Java 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>16.4.1</version>
    </dependency>
</dependencies>

Replace Images in Excel in Java

To replace a picture in Excel, you can load a new picture and then pass it as a parameter to the ExcelPicture.setPicture() method. The following are the detailed steps to replace an Excel image with another one.

  • Create a Workbook instance.
  • Load an Excel file using Workbook.loadFromFile() method.
  • Get the first worksheet using Workbook.getWorksheets().get() method.
  • Get the first picture in the worksheet using Worksheet.getPictures().get() method.
  • Load an image, and then pass it as a parameter to the ExcelPicture.setPicture() method to replace the original picture with the new one.
  • Save the result file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

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

        // Load an Excel file
        workbook.loadFromFile("ExcelImg.xlsx");

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

        //Get the first image in the worksheet
        ExcelPicture pic = sheet.getPictures().get(0);

        // Load an image
        BufferedImage bufferedImage = ImageIO.read(new File("logo.png"));

        // Replace the first image with the loaded one
        pic.setPicture(bufferedImage);

        // Save the result file
        workbook.saveToFile("ReplaceImage.xlsx", ExcelVersion.Version2016);
    }
}

Replace the first image in an Excel worksheet using Java

Extract Images from Excel in Java

With Spire.XLS for Java, you can also extract all images in an Excel worksheet at once. The following are the detailed steps.

  • Create a Workbook instance.
  • Load an Excel file using Workbook.loadFromFile() method.
  • Get the first worksheet using Workbook.getWorksheets().get() method.
  • Get all the images in the worksheet using Worksheet.getPictures() method.
  • Loop through to get each image, and then save them to a specified file path.
  • Java
import com.spire.xls.*;
import com.spire.xls.collections.PicturesCollection;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

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

        // Load an Excel file
        workbook.loadFromFile("Test.xlsx");

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

        // Get all the pictures in the worksheet
        PicturesCollection pic = sheet.getPictures();

        // Iterate through each image
        for (int i = pic.getCount() - 1; i >= 0; i--)
        {
            // Get a specified image
            ExcelPicture excelPic = pic.get(i);
            BufferedImage loImage = excelPic.getPicture();

            // Save the image to a specified file path
            File file = new File(String.format("ExtractImages\\Image-%d.png", i));
            ImageIO.write(loImage, "PNG", file);
        }
    }
}

Extract all images from an Excel worksheet using Java

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 142