Think I may have sorted it out James; sequencing is the issue IMO.

This code works to lock the structure of the workbook;

public static void main(String[] args) {
        File file = null;
        FileOutputStream fos = null;
        XSSFWorkbook workbook = null;
        XSSFSheet sheet = null;
        XSSFRow row = null;
        XSSFCell cell = null;
        try {

            workbook = new XSSFWorkbook();
            sheet = workbook.createSheet("To test protection.");
            row = sheet.createRow(0);
            cell = row.createCell(0);
            cell.setCellValue(123.456);

            file = new File("C:/temp/Protection Test.xlsx");
            fos = new FileOutputStream(file);
            
            sheet.lockDeleteColumns();
            sheet.lockDeleteRows();
            sheet.lockFormatCells();
            sheet.lockFormatColumns();
            sheet.lockFormatRows();
            sheet.lockInsertColumns();
            sheet.lockInsertRows();
            sheet.enableLocking();

            workbook.lockStructure();

            workbook.write(fos);
        }
        catch(IOException ioEx) {
            System.out.println("Caught an: " + ioEx.getClass().getName());
            System.out.println("Message: " + ioEx.getMessage());
            System.out.println("Stacktrace follows:...........");
            ioEx.printStackTrace(System.out);
        }
        finally {
            if(fos != null) {
                try {
                    fos.close();
                }
                catch(IOException ioEx) {
                    // Nothing can be done by this point.
                }
            }
        }
    }

As you will see, I have locked pretty much everything which I doubt you want
to do but at least you can play around to find out which of the lockXXX()
methods to call. It seems as if the call to the enableLocking() method is
the key, it has to follow the call to one or more of the lockXXX() methods.

Hope that helps.

Yours

Mark B

PS I tested this code using th very latest release of the API - 3.7
Scratchpad.



James Fedor wrote:
> 
> I'm new to this group, so I apologize if this question has already been
> answered. Thanks in advance for any assistance.
> 
> Question:
> 
> http://stackoverflow.com/questions/2142232/poi-xssf-api-how-do-i-lock-specific-excel-sheet-features
> 
> 
> I'm using the POI-XSSF API<http://poi.apache.org/spreadsheet/index.html>
> to write Excel 2007 documents, and i'm having trouble getting specific
> facets of a given sheet to lock. The API says
> that<http://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFSheet.html#lockDeleteColumns%28%29>
> you need to call enableLocking() on the sheet but when I do that it locks
> everything and ignores my specific locking settings (example below). When
> I don't call it nothing is locked.
> 
> It seems this must work given the fact they provided specific methods to
> give one granular locking control at a sheet level. So how can I lock
> specific facets of a workbook/worksheet using this API?
> 
> 
> XSSFWorkbook wb = new XSSFWorkbook();
> XSSFSheet sheet = wb.createSheet("Sheet1");
> XSSFCell cell;
> 
> XSSFRow row = sheet.createRow(0);
> cell = row.createCell(0, XSSFCell.CELL_TYPE_STRING);
> cell.setCellValue("Sku");
> cell = row.createCell(1, XSSFCell.CELL_TYPE_STRING);
> cell.setCellValue("Category");
> cell = row.createCell(2, XSSFCell.CELL_TYPE_STRING);
> cell.setCellValue("SubCategory");
> cell = row.createCell(3, XSSFCell.CELL_TYPE_STRING);
> cell.setCellValue("Name");
> cell = row.createCell(4, XSSFCell.CELL_TYPE_STRING);
> cell.setCellValue("DATE_ADDED");
> cell = row.createCell(5, XSSFCell.CELL_TYPE_STRING);
> cell.setCellValue("SEO_DESCRIPTION");
> 
> row = sheet.createRow(1);
> cell = row.createCell(0, XSSFCell.CELL_TYPE_STRING);
> cell.setCellValue("4bd2c24571534e098589");
> cell = row.createCell(1, XSSFCell.CELL_TYPE_STRING);
> cell.setCellValue("CLUBS");
> cell = row.createCell(2, XSSFCell.CELL_TYPE_STRING);
> cell.setCellValue("DRIVERS");
> cell = row.createCell(3, XSSFCell.CELL_TYPE_STRING);
> cell.setCellValue("Super dooper pooper 1112222 -- ASFASDF");
> cell = row.createCell(4, XSSFCell.CELL_TYPE_STRING); cell.setCellValue(new
> Date());
> cell = row.createCell(5, XSSFCell.CELL_TYPE_STRING);
> cell.setCellValue("asdfhavsdvf absdfhvbashdv asvdfhavsfasdf");
> 
> sheet.lockDeleteColumns();
> sheet.lockInsertColumns();
> sheet.lockDeleteRows();
> 
> sheet.enableLocking(); // why does this call ignore the last 3 lock
> settings i just set???????????
> 
> OutputStream out;
> try {
>     out = new FileOutputStream("c:\\temp\\TestWB.xlsx", false);
>     wb.write(out);
> } catch (Exception e) {
>     // TODO Auto-generated catch block
>     e.printStackTrace();
> }
> 
> 
> 
> 
> 
> 
> 

-- 
View this message in context: 
http://old.nabble.com/POI-XSSF-API-how-do-I-lock-specific-Excel-sheet-features--tp27332341p27622011.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to