Skip to main content
Announcements
Introducing Qlik Answers: A plug-and-play, Generative AI powered RAG solution. READ ALL ABOUT IT!
cancel
Showing results for 
Search instead for 
Did you mean: 
ycham006
Contributor
Contributor

How to run this java code

Hello Team,

 

Please can you help me. I wana run this code in Talend. The objectif is adding Password to xlsx File.

 

 

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;

public class ProtectedExcelFile {

public static void main(final String... args) throws Exception {

String fname = "C:\\Users\\ycham006\\Desktop\\REFRENTIEL_ITEM_BYTEL.xlsx";

FileInputStream fileInput = null;
BufferedInputStream bufferInput = null;
POIFSFileSystem poiFileSystem = null;
FileOutputStream fileOut = null;

try {
fileInput = new FileInputStream(fname);
bufferInput = new BufferedInputStream(fileInput);
poiFileSystem = new POIFSFileSystem(bufferInput);

Biff8EncryptionKey.setCurrentUserPassword("secret");
HSSFWorkbook workbook = new HSSFWorkbook(poiFileSystem, true);
HSSFSheet sheet = workbook.getSheetAt(0);

HSSFRow row = sheet.createRow(0);
Cell cell = row.createCell(0);

cell.setCellValue("THIS WORKS!");

fileOut = new FileOutputStream(fname);
workbook.writeProtectWorkbook(Biff8EncryptionKey.getCurrentUserPassword(), "");
workbook.write(fileOut);
}
catch (Exception ex) {

System.out.println(ex.getMessage());
}
finally {
try {

bufferInput.close();
}
catch (IOException ex) {

System.out.println(ex.getMessage());
}

try {

fileOut.close();
}
catch (IOException ex) {

System.out.println(ex.getMessage());
}
}
}
}

Labels (3)
1 Reply
TRF
Champion II
Champion II

You should create a routine.
Here is a tutorial
https://www.talendbyexample.com/talend-code-routines-reference.html