Simple Java Code To Read Password Protected XLSX File
package com.techhelpline.PractiseCode;
import java.io.File;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.poi.poifs.crypt.Decryptor;
import org.apache.poi.poifs.crypt.EncryptionInfo;
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;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadProtectedFileXLSX {
public static void main(String[] args) {
String inputFilePath = "UserDetails.xlsx";
String password = "test";
try {
NPOIFSFileSystem fileSystem = new NPOIFSFileSystem(new File(inputFilePath));
EncryptionInfo info = new EncryptionInfo(fileSystem);
Decryptor decryptor = Decryptor.getInstance(info);
if (!decryptor.verifyPassword(password)) {
System.out.println("File in password protected.");
return;
}
InputStream dataStream = decryptor.getDataStream(fileSystem);
Workbook workbook = new XSSFWorkbook(dataStream);
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;
default:
break;
}
System.out.print("\t");
}
System.out.println();
}
workbook.close();
dataStream.close();
fileSystem.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
import java.io.File;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.poi.poifs.crypt.Decryptor;
import org.apache.poi.poifs.crypt.EncryptionInfo;
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;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadProtectedFileXLSX {
public static void main(String[] args) {
String inputFilePath = "UserDetails.xlsx";
String password = "test";
try {
NPOIFSFileSystem fileSystem = new NPOIFSFileSystem(new File(inputFilePath));
EncryptionInfo info = new EncryptionInfo(fileSystem);
Decryptor decryptor = Decryptor.getInstance(info);
if (!decryptor.verifyPassword(password)) {
System.out.println("File in password protected.");
return;
}
InputStream dataStream = decryptor.getDataStream(fileSystem);
Workbook workbook = new XSSFWorkbook(dataStream);
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;
default:
break;
}
System.out.print("\t");
}
System.out.println();
}
workbook.close();
dataStream.close();
fileSystem.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Comments
Post a Comment