C#/VB.NET: Delete Blank Cells, Rows and Columns in Excel

Sometimes, you may find it difficult to read or edit data in an Excel worksheet because it contains blank cells, rows or columns. It is possible to delete the blank cells, rows and columns manually, however, if the file contains a large amount of data, you may waste a lot of time doing so. In this article, I will demonstrate how to delete blank cells, rows and columns in Excel programmatically using C# and VB.NET.

Installation

In order to work with Excel, I will be using Free Spire.XLS for .NET, which is a free and easy-to-use API for creating, editing, converting and printing Excel files.

The DLL files of Free Spire.XLS for .NET can be either downloaded from the official website or installed via NuGet by selecting Tools > NuGet Package Manager > Package Manager Console and then add the following code:

PM> Install-Package FreeSpire.XLS

Delete Blank Cells in Excel in C# and VB.NET

The following are the steps to delete blank cells in Excel:

  • Create an instance of Workbook class.
  • Load the Excel file using Workbook.LoadFromFile() method.
  • Get the worksheet that contains blank cells using Workbook.Worksheets[sheetIndex] property.
  • Get the row count and column count using Worksheet.Rows.Count() and Worksheet.Columns.Count() methods.
  • Loop through the rows and columns, access the current cell using Worksheet.Range[rowIndex, columnIndex] property. Note the row index and column index here are 1-based.
  • Determine if the current cell is blank or not using XlsRange.IsBlank Property. If any blank cell is found, remove it using XlsWorksheet.DeleteRange(CellRange, DeleteOption) method.
  • Save the result file using Workbook.SaveToFile() method.

C#

using Spire.Xls;
using System.Linq;

namespace DeleteBlankCells
{
    class Program
    {
        static void Main(string[] args)
        {
            //Instantiate a Workbook object
            Workbook workbook = new Workbook();
            //Load the Excel file
            workbook.LoadFromFile("Sample1.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Get the row count
            int maxRow = sheet.Rows.Count();
            //Get the column count
            int maxColumn = sheet.Columns.Count();

            //Loop through the rows and columns
            for (int i = maxRow - 1; i >= 0; i--)
            {
                for (int j = maxColumn - 1; j >= 0; j--)
                {
                    //Get the current cell
                    CellRange cell = sheet.Range[i + 1, j + 1];
                    //Determine if the cell is blank or not
                    if (cell.IsBlank)
                    {
                        //Remove the blank cell with DeletOption (MoveLeft/MoveUp)
                        sheet.DeleteRange(cell, DeleteOption.MoveLeft);
                    }
                }
            }

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

VB.NET

Imports Spire.Xls

Namespace DeleteBlankCells
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Instantiate a Workbook object
            Dim workbook As Workbook = New Workbook()
            'Load the Excel file
            workbook.LoadFromFile("Sample1.xlsx")

            'Get the first worksheet
            Dim sheet As Worksheet = workbook.Worksheets(0)

            'Get the row count
            Dim maxRow As Integer = sheet.Rows.Count()
            'Get the column count
            Dim maxColumn As Integer = sheet.Columns.Count()

            'Loop through the rows and columns
            For i = maxRow - 1 To 0 Step -1

                For j = maxColumn - 1 To 0 Step -1
                    'Get the current cell
                    Dim cell As CellRange = sheet.Range(i + 1, j + 1)
                    'Determine if the cell is blank or not
                    If cell.IsBlank Then
                        'Remove the blank cell with DeletOption (MoveLeft/MoveUp)
                        sheet.DeleteRange(cell, DeleteOption.MoveLeft)
                    End If
                Next
            Next

            'Save the result file
            workbook.SaveToFile("DeleteBlankCells.xlsx", ExcelVersion.Version2013)
        End Sub
    End Class
End Namespace
Delete blank cells in Excel in C# and VB.NET

Delete Blank Rows in Excel in C# and VB.NET

The following are the steps to delete blank rows in Excel:

  • Create an instance of Workbook class.
  • Load the Excel file using Workbook.LoadFromFile() method.
  • Get the worksheet that contains blank rows using Workbook.Worksheets[sheetIndex] property.
  • Get the row count using Worksheet.Rows.Count() method.
  • Loop through the rows in the worksheet.
  • Determine whether the current row is blank or not using Worksheet.Rows[rowIndex].IsBlank property. If any blank row is found, remove it using XlsWorksheet.DeleteRow(rowIndex) method (note that the row index in the DeleteRow method starts from 1).
  • Save the result file using Workbook.SaveToFile() method.

C#

using Spire.Xls;
using System.Linq;

namespace DeleteBlankRows
{
    class Program
    {
        static void Main(string[] args)
        {
            //Instantiate a Workbook object
            Workbook workbook = new Workbook();
            //Load the Excel file
            workbook.LoadFromFile("Sample2.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];
            //Get the row count
            int rowCount = sheet.Rows.Count();

            //Loop through the rows 
            for (int i = rowCount - 1; i >= 0; i--)
            {
                //Determine if the row is blank or not
                if (sheet.Rows[i].IsBlank)
                {
                    //Delete blank row
                    sheet.DeleteRow(i + 1); //Index parameter in DeleteRow method starts from 1
                }
            }


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

VB.NET

Imports Spire.Xls

Namespace DeleteBlankRows
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Instantiate a Workbook object
            Dim workbook As Workbook = New Workbook()
            'Load the Excel file
            workbook.LoadFromFile("Sample2.xlsx")

            'Get the first worksheet
            Dim sheet As Worksheet = workbook.Worksheets(0)
            'Get the row count
            Dim rowCount As Integer = sheet.Rows.Count()

            'Loop through the rows 
            For i = rowCount - 1 To 0 Step -1
                'Determine if the row is blank or not
                If sheet.Rows(i).IsBlank Then
                    'Delete blank row
                    sheet.DeleteRow(i + 1) 'Index parameter in DeleteRow method starts from 1
                End If
            Next

            'Save the result file
            workbook.SaveToFile("DeleteBlankRows.xlsx", ExcelVersion.Version2013)
        End Sub
    End Class
End Namespace
Delete blank rows in Excel in C# and VB.NET

Delete Blank Columns in Excel in C# and VB.NET

The steps to delete blank columns is very similar to the steps of deleting blank rows, so I’m going to omit it here.

C#

using Spire.Xls;
using System.Linq;

namespace DeleteBlankRowsAndColumns
{
    class Program
    {
        static void Main(string[] args)
        {
            //Instantiate a Workbook object
            Workbook workbook = new Workbook();
            //Load the Excel file
            workbook.LoadFromFile("Sample3.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];
            //Get the column count
            int columnCount = sheet.Columns.Count();

            //Loop through the columns 
            for (int j = columnCount - 1; j >= 0; j--)
            {
                //Determine if the column is blank or not
                if (sheet.Columns[j].IsBlank)
                {
                    //Delete blank column
                    sheet.DeleteColumn(j + 1); //Index parameter in DeleteColumn method starts from 1
                }
            }

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

VB.NET

Imports Spire.Xls

Namespace DeleteBlankRowsAndColumns
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Instantiate a Workbook object
            Dim workbook As Workbook = New Workbook()
            'Load the Excel file
            workbook.LoadFromFile("Sample3.xlsx")

            'Get the first worksheet
            Dim sheet As Worksheet = workbook.Worksheets(0)
            'Get the column count
            Dim columnCount As Integer = sheet.Columns.Count()

            'Loop through the columns 
            For j = columnCount - 1 To 0 Step -1
                'Determine if the column is blank or not
                If sheet.Columns(j).IsBlank Then
                    'Delete blank column
                    sheet.DeleteColumn(j + 1) 'Index parameter in DeleteColumn method starts from 1
                End If
            Next

            'Save the result file
            workbook.SaveToFile("DeleteBlankColumns.xlsx", ExcelVersion.Version2013)
        End Sub
    End Class
End Namespace
Delete blank columns in Excel in C# and VB.NET

See More

Product Page | Documentation | Examples | Forum |

C#/VB.NET: Merge Multiple Excel Workbooks or Worksheets into One

Sometimes, we may need to merge Excel files of the same type or category into one so that we can analyze and calculate data more easily. However, merging the files one by one manually is very annoying and time-consuming. In this article, I will demonstrate how to programmatically merge multiple Excel files into one in C# and VB.NET.

The following two topics will be covered:

  • Merge Multiple Excel Workbooks into One in C# and VB.NET
  • Merge Multiple Excel Worksheets into One in C# and VB.NET

Installation

In order to merge Excel files, I will be using Free Spire.XLS for .NET, which is a free and easy-to-use API for creating, editing, converting and printing Excel files.

The DLL files of Free Spire.XLS for .NET can be either downloaded from the official website or installed via NuGet by selecting Tools > NuGet Package Manager > Package Manager Console and then add the following code:

PM> Install-Package FreeSpire.XLS

Merge Multiple Excel Workbooks into One in C# and VB.NET

The following are the steps to merge multiple Excel workbooks into one:

  • Create a string array from the Excel file paths.
  • Initialize a Workbook object to create a new Excel workbook, and clear the default worksheets in the workbook using Workbook.Worksheets.Clear() method.
  • Initialize another temporary Workbook object.
  • Loop through the string array, load the current workbook into the temporary Workbook object using Workbook.LoadFromFile() method.
  • loop through the worksheets in the current workbook, then copy each worksheet from the current workbook to the new workbook using Workbook.Worksheets.AddCopy() method.
  • Save the new workbook to file using Workbook.SaveToFile() method.

C#

using Spire.Xls;

namespace MergeExcelFiles
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a string array from Excel file paths
            string[] inputFiles = new string[] { "April.xlsx", "May.xlsx", "June.xlsx" };

            //Initialize a new Workbook object
            Workbook newWorkbook = new Workbook();
            //Clear the default worksheets
            newWorkbook.Worksheets.Clear();

            //Initialize another temporary Workbook object
            Workbook tempWorkbook = new Workbook();

            //Loop through the string array
            foreach (string file in inputFiles)
            {
                //Load the current workbook
                tempWorkbook.LoadFromFile(file);
                //Loop through the worksheets in the current workbook
                foreach (Worksheet sheet in tempWorkbook.Worksheets)
                {
                    //Copy each worksheet from the current workbook to the new workbook
                    newWorkbook.Worksheets.AddCopy(sheet, WorksheetCopyType.CopyAll);
                }
            }

            //Save the new workbook to file
            newWorkbook.SaveToFile("MergeWorkbooks.xlsx", ExcelVersion.Version2013);
        }
    }
}

VB.NET

Imports Spire.Xls

Namespace MergeExcelFiles
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a string array from Excel file paths
            Dim inputFiles = New String() {"April.xlsx", "May.xlsx", "June.xlsx"}

            'Initialize a new Workbook object
            Dim newWorkbook As Workbook = New Workbook()
            'Clear the default worksheets
            newWorkbook.Worksheets.Clear()

            'Initialize another temporary Workbook object
            Dim tempWorkbook As Workbook = New Workbook()

            'Loop through the string array
            For Each file In inputFiles
                'Load the current workbook
                tempWorkbook.LoadFromFile(file)
                'Loop through the worksheets in the current workbook
                For Each sheet As Worksheet In tempWorkbook.Worksheets
                    'Copy each worksheet from the current workbook to the new workbook
                    newWorkbook.Worksheets.AddCopy(sheet, WorksheetCopyType.CopyAll)
                Next
            Next

            'Save the new workbook to file
            newWorkbook.SaveToFile("MergeWorkbooks.xlsx", ExcelVersion.Version2013)
        End Sub
    End Class
End Namespace

The input Excel workbooks:

The input Excel files

The merged Excel workbook:

The merged Excel file

Merge Multiple Excel Worksheets into One in C# and VB.NET

We can merge multiple worksheets in the same or different workbooks into one. The following steps show how to merge two Excel worksheets in the same workbook into a single worksheet:

  • Initialize a Workbook object and load an Excel file using Workbook.LoadFromFile() method.
  • Get the two worksheets that need to be merged using Workbook.Worksheets[sheetIndex] property. Note the sheet index is zero-based.
  • Get the used range of the second worksheet using Worksheet.AllocatedRange property.
  • Specify the destination range in the first worksheet using Worksheet.Range[rowIndex, columnIndex] property. Note the row and column indexes are 1-based.
  • Copy the used range of the second worksheet to the destination range in the first worksheet using CellRange.Copy(destRange) method.
  • Remove the second worksheet using XlsWorksheet.Remove() method.
  • Save the result file using Workbook.SaveToFile() method.

C#

using Spire.Xls;

namespace MergeExcelWorksheets
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook object
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("Sample.xlsx");

            //Get the first worksheet
            Worksheet sheet1 = workbook.Worksheets[0];
            //Get the second worksheet
            Worksheet sheet2 = workbook.Worksheets[1];

            //Get the used range in the second worksheet
            CellRange sourceRange = sheet2.AllocatedRange;
            //Specify the destination range in the first worksheet
            CellRange destRange = sheet1.Range[sheet1.LastRow + 1, 1];

            //Copy the used range of the second worksheet to the destination range in the first worksheet
            sourceRange.Copy(destRange);

            //Remove the second worksheet
            sheet2.Remove();

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

VB.NET

Imports Spire.Xls

Namespace MergeExcelWorksheets
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a Workbook object
            Dim workbook As Workbook = New Workbook()
            'Load an Excel file
            workbook.LoadFromFile("Sample.xlsx")

            'Get the first worksheet
            Dim sheet1 As Worksheet = workbook.Worksheets(0)
            'Get the second worksheet
            Dim sheet2 As Worksheet = workbook.Worksheets(1)

            'Get the used range in the second worksheet
            Dim sourceRange As CellRange = sheet2.AllocatedRange
            'Specify the destination range in the first worksheet
            Dim destRange As CellRange = sheet1.Range(sheet1.LastRow + 1, 1)

            'Copy the used range of the second worksheet to the destination range in the first worksheet
            sourceRange.Copy(destRange)

            'Remove the second worksheet
            sheet2.Remove()

            'Save the result file
            workbook.SaveToFile("MergeWorksheets.xlsx", ExcelVersion.Version2013)
        End Sub
    End Class
End Namespace

The input Excel worksheets:

The input Excel worksheets

The merged Excel worksheet:

The merged Excel worksheet

See More

Product Page | Documentation | Examples | Forum |

Java: Convert PDF to PDF/A and Determine if a PDF is PDF/A

PDF/A is an archiving format of PDF that ensures a document with text, vector graphics, raster images and related metadata can be faithfully reproduced after a long-term preservation. Under certain circumstances, you may need to convert a regular PDF file to PDF/A or determine if a PDF is already a PDF/A file. In this article, I will demonstrate how to convert PDF to PDF/A, to be more specific, PDF/A-1a, PDF/A-1b, PDF/A-2a, PDF/A-2b, PDF/A-3a, PDF/A-3b, and how to determine if a PDF is PDF/A in java.

Add Dependencies

In order to convert PDF to PDF/A, I will be using Spire.PDF for Java, which is an easy-to-use and multi-functional PDF library.

The jar file of Spire.PDF for Java can be downloaded from this link or installed from Maven by adding the following code to your maven-based 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.pdf</artifactId>
        <version>5.1.0</version>
    </dependency>
</dependencies>

Convert PDF to PDF/A

The following are the steps to convert a PDF file to PDF/A:

  • Create a PdfStandardsConverter instance, within the constructor, pass in the file path of the sample PDF file as a parameter.
  • Convert the sample file to PdfA1A conformance level using PdfStandardsConverter.toPdfA1A() method.
  • Convert the sample file to PdfA1B conformance level using PdfStandardsConverter. toPdfA1B() method.
  • Convert the sample file to PdfA2A conformance level using PdfStandardsConverter. toPdfA2A() method.
  • Convert the sample file to PdfA2B conformance level using PdfStandardsConverter. toPdfA2B() method.
  • Convert the sample file to PdfA3A conformance level using PdfStandardsConverter. toPdfA3A() method.
  • Convert the sample file to PdfA3B conformance level using PdfStandardsConverter. toPdfA3B() method.
import com.spire.pdf.conversion.PdfStandardsConverter;

public class ConvertPdfToPdfA {
    public static void main(String[] args) {
        //Load a PDF file using PdfStandardsConverter class
        PdfStandardsConverter converter = new PdfStandardsConverter("Input.pdf");

        //Convert to PdfA1A
        converter.toPdfA1A("output/ToPdfA1A.pdf");

        //Convert to PdfA1B
        converter.toPdfA1B("output/ToPdfA1B.pdf");

        //Convert to PdfA2A
        converter.toPdfA2A( "output/ToPdfA2A.pdf");

        //Convert to PdfA2B
        converter.toPdfA2B("output/ToPdfA2B.pdf");

        //Convert to PdfA3A
        converter.toPdfA3A("output/ToPdfA3A.pdf");

        //Convert to PdfA3B
        converter.toPdfA3B("output/ToPdfA3B.pdf");
    }
}

The converted PDF/A-1a document:

Convert PDF to PDF/A in Java

Determine if a PDF is PDF/A

You can use the getConformance() method of the PdfDocument class to get the PDF/A version and the conformance level of a PDF file. If it’s a PDF/A file, let’s say, PDF/A-1a, it returns “Pdf_A_1_A”. If it’s a regular PDF file, it returns “None”.

The following are the steps to determine if a PDF file is PDF/A:

  • Create a PdfDocument instance, within the constructor, pass in the file path of the sample PDF file as a parameter.
  • Get the PDF/A version and conformance level of the file using PdfDocument.getConformance() method.
import com.spire.pdf.PdfConformanceLevel;
import com.spire.pdf.PdfDocument;

public class DetectIfAPdfIsPdfa {
    public static void main(String[] args){
        //Load a Pdf document
        PdfDocument pdf = new PdfDocument("ToPdfA1A.pdf");
        //Get the PDF/A version and conformance level
        PdfConformanceLevel conformanceLevel = pdf.getConformance();
        System.out.println(conformanceLevel.toString());
    }
}

Output:

Determine if a PDF is PDF/A in Java

Conclusion

In this article, I have demonstrated how to convert PDF to PDF/A using Spire.PDF for Java library. Besides the PDF to PDF/A conversion, the library can also be used to convert PDF to Word, Excel, HTML, images and many more. You can explore more about it by visiting the documentation.

In case you have any question, you can post it on the forum.

Design a site like this with WordPress.com
Get started