Simple Java Code To Read Password Protected XLS File

package com.techhelpline.PractiseCode;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class ReadProtectedFileXLS {
public static void main(String[] args) throws IOException {

String inputFilePath = "UserDetails.xls";
String password="test";

NPOIFSFileSystem fileSystem = new NPOIFSFileSystem(new File(inputFilePath), true);

Biff8EncryptionKey.setCurrentUserPassword(password);

Workbook workbook = new HSSFWorkbook(fileSystem);

Sheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = firstSheet.iterator();

while (iterator.hasNext()) {
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();

while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();

switch (cell.getCellTypeEnum()) {
case STRING:
System.out.print(cell.getStringCellValue());
break;
case NUMERIC:
System.out.print(cell.getNumericCellValue());
break;
}
System.out.print("\t");
}
System.out.println();
}

workbook.close();
fileSystem.close();
}
}

Comments

Popular Post

MCQ questions for graphics class method in Java

Session 2 TestNG Framework | How to create TestNG Project In Eclipse IDE | With Selenium Test Cases

Print Hello World In Different Programming Languages | Boost Coding Skill | Developer Life

How to run Selenium script when system is locked?

How to Fix Your Connection is Not Private Error in Google Chrome | SSL Certificate Issue | JAVA | Eclipse