Example usage for org.apache.poi.hssf.usermodel HSSFSheet protectSheet

List of usage examples for org.apache.poi.hssf.usermodel HSSFSheet protectSheet

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFSheet protectSheet.

Prototype

@Override
public void protectSheet(String password) 

Source Link

Document

Sets the protection enabled as well as the password

Usage

From source file:com.dexter.fuelcard.mbean.UserMBean.java

public void offloadCards() {
    FacesContext curContext = FacesContextImpl.getCurrentInstance();
    if (getUploadItem() != null) {
        try {//w  w  w  .  j  a v a 2s .  c  o m
            HSSFWorkbook workbook = new HSSFWorkbook(getUploadItem().getInputstream());
            int sheetCount = workbook.getNumberOfSheets();
            if (sheetCount >= 1) {
                String password = getRandomDigitPassword();

                HSSFSheet sheet = workbook.getSheetAt(0); // first sheet should be the main sheet
                sheet.protectSheet(password);

                ByteArrayOutputStream byout = new ByteArrayOutputStream();
                workbook.write(byout);
                byout.close();

                CardRequest cr = new CardRequest();
                cr.setRequestRefNum(getActiveUser().getPartner().getCode() + "-" + password);
                cr.setAdditionalComment(getRequestComment());
                cr.setCrt_dt(new Date());
                cr.setExcelFile(byout.toByteArray());
                cr.setPartner(getActiveUser().getPartner());
                cr.setRequest_dt(new Date());
                cr.setRequestedBy(getActiveUser());
                cr.setRequestType("OFFLOAD-CARDS");
                cr.setStatus("PENDING");

                GeneralDAO gDAO = new GeneralDAO();
                gDAO.startTransaction();
                boolean ret = gDAO.save(cr);
                if (ret) {
                    gDAO.commit();
                    setRequestComment(null);
                    setMyPendingCardCancelRequests(null);
                    curContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Success",
                            "Request submitted successfully!"));

                    // Send email to user that submitted that their request is been treated
                    String body = "<html><body><p>Dear " + getActiveUser().getFullname()
                            + ",</p><p>Your request to offload cards has been recieved and will be treated as soon as possible. You will be notified of every progress.</p><p>Regards<br/>Fuel Card Platform</p></body></html>";
                    Emailer.sendEmail("fuelcard@sattrakservices.com",
                            new String[] { getActiveUser().getEmail() },
                            "Card OffLoad Request - " + cr.getRequestRefNum() + " Received", body);
                    // Send email to sattrak that request is submitted
                    byte[] doc = cr.getExcelFile();
                    String body2 = "<html><body><p>Hello,</p><p>A request to offload cards has been submitted. The document is attached.</p><p>Regards<br/>Fuel Card Platform</p></body></html>";
                    if (getSattrakPartner() != null && getSattrakPartner().getContactEmails() != null
                            && getSattrakPartner().getContactEmails().trim().length() > 0) {
                        String[] to = getSattrakPartner().getContactEmails().split(",");
                        Emailer.sendEmail("fuelcard@sattrakservices.com", to,
                                "Card OffLoad Request - " + cr.getRequestRefNum() + " Submitted", body2, doc,
                                cr.getRequestRefNum() + ".xls", "application/vnd.ms-excel");
                    }
                } else {
                    gDAO.rollback();
                    curContext.addMessage(null,
                            new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", gDAO.getMessage()));
                }
                gDAO.destroy();
            } else {
                curContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed",
                        "Excel document is not valid!"));
            }
        } catch (Exception ex) {
            curContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Severe",
                    "Please upload an excel document! Error: " + ex.getMessage()));
        }
    } else {
        curContext.addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed", "Please upload an excel document!"));
    }
}

From source file:com.dexter.fuelcard.mbean.UserMBean.java

public void loadCards() {
    FacesContext curContext = FacesContextImpl.getCurrentInstance();
    if (getUploadItem() != null) {
        try {//from   w w  w . j  a v a 2  s.c  o  m
            HSSFWorkbook workbook = new HSSFWorkbook(getUploadItem().getInputstream());
            int sheetCount = workbook.getNumberOfSheets();
            if (sheetCount >= 1) {
                String password = getRandomDigitPassword();

                HSSFSheet sheet = workbook.getSheetAt(0); // first sheet should be the main sheet
                sheet.protectSheet(password);

                ByteArrayOutputStream byout = new ByteArrayOutputStream();
                workbook.write(byout);
                byout.close();

                CardRequest cr = new CardRequest();
                cr.setRequestRefNum(getActiveUser().getPartner().getCode() + "-" + password);
                cr.setAdditionalComment(getRequestComment());
                cr.setCrt_dt(new Date());
                cr.setExcelFile(byout.toByteArray());
                cr.setPartner(getActiveUser().getPartner());
                cr.setRequest_dt(new Date());
                cr.setRequestedBy(getActiveUser());
                cr.setRequestType("LOAD-CARDS");
                cr.setStatus("PENDING");

                GeneralDAO gDAO = new GeneralDAO();
                gDAO.startTransaction();
                boolean ret = gDAO.save(cr);
                if (ret) {
                    gDAO.commit();
                    setRequestComment(null);
                    setMyPendingCardCancelRequests(null);
                    curContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Success",
                            "Request submitted successfully!"));

                    // Send email to user that submitted that their request is been treated
                    String body = "<html><body><p>Dear " + getActiveUser().getFullname()
                            + ",</p><p>Your request to load cards has been recieved and will be treated as soon as possible. You will be notified of every progress.</p><p>Regards<br/>Fuel Card Platform</p></body></html>";
                    Emailer.sendEmail("fuelcard@sattrakservices.com",
                            new String[] { getActiveUser().getEmail() },
                            "Card Load Request - " + cr.getRequestRefNum() + " Received", body);
                    // Send email to sattrak that request is submitted
                    byte[] doc = cr.getExcelFile();
                    String body2 = "<html><body><p>Hello,</p><p>A request to load cards has been submitted. The document is attached.</p><p>Regards<br/>Fuel Card Platform</p></body></html>";
                    if (getSattrakPartner() != null && getSattrakPartner().getContactEmails() != null
                            && getSattrakPartner().getContactEmails().trim().length() > 0) {
                        String[] to = getSattrakPartner().getContactEmails().split(",");
                        Emailer.sendEmail("fuelcard@sattrakservices.com", to,
                                "Card Load Request - " + cr.getRequestRefNum() + " Submitted", body2, doc,
                                cr.getRequestRefNum() + ".xls", "application/vnd.ms-excel");
                    }
                } else {
                    gDAO.rollback();
                    curContext.addMessage(null,
                            new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", gDAO.getMessage()));
                }
                gDAO.destroy();
            } else {
                curContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed",
                        "Excel document is not valid!"));
            }
        } catch (Exception ex) {
            curContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Severe",
                    "Please upload an excel document! Error: " + ex.getMessage()));
        }
    } else {
        curContext.addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed", "Please upload an excel document!"));
    }
}

From source file:com.dexter.fuelcard.mbean.UserMBean.java

public void cancelCards() {
    FacesContext curContext = FacesContextImpl.getCurrentInstance();
    if (getUploadItem() != null) {
        try {/*from www  .jav a2 s . c o  m*/
            HSSFWorkbook workbook = new HSSFWorkbook(getUploadItem().getInputstream());
            int sheetCount = workbook.getNumberOfSheets();
            if (sheetCount >= 1) {
                String password = getRandomDigitPassword();

                HSSFSheet sheet = workbook.getSheetAt(0); // first sheet should be the main sheet
                sheet.protectSheet(password);

                ByteArrayOutputStream byout = new ByteArrayOutputStream();
                workbook.write(byout);
                byout.close();

                CardRequest cr = new CardRequest();
                cr.setRequestRefNum(getActiveUser().getPartner().getCode() + "-" + password);
                cr.setAdditionalComment(getRequestComment());
                cr.setCrt_dt(new Date());
                cr.setExcelFile(byout.toByteArray());
                cr.setPartner(getActiveUser().getPartner());
                cr.setRequest_dt(new Date());
                cr.setRequestedBy(getActiveUser());
                cr.setRequestType("CANCEL-CARDS");
                cr.setStatus("PENDING");

                GeneralDAO gDAO = new GeneralDAO();
                gDAO.startTransaction();
                boolean ret = gDAO.save(cr);
                if (ret) {
                    gDAO.commit();
                    setRequestComment(null);
                    setMyPendingCardCancelRequests(null);
                    curContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Success",
                            "Request submitted successfully!"));

                    // Send email to user that submitted that their request is been treated
                    String body = "<html><body><p>Dear " + getActiveUser().getFullname()
                            + ",</p><p>Your request to decommission cards has been recieved and will be treated as soon as possible. You will be notified of every progress.</p><p>Regards<br/>Fuel Card Platform</p></body></html>";
                    Emailer.sendEmail("fuelcard@sattrakservices.com",
                            new String[] { getActiveUser().getEmail() },
                            "Card Decommission Request - " + cr.getRequestRefNum() + " Received", body);
                    // Send email to sattrak that request is submitted
                    byte[] doc = cr.getExcelFile();
                    String body2 = "<html><body><p>Hello,</p><p>A request to decommission cards has been submitted. The document is attached.</p><p>Regards<br/>Fuel Card Platform</p></body></html>";
                    if (getSattrakPartner() != null && getSattrakPartner().getContactEmails() != null
                            && getSattrakPartner().getContactEmails().trim().length() > 0) {
                        String[] to = getSattrakPartner().getContactEmails().split(",");
                        Emailer.sendEmail("fuelcard@sattrakservices.com", to,
                                "Card Decommission Request - " + cr.getRequestRefNum() + " Submitted", body2,
                                doc, cr.getRequestRefNum() + ".xls", "application/vnd.ms-excel");
                    }
                } else {
                    gDAO.rollback();
                    curContext.addMessage(null,
                            new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", gDAO.getMessage()));
                }
                gDAO.destroy();
            } else {
                curContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed",
                        "Excel document is not valid!"));
            }
        } catch (Exception ex) {
            curContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Severe",
                    "Please upload an excel document! Error: " + ex.getMessage()));
        }
    } else {
        curContext.addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed", "Please upload an excel document!"));
    }
}

From source file:com.dexter.fuelcard.mbean.UserMBean.java

public void orderCards() {
    FacesContext curContext = FacesContextImpl.getCurrentInstance();
    if (getUploadItem() != null) {
        try {//from   www.j  a  v a2  s .  c o  m
            HSSFWorkbook workbook = new HSSFWorkbook(getUploadItem().getInputstream());
            int sheetCount = workbook.getNumberOfSheets();
            if (sheetCount >= 2) {
                String password = getRandomDigitPassword();

                HSSFSheet sheet = workbook.getSheetAt(0); // first sheet should be the main sheet
                sheet.protectSheet(password);

                ByteArrayOutputStream byout = new ByteArrayOutputStream();
                workbook.write(byout);
                byout.close();

                CardRequest cr = new CardRequest();
                cr.setRequestRefNum(getActiveUser().getPartner().getCode() + "-" + password);
                cr.setAdditionalComment(getRequestComment());
                cr.setCrt_dt(new Date());
                cr.setExcelFile(byout.toByteArray());
                cr.setPartner(getActiveUser().getPartner());
                cr.setRequest_dt(new Date());
                cr.setRequestedBy(getActiveUser());
                cr.setRequestType("ORDER-CARDS");
                cr.setStatus("PENDING");

                GeneralDAO gDAO = new GeneralDAO();
                gDAO.startTransaction();
                boolean ret = gDAO.save(cr);
                if (ret) {
                    gDAO.commit();
                    setRequestComment(null);
                    setMyPendingCardOrderRequests(null);
                    curContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Success",
                            "Request submitted successfully!"));

                    // Send email to user that submitted that their request is been treated
                    String body = "<html><body><p>Dear " + getActiveUser().getFullname()
                            + ",</p><p>Your request to order cards has been recieved and will be treated as soon as possible. You will be notified of every progress.</p><p>Regards<br/>Fuel Card Platform</p></body></html>";
                    Emailer.sendEmail("fuelcard@sattrakservices.com",
                            new String[] { getActiveUser().getEmail() },
                            "Card Order Request - " + cr.getRequestRefNum() + " Received", body);
                    // Send email to sattrak that request is submitted
                    byte[] doc = cr.getExcelFile();
                    String body2 = "<html><body><p>Hello,</p><p>A request to order cards has been submitted. The document is attached.</p><p>Regards<br/>Fuel Card Platform</p></body></html>";
                    if (getSattrakPartner() != null && getSattrakPartner().getContactEmails() != null
                            && getSattrakPartner().getContactEmails().trim().length() > 0) {
                        String[] to = getSattrakPartner().getContactEmails().split(",");
                        Emailer.sendEmail("fuelcard@sattrakservices.com", to,
                                "Card Order Request - " + cr.getRequestRefNum() + " Submitted", body2, doc,
                                cr.getRequestRefNum() + ".xls", "application/vnd.ms-excel");
                    }
                } else {
                    gDAO.rollback();
                    curContext.addMessage(null,
                            new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", gDAO.getMessage()));
                }
                gDAO.destroy();
            } else {
                curContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed",
                        "Excel document is not valid!"));
            }
        } catch (Exception ex) {
            curContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Severe",
                    "Please upload an excel document! Error: " + ex.getMessage()));
        }
    } else {
        curContext.addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed", "Please upload an excel document!"));
    }
}

From source file:senselogic.excelbundle.ExcelExporter.java

License:Apache License

/**
 * Create a sheet and fill it with stuff.
 */// ww w  .j a va2  s  .c  o m
private void createSheet(String sheetName, LanguagePack referencePack) {
    HSSFSheet sheet = wb.createSheet(sheetName);
    sheet.protectSheet("excelbundle");
    sheet.setDisplayGridlines(false);
    sheet.createFreezePane(1, 0, 1, 0);

    //Set some column widths:
    int numCols = langPacks.size() + 2;
    for (short i = 0; i <= numCols; i++)
        sheet.setColumnWidth(i, 10000);

    //Now lets fill it with stuff
    HSSFRow r;
    HSSFCell c;
    int rowIndex = 0;
    int colIndex;
    for (LanguageFile refLangFile : referencePack.getLanguageFiles()) {
        //Bundle path
        r = sheet.createRow(rowIndex);
        c = r.createCell(0);
        c.setCellStyle(bundlePathStyle);
        c.setCellValue(new HSSFRichTextString(refLangFile.getPath()));
        rowIndex++;

        //"Table header"
        r = sheet.createRow(rowIndex);
        colIndex = 1;
        for (LanguagePack pack : langPacks.values()) {
            c = r.createCell(colIndex);
            c.setCellStyle(languageStyle);
            c.setCellValue(new HSSFRichTextString(pack.getLanguage()));
            colIndex++;
        }
        rowIndex++;

        //Keys and values
        for (LanguageFile.KeyValuePair pair : refLangFile.getPairs()) {
            r = sheet.createRow(rowIndex);
            r.setHeightInPoints(16);
            c = r.createCell(0);
            c.setCellStyle(keyStyle);
            c.setCellValue(new HSSFRichTextString(pair.getKey()));

            colIndex = 1;
            for (LanguagePack pack : langPacks.values()) {
                c = r.createCell(colIndex);

                String value = null;
                LanguageFile langFile = pack.getLanguageFile(refLangFile.getPath());
                if (langFile != null)
                    value = langFile.getValue(pair.getKey());

                if (value == null) {
                    if (redmark)
                        c.setCellStyle(missingStyle);
                    else
                        c.setCellStyle(valueStyle);
                } else {
                    c.setCellStyle(valueStyle);
                    c.setCellValue(new HSSFRichTextString(value));
                }
                colIndex++;
            }
            rowIndex++;
        }
        rowIndex++;
    } //End of sheet creation
}