Protecting Excel sheets using Apache POI

Hi All,

In this article I will show how to protect excel sheet using Apache POI, Apache POI, a project run by the Apache Software Foundation, and previously a sub-project of the Jakarta Project, provides pure Java libraries for reading and writing files in Microsoft Office formats, such as Word, PowerPoint and Excel.

The below programme will demonstrate on Excel :

public static void main(String arg[]) {
try{
//Specify the name of the outout file
FileOutputStream out=new FileOutputStream(new File(“protected.xls”));

//Create  the workbook object
HSSFWorkbook hssfworkbook = new HSSFWorkbook();

/* Creates a sample sheet with name as Sheet 1 */
HSSFSheet sheet = hssfworkbook.createSheet(“Sheet1”);

String CELL_DATA = “First cell data”;

/* 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(
CELL_DATA );

/* Sets the password for the sheet */
sheet.protectSheet(“myPassword”);

/* Writes to the File Output stream */
hssfworkbook.write(out);
out.close();

} catch(Exception e){
e.printStackTrace();
}

}

Hope this helps.


Thanks
R Vashi

Advertisements

2 thoughts on “Protecting Excel sheets using Apache POI

  1. Bunty Raut

    Hi Rakesh,

    One Request can you please add some XML releted critical issue which you faced in your work experince , i am new to on XML and my current project i am using much XMl .

    Bunty Raut

    Reply
    1. ®V Post author

      Hi Bunty,

      Sure there are plenty of issues we face while working XML like, parsing issue, DTD Validations, navigation issue, namespace issues,
      I will sure try to post more example on the same, Also you can post your issues which you are facing while handling XML Data. I would be very happy to solve the same.

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s