Here use APACHE POI jar file: poi-3.6 or above version
import java.io.*;
import org.apache.poi.hssf.usermodel.*;
/**
*
* @author sookie
*/
public class PasswordProtectedExcelSheet {
public static void main(String arg[]) {
try{
FileOutputStream out = new FileOutputStream("C:\\images\\Sample.xls");
/* Creates the workbook */
HSSFWorkbook hssfworkbook = new HSSFWorkbook();
// hssfworkbook.writeProtectWorkbook("xyz","xyz"); --> shows same behaviour even if uncommented
/* Creates a sample sheet with name as Crazyengineer */
HSSFSheet sheet = hssfworkbook.createSheet("Crazy Engineer");
String CEAN_NAME = "sookie";
/* Creates first row */
HSSFRow row = sheet.createRow(0);
/* Creates a first cell */
HSSFCell cell =row.createCell((short)0);
/* Sets the value in first cell */
cell.setCellValue(CEAN_NAME);
/* Sets the password for the sheet */
sheet.protectSheet("laxman");
/* Writes to the File Output stream */
hssfworkbook.write(out);
out.close();
} catch(Exception e){
e.printStackTrace();
System.out.println("Exception occured");
}
}
}
This will only make the sheet write protected. Does not make the xls read protected
ReplyDelete