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();
}
}
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
Post a Comment