Showing posts with label Excel. Show all posts
Showing posts with label Excel. Show all posts

Friday, May 26, 2023

How to Iterate Over Rows and Cells of Excel file in Java - Example

To iterate over rows and cells of an Excel file in Java, you can use a library like Apache POI. Apache POI provides a powerful set of Java libraries for reading and writing Microsoft Office file formats, including Excel. 

Here's an example of how you can iterate over rows and cells of an Excel file using Apache POI in Java:


import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;

public class ExcelIteratorExample {
    public static void main(String[] args) {
        String filePath = "path/to/your/excel/file.xlsx";

        try (FileInputStream fis = new FileInputStream(filePath);
             Workbook workbook = new XSSFWorkbook(fis)) {

            Sheet sheet = workbook.getSheetAt(0); // Assuming the first sheet

            // Iterate over rows
            for (Row row : sheet) {
                // Iterate over cells
                for (Cell cell : row) {
                    CellValue cellValue = evaluateCell(cell);
                    System.out.print(cellValue + "\t");
                }
                System.out.println(); // Move to the next line after each row
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // Evaluate cell value based on its type
    private static CellValue evaluateCell(Cell cell) {
        FormulaEvaluator evaluator = cell.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
        return evaluator.evaluate(cell);
    }
}

Make sure to replace "path/to/your/excel/file.xlsx" with the actual path to your Excel file. In this example, we use the XSSFWorkbook class to read the Excel file. 

We open the file using FileInputStream, create an instance of XSSFWorkbook, and obtain the first sheet using getSheetAt(0). We then iterate over each row using a for-each loop and iterate over each cell within the row. 

The evaluateCell method is used to evaluate the value of each cell, considering its type (e.g., numeric, string, formula). 

Finally, we print the cell values, separating them with tabs, and move to the next line after each row. Remember to include the Apache POI dependencies in your project's classpath for this code to work.

Saturday, May 20, 2023

How to compare two lists of values in Microsoft Excel? Example

To compare two lists of values in Microsoft Excel, you can use the VLOOKUP or COUNTIF functions. Here's an example of how to do it:


Let's assume you have two lists of values in columns A and B, and you want to compare whether each value in column A exists in column B.

In an empty column (e.g., column C), enter the following formula in cell C1:


excel

=IF(ISNUMBER(VLOOKUP(A1, $B$1:$B$10, 1, FALSE)), "Exists", "Does Not Exist")

This formula uses the VLOOKUP function to search for the value in cell A1 in the range B1:B10. If a match is found, it returns a number, indicating that the value exists in column B. The ISNUMBER function is used to check if the result is a number, and the IF function displays "Exists" if it is true, or "Does Not Exist" if it is false.


Note: Adjust the range B1:B10 to match the actual range of your second list of values in column B.


Drag the formula from C1 down to the last row of your data in column A to apply the formula to the entire column.

Column C will now display "Exists" for the values in column A that are found in column B, and "Does Not Exist" for the values that are not found.


This method allows you to compare two lists of values and identify which values from the first list exist in the second list.

How to remove duplicates from Excel Columns? COUNTIF Example

To remove duplicates from an Excel column using the COUNTIF function, you can follow these steps:


Assuming your data is in column A, insert a new column next to it (e.g., column B) to store the unique values.

In cell B1, enter the following formula to check if the value in cell A1 is the first occurrence in the column:


excel

=IF(COUNTIF($A$1:A1, A1) = 1, A1, "")

This formula uses the COUNTIF function to count the occurrences of the value in cell A1 from A1 to the current row. If the count is 1, it means it's the first occurrence, so the value is displayed; otherwise, an empty string is returned.


Drag the formula from B1 down to the last cell in column B to apply the formula to the entire column.

Select the entire column B, copy it (Ctrl+C), and then paste it (Ctrl+V) as values back into column A (paste values only).

Now, column A will contain only the unique values from the original column. You can safely remove column B if you no longer need it.


This approach using the COUNTIF function effectively filters out duplicate values and retains only the unique ones in the column.