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

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

Introduction

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

Prototype

@Override
public HSSFRow createRow(int rownum) 

Source Link

Document

Create a new row within the sheet and return the high level representation

Usage

From source file:com.ve.reubicate.inmopublicity.utils.ExlFileCreator.java

public void createExcel(List<Customer> customers, String message, String countryCode) {
    try {/*from  www  .ja v  a2  s .c o m*/
        String filename = "Customers.xls";
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("Customers");
        int count = 0;
        for (Customer customer : customers) {

            for (String phoneNumber : customer.getPhoneNumbers()) {
                HSSFRow row = sheet.createRow(count);
                row.createCell(0).setCellValue(phoneNumber);
                row.createCell(1).setCellValue(phoneNumber);
                row.createCell(2).setCellValue(customer.getEmail());
                row.createCell(3).setCellValue(countryCode);
                row.createCell(4).setCellValue(message);
            }
            count++;

        }
        FileOutputStream fileOut = new FileOutputStream(filename);
        workbook.write(fileOut);
        fileOut.close();
        System.out.println("Your excel file has been generated!");

    } catch (Exception ex) {
        System.out.println(ex);

    }
}

From source file:com.vportal.portlet.vcms.action.ReportAction.java

License:Open Source License

public void setReportByType(int y, int x, HSSFSheet sheet, HSSFWorkbook wb, long groupId, String language,
        long statusId, Date fromDate, Date toDate, String byUser) throws Exception {

    HSSFRow row;//from   www. ja v  a 2  s. c o  m

    List listType = VcmsTypeServiceUtil.getTypesByS_L(groupId, language);
    sheet.shiftRows(y, sheet.getLastRowNum(), 1);

    //date
    row = sheet.createRow(y - 2);
    ReportUtil.createCellNoBorder(row, (short) (x + 1), " T\u1eeb ng\u00e0y " + ActionUtil.dateParser(fromDate)
            + " \u0111\u1ebfn ng\u00e0y " + ActionUtil.dateParser(toDate), wb);

    row = sheet.createRow(y);

    ReportUtil.createCellBold(row, (short) (x), "S\u1ed1 TT", wb);
    ReportUtil.createCellBold(row, (short) (x + 1), "Lo\u1ea1i tin", wb);
    ReportUtil.createCellBold(row, (short) (x + 2), "T\u00e1c ph\u1ea9m", wb);

    int totalArticle = 0;
    int stt = 0;
    for (int j = 0; j < listType.size(); j++) {
        VcmsType vcmsType = (VcmsType) listType.get(j);
        sheet.shiftRows(y + 1, sheet.getLastRowNum(), 1);
        row = sheet.createRow(1 + y++);

        int total = VcmsArticleServiceUtil.countByType(groupId, language, statusId,
                Long.valueOf(vcmsType.getTypeId()), fromDate, toDate, byUser);
        totalArticle += total;

        if (vcmsType.getTypeId() != null) {
            ReportUtil.createCell(row, (short) (x), ++stt, wb);
            ReportUtil.createCellAlignLeft(row, (short) (x + 1), "" + vcmsType.getName() + "", wb);
            ReportUtil.createCell(row, (short) (x + 2), total, wb);
        }

    }
    //total count
    row = sheet.createRow(y + 2);
    ReportUtil.createCellNoBorder(row, (short) (x + 1), "T\u1ed5ng s\u1ed1 b\u00e0i vi\u1ebft", wb);
    ReportUtil.createCellNoBorder(row, (short) (x + 2), String.valueOf(totalArticle), wb);

    FileOutputStream fileOut = null;
    try {
        fileOut = new FileOutputStream(
                PortalUtil.getPortalWebDir() + "/VCMS-portlet/html/portlet/vcms/report/file/reportByType.xls");
        wb.write(fileOut);

    } catch (Exception ex) {
        System.out.println(ex.toString());
    } finally {
        fileOut.flush();
        fileOut.close();
    }
}

From source file:com.vportal.portlet.vcms.action.ReportAction.java

License:Open Source License

public void setCategoryReport(List portionResults, int y, int x, HSSFSheet sheet, HSSFWorkbook wb, long groupId,
        String language, String parentId, Date dateFrom, Date dateTo, String byUser) throws Exception {
    int totalStatus = VcmsStatusLocalServiceUtil.countByGroupId(groupId);
    List statusResults = VcmsStatusLocalServiceUtil.getByGroupId(groupId, 0, totalStatus);

    HSSFRow row;//w  ww  .j a v a 2  s.co m
    HSSFCell cell;
    User userBean;

    sheet.shiftRows(y, sheet.getLastRowNum(), 1);

    row = sheet.createRow(y - 2);
    ReportUtil.createCellNoBorder(row, (short) (x + 1), " T\u1eeb ng\u00e0y " + ActionUtil.dateParser(dateFrom)
            + " \u0111\u1ebfn ng\u00e0y " + ActionUtil.dateParser(dateTo), wb);

    row = sheet.createRow(y);
    ReportUtil.createCellBold(row, (short) (x), "S\u1ed1 TT", wb);
    ReportUtil.createCellBold(row, (short) (x + 1), "Chuy\u00ean m\u1ee5c", wb);

    for (int j = 0; j < totalStatus; j++) {
        VcmsStatus vcmsStatus = (VcmsStatus) statusResults.get(j);
        ReportUtil.createCellBold(row, (short) (x + 2 + j), "" + vcmsStatus.getName() + "", wb);
    }

    int stt = 0;
    int totalArticle = 0;
    for (int i = portionResults.size() - 1; i >= 0; --i) {
        VcmsPortion vcmsPortion = (VcmsPortion) portionResults.get(i);
        List categoryByPortion = ActionUtil.getListCategoryReport(groupId, vcmsPortion.getPortionId(), parentId,
                language);

        for (int j = 0; j < categoryByPortion.size(); j++) {
            VcmsCategory vcmsCategory = (VcmsCategory) categoryByPortion.get(j);
            sheet.shiftRows(y + 1, sheet.getLastRowNum(), 1);
            row = sheet.createRow(1 + y++);

            if (vcmsCategory.getName() != null) {
                ReportUtil.createCell(row, (short) (x), ++stt, wb);
            }
            if (vcmsCategory.getName() != null) {
                ReportUtil.createCellAlignLeft(row, (short) (x + 1), "" + vcmsCategory.getName() + "", wb);
            }
            for (int k = 0; k < totalStatus; k++) {
                int countArticles = 0;
                VcmsStatus status = (VcmsStatus) statusResults.get(k);

                countArticles = VcmsArticleServiceUtil.countByC_P_L_S_D(vcmsCategory.getCategoryId(), groupId,
                        language, status.getStatusId(), dateFrom, dateTo, byUser);
                totalArticle += countArticles;

                ReportUtil.createCell(row, (short) (x + 2 + k), countArticles, wb);
            }
        }

    }

    row = sheet.createRow(y + 2);
    ReportUtil.createCellNoBorder(row, (short) (x + 1), "T\u1ed5ng s\u1ed1 b\u00e0i vi\u1ebft", wb);
    ReportUtil.createCellNoBorder(row, (short) (x + 2), String.valueOf(totalArticle), wb);

    FileOutputStream fileOut = null;
    try {
        fileOut = new FileOutputStream(PortalUtil.getPortalWebDir()
                + "/VCMS-portlet/html/portlet/vcms/report/file/reportCategory.xls");
        wb.write(fileOut);
    } catch (Exception ex) {
        System.out.println(ex.toString());
    } finally {
        fileOut.flush();
        fileOut.close();
    }
}

From source file:com.vportal.portlet.vcms.action.ReportAction.java

License:Open Source License

public void setReportByDate(int y, int x, HSSFSheet sheet, HSSFWorkbook wb, long groupId, String language,
        long statusId, Date dateFrom, Date dateTo, String byUser, String[] strType, int begin, int end)
        throws Exception {

    List listNews = new ArrayList();

    listNews = VcmsArticleServiceUtil.listArticleByDateTypes(groupId, language, statusId, dateFrom, dateTo,
            byUser, strType, begin, end);

    HSSFRow row;/*from ww w.  ja v a2 s.c o m*/
    HSSFCell cell;
    User userBean;

    VcmsStatus vcmsStatus = VcmsStatusLocalServiceUtil.getVcmsStatus(statusId);

    sheet.shiftRows(y, sheet.getLastRowNum(), 1);
    row = sheet.createRow(y - 2);
    ReportUtil.createCellNoBorder(row, (short) (x + 1), " T\u1eeb ng\u00e0y " + ActionUtil.dateParser(dateFrom)
            + " \u0111\u1ebfn ng\u00e0y " + ActionUtil.dateParser(dateTo), wb);

    row = sheet.createRow(y);

    ReportUtil.createCellBold(row, (short) (x), "S\u1ed1 TT", wb);
    ReportUtil.createCellBold(row, (short) (x + 1), "Ti\u00eau \u0111\u1ec1", wb);
    ReportUtil.createCellBold(row, (short) (x + 2), "Ng\u00e0y \u0111\u0103ng", wb);
    ReportUtil.createCellBold(row, (short) (x + 3), "Ng\u01b0\u1eddi t\u1ea1o", wb);
    ReportUtil.createCellBold(row, (short) (x + 4), "Ng\u01b0\u1eddi duy\u1ec7t", wb);
    ReportUtil.createCellBold(row, (short) (x + 5), "Ng\u01b0\u1eddi xu\u1ea5t b\u1ea3n", wb);
    ReportUtil.createCellBold(row, (short) (x + 6), "Lo\u1ea1i tin", wb);

    int stt = 0;
    for (int j = 0; j < listNews.size(); j++) {
        VcmsArticle vcmsAr = (VcmsArticle) listNews.get(j);

        String typeNames = "";
        try {
            typeNames += ReportUtil.getTypeNames(vcmsAr.getArticleId());
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        sheet.shiftRows(y + 1, sheet.getLastRowNum(), 1);
        row = sheet.createRow(1 + y++);

        if (vcmsAr != null) {
            ReportUtil.createCell(row, (short) (x), ++stt, wb);
            ReportUtil.createCellAlignLeft(row, (short) (x + 1), "" + vcmsAr.getTitle() + "", wb);
            ReportUtil.createCell(row, (short) (x + 2), ActionUtil.dateParser(vcmsAr.getPublishedDate()), wb);

            try {
                User userCreate = UserServiceUtil.getUserById(Long.parseLong(vcmsAr.getCreatedByUser()));
                ReportUtil.createCellAlignLeft(row, (short) (x + 3), userCreate.getFullName(), wb);
            } catch (Exception ex) {
                ReportUtil.createCellAlignLeft(row, (short) (x + 3), "User \u0111\u00e3 b\u1ecb x\u00f3a", wb);
            }

            try {
                User modifieddUser = UserServiceUtil.getUserById(Long.parseLong(vcmsAr.getModifiedByUser()));
                ReportUtil.createCellAlignLeft(row, (short) (x + 4), modifieddUser.getFullName(), wb);
            } catch (Exception ex) {
                ReportUtil.createCellAlignLeft(row, (short) (x + 4), "User \u0111\u00e3 b\u1ecb x\u00f3a", wb);
            }

            try {
                User publishedUser = UserServiceUtil.getUserById(Long.parseLong(vcmsAr.getPublishedByUser()));
                ReportUtil.createCellAlignLeft(row, (short) (x + 5), publishedUser.getFullName(), wb);
            } catch (Exception ex) {
                ReportUtil.createCellAlignLeft(row, (short) (x + 5), "User \u0111\u00e3 b\u1ecb x\u00f3a", wb);
            }

            ReportUtil.createCellAlignLeft(row, (short) (x + 6), typeNames, wb);
        }

    }

    row = sheet.createRow(y + 2);
    ReportUtil.createCellNoBorder(row, (short) (x + 1), "T\u1ed5ng s\u1ed1 b\u00e0i vi\u1ebft", wb);
    ReportUtil.createCellNoBorder(row, (short) (x + 2), String.valueOf(listNews.size()), wb);

    FileOutputStream fileOut = null;
    try {
        fileOut = new FileOutputStream(
                PortalUtil.getPortalWebDir() + "/VCMS-portlet/html/portlet/vcms/report/file/reportByDate.xls");
        wb.write(fileOut);
    } catch (Exception ex) {
        System.out.println(ex.toString());
    } finally {
        fileOut.flush();
        fileOut.close();
    }
}

From source file:com.vportal.portlet.vcms.action.ReportAction.java

License:Open Source License

public void setUserReport(List userList, int y, int x, HSSFSheet sheet, HSSFWorkbook wb, long groupId,
        String language, String parentId, long statusId, Date dateFrom, Date dateTo) throws Exception {

    List listType = VcmsTypeServiceUtil.getTypesByS_L(groupId, language);

    HSSFRow row;/*  w  w w . ja v a2  s.  c  o  m*/
    User userBean;

    sheet.shiftRows(y, sheet.getLastRowNum(), 1);

    row = sheet.createRow(y - 2);
    ReportUtil.createCellNoBorder(row, (short) (x + 1), " T\u1eeb ng\u00e0y " + ActionUtil.dateParser(dateFrom)
            + " \u0111\u1ebfn ng\u00e0y " + ActionUtil.dateParser(dateTo), wb);

    row = sheet.createRow(y);
    ReportUtil.createCellBold(row, (short) (x), "S\u1ed1 TT", wb);
    ReportUtil.createCellBold(row, (short) (x + 1), "H\u1ecd T\u00ean", wb);

    for (int j = 0; j < listType.size(); j++) {
        VcmsType vcmsType = (VcmsType) listType.get(j);
        ReportUtil.createCellBold(row, (short) (x + 2 + j), "" + vcmsType.getName() + "", wb);
    }
    ReportUtil.createCellBold(row, (short) (x + 2 + listType.size()), "C\u00e1c lo\u1ea1i tin kh\u00e1c", wb);

    int stt = 0;
    int totalArticle = 0;

    for (int i = userList.size() - 1; i >= 0; --i) {
        userBean = (User) userList.get(i);
        sheet.shiftRows(y + 1, sheet.getLastRowNum(), 1);
        row = sheet.createRow(1 + y++);

        if (userBean != null) {
            ReportUtil.createCell(row, (short) (x), ++stt, wb);
        }
        if (userBean != null) {
            ReportUtil.createCellAlignLeft(row, (short) (x + 1), "" + userBean.getFullName() + "", wb);
        }

        for (int j = 0; j < listType.size(); j++) {
            VcmsType vcmsType = (VcmsType) listType.get(j);
            int countArticle = 0;
            if (!parentId.equals("0")) {
                countArticle = VcmsArticleServiceUtil.countByUser(groupId, language, parentId, statusId,
                        Long.valueOf(vcmsType.getTypeId()), dateFrom, dateTo,
                        String.valueOf(userBean.getUserId()));
            } else {

                countArticle = VcmsArticleServiceUtil.countByType(groupId, language, statusId,
                        Long.valueOf(vcmsType.getTypeId()), dateFrom, dateTo,
                        String.valueOf(userBean.getUserId()));
            }

            ReportUtil.createCell(row, (short) (x + 2 + j), countArticle, wb);
            totalArticle += countArticle;
        }

        //other type
        int countArticleAllCatNotInType = 0;
        if (!parentId.equals("0")) {
            countArticleAllCatNotInType = VcmsArticleServiceUtil.countArticleNotInType(groupId, language,
                    parentId, statusId, dateFrom, dateTo, String.valueOf(userBean.getUserId()));
        } else {
            countArticleAllCatNotInType = VcmsArticleServiceUtil.countArticleAllCatNotInType(groupId, language,
                    statusId, dateFrom, dateTo, String.valueOf(userBean.getUserId()));
        }

        ReportUtil.createCell(row, (short) (x + 2 + listType.size()), countArticleAllCatNotInType, wb);
        totalArticle += countArticleAllCatNotInType;
    }

    row = sheet.createRow(y + 2);
    ReportUtil.createCellNoBorder(row, (short) (x + 1), "T\u1ed5ng s\u1ed1 b\u00e0i vi\u1ebft", wb);
    ReportUtil.createCellNoBorder(row, (short) (x + 2), String.valueOf(totalArticle), wb);

    FileOutputStream fileOut = null;
    try {
        fileOut = new FileOutputStream(
                PortalUtil.getPortalWebDir() + "/VCMS-portlet/html/portlet/vcms/report/file/reportUser.xls");
        wb.write(fileOut);
    } catch (Exception ex) {
        System.out.println(ex.toString());
    } finally {
        fileOut.flush();
        fileOut.close();
    }

}

From source file:com.vportal.portlet.vdoc.action.VDocManage.java

License:Open Source License

public void setReportByDate(int y, int x, HSSFSheet sheet, HSSFWorkbook wb, long groupId, String language,
        int statusId, Date dateFrom, Date dateTo) throws Exception {
    List listnew = new ArrayList();
    listnew = vdocDocumentServiceUtil.getDocbyModifyDate(groupId, language, dateFrom, dateTo, statusId);
    HSSFRow row;// w w  w  .j a v a2s.co m
    HSSFCell cell;
    sheet.shiftRows(sheet.getLastRowNum(), y, 1);
    row = sheet.createRow(y - 2);
    ReportUtil.createCellNoBorder(row, (short) (x + 1), " T\u1eeb ng\u00e0y " + ActionUtil.dateParser(dateFrom)
            + " \u0111\u1ebfn ng\u00e0y " + ActionUtil.dateParser(dateTo), wb);

    row = sheet.createRow(y);

    ReportUtil.createCellBold(row, (short) (x), "S\u1ed1 TT", wb);
    ReportUtil.createCellBold(row, (short) (x + 1), "Ti\u00eau \u0111\u1ec1", wb);
    ReportUtil.createCellBold(row, (short) (x + 2), "Nga\u0300y xu\u00E2\u0301t ba\u0309n", wb);
    ReportUtil.createCellBold(row, (short) (x + 3), "Nga\u0300y chi\u0309nh s\u01B0\u0309a", wb);
    ReportUtil.createCellBold(row, (short) (x + 4), "Ng\u01B0\u01A1\u0300i duy\u00EA\u0323t", wb);
    ReportUtil.createCellBold(row, (short) (x + 5), "Ng\u01B0\u01A1\u0300i chi\u0309nh s\u01B0\u0309a", wb);
    ReportUtil.createCellBold(row, (short) (x + 6), "\u0110\u01A1n vi\u0323", wb);
    int stt = 0;
    for (int j = 0; j < listnew.size(); j++) {
        // VcmsArticle vcmsAr = (VcmsArticle) listNews.get(j);
        vdocDocument vdoc = (vdocDocument) listnew.get(j);

        sheet.shiftRows(y + 1, sheet.getLastRowNum(), 1);
        row = sheet.createRow(1 + y++);

        if (vdoc != null) {
            ReportUtil.createCell(row, (short) (x), ++stt, wb);
            ReportUtil.createCellAlignLeft(row, (short) (x + 1), "" + vdoc.getTitle() + "", wb);
            ReportUtil.createCell(row, (short) (x + 2), ActionUtil.dateParser(vdoc.getPublishedDate()), wb);

            try {
                // User userCreate =
                // UserServiceUtil.getUserById(vdoc.getCreatedByUser());
                ReportUtil.createCellAlignLeft(row, (short) (x + 3),
                        ActionUtil.dateParser(vdoc.getModifiedDate()), wb);
            } catch (Exception ex) {
                ReportUtil.createCellAlignLeft(row, (short) (x + 3), "Ng&#224;y ch&#7881;nh s&#7917;a", wb);
            }

            try {
                User modifieddUser = UserServiceUtil.getUserById(vdoc.getPublishedByUser());
                ReportUtil.createCellAlignLeft(row, (short) (x + 4), modifieddUser.getFullName(), wb);
            } catch (Exception ex) {
                ReportUtil.createCellAlignLeft(row, (short) (x + 4), "Ng\u01B0\u01A1\u0300i duy\u00EA\u0323t",
                        wb);
            }

            try {
                User publishedUser = UserServiceUtil.getUserById(vdoc.getModifiedByUser());
                ReportUtil.createCellAlignLeft(row, (short) (x + 5), publishedUser.getFullName(), wb);
            } catch (Exception ex) {
                ReportUtil.createCellAlignLeft(row, (short) (x + 5),
                        "Ng\u01B0\u01A1\u0300i chi\u0309nh s\u01B0\u0309a", wb);
            }
            try {

                ReportUtil.createCellAlignLeft(row, (short) (x + 6), vdoc.getOrgRels(), wb);
            } catch (Exception ex) {
                ReportUtil.createCellAlignLeft(row, (short) (x + 6), "\u0110\u01A1n vi\u0323", wb);
            }

        }

    }

    row = sheet.createRow(y + 2);
    ReportUtil.createCellNoBorder(row, (short) (x + 1), "T\u1ed5ng s\u1ed1 b\u00e0i vi\u1ebft", wb);
    ReportUtil.createCellNoBorder(row, (short) (x + 2), String.valueOf(listnew.size()), wb);

    FileOutputStream fileOut = null;
    String porttalDir = PortalUtil.getPortalWebDir();
    try {
        fileOut = new FileOutputStream(porttalDir.substring(0, porttalDir.indexOf("ROOT"))
                + "//VDoc-portlet//html//vdoc_manage//statistics//file//reportByDate.xls");
        wb.write(fileOut);
    } catch (Exception ex) {
        System.out.println(ex.toString());
    } finally {
        fileOut.flush();
        fileOut.close();
    }
}

From source file:com.vportal.portlet.vdoc.action.VDocManage.java

License:Open Source License

public void setReportByOrg(int y, int x, HSSFSheet sheet, HSSFWorkbook wb, long groupId, String language,
        int statusId) throws Exception {
    List listnew = new ArrayList();
    listnew = vdocOrgServiceUtil.getOrgByG_L(groupId, language);
    HSSFRow row;/*from  w w  w  .  j a v  a  2 s . c  o  m*/
    HSSFCell cell;
    sheet.shiftRows(sheet.getLastRowNum(), y, 1);
    row = sheet.createRow(y - 2);
    row = sheet.createRow(y);

    ReportUtil.createCellBold(row, (short) (x), "S\u1ed1 TT", wb);
    ReportUtil.createCellBold(row, (short) (x + 1), "C\u01A1 quan - \u0110\u01A1n vi\u0323", wb);
    ReportUtil.createCellBold(row, (short) (x + 2), "Ch\u01A1\u0300 xu\u00E2\u0301t ba\u0309n", wb);
    ReportUtil.createCellBold(row, (short) (x + 3), "\u0110a\u0303 xu\u00E2\u0301t ba\u0309n", wb);
    ReportUtil.createCellBold(row, (short) (x + 4), "Chi\u0309nh s\u01B0\u0309a", wb);
    int stt = 0;
    for (int j = 0; j < listnew.size(); j++) {
        // VcmsArticle vcmsAr = (VcmsArticle) listNews.get(j);
        vdocOrg org = (vdocOrg) listnew.get(j);

        sheet.shiftRows(y + 1, sheet.getLastRowNum(), 1);
        row = sheet.createRow(1 + y++);

        if (org != null) {
            ReportUtil.createCell(row, (short) (x), ++stt, wb);
            ReportUtil.createCellAlignLeft(row, (short) (x + 1), "" + org.getName() + "", wb);
            ReportUtil.createCell(row, (short) (x + 2),
                    vdocDORelServiceUtil.countCategory_approving(org.getOrgId()), wb);

            try {
                // User userCreate =
                // UserServiceUtil.getUserById(vdoc.getCreatedByUser());
                ReportUtil.createCellAlignLeft(row, (short) (x + 3),
                        String.valueOf(vdocDORelServiceUtil.countCategory_approving(org.getOrgId())), wb);
            } catch (Exception ex) {
                ReportUtil.createCellAlignLeft(row, (short) (x + 3), "...", wb);
            }

            try {

                ReportUtil.createCellAlignLeft(row, (short) (x + 4),
                        String.valueOf(vdocDORelServiceUtil.countCategory_approving(org.getOrgId())), wb);
            } catch (Exception ex) {
                ReportUtil.createCellAlignLeft(row, (short) (x + 4), "...", wb);
            }

        }

    }

    FileOutputStream fileOut = null;
    String porttalDir = PortalUtil.getPortalWebDir();
    try {
        fileOut = new FileOutputStream(porttalDir.substring(0, porttalDir.indexOf("ROOT"))
                + "//VDoc-portlet//html//vdoc_manage//statistics//file//reportByOrg.xls");
        wb.write(fileOut);
    } catch (Exception ex) {
        System.out.println(ex.toString());
    } finally {
        fileOut.flush();
        fileOut.close();
    }
}

From source file:com.work.fileHandle.fileHandleServlet.java

public HSSFWorkbook createExcel() {

    HSSFWorkbook webbook = new HSSFWorkbook();

    HSSFSheet sheet = webbook.createSheet("?");

    HSSFRow row = sheet.createRow((int) 0);

    HSSFCellStyle style = webbook.createCellStyle();

    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

    HSSFCell cell = row.createCell((short) 0);
    cell.setCellValue("?");
    cell.setCellStyle(style);/*from w w w. j  a  v  a 2  s. co m*/
    cell = row.createCell((short) 1);
    cell.setCellValue("??");
    cell.setCellStyle(style);
    cell = row.createCell((short) 2);
    cell.setCellValue("??");
    cell.setCellStyle(style);
    cell = row.createCell((short) 3);
    cell.setCellValue("??");
    cell.setCellStyle(style);
    cell = row.createCell((short) 4);
    cell.setCellValue("?");
    cell.setCellStyle(style);

    List students = query();

    for (int i = 0; i < students.size(); i++) {
        row = sheet.createRow((int) i + 1);
        Student student = (Student) students.get(i);

        row.createCell((short) 0).setCellValue(student.getStudentId().toString());
        row.createCell((short) 1).setCellValue(student.getStudentCode());
        row.createCell((short) 2).setCellValue(student.getEnName());
        row.createCell((short) 3).setCellValue(student.getCnName());
        row.createCell((short) 4).setCellValue(student.getGrade());
    }

    try {
        String savePath = this.getServletContext().getRealPath("/WEB-INF/uploads");
        String filename = "students-grade.xls";
        File file = new File(savePath);

        if (!file.exists() && !file.isDirectory()) {
            System.out.println(savePath + "??");
            file.mkdir();
        }

        FileOutputStream fout = new FileOutputStream(savePath + "/" + filename);
        webbook.write(fout);
        fout.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return webbook;
}

From source file:com.xnradmin.core.test.TestJson.java

private static void processYsmcExcel(String res) throws JSONException, org.json.JSONException {
    JSONObject jo = new JSONObject(res);

    Iterator it = jo.keys();//from  ww w. j  a  va2  s  .co  m

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("ysmc");
    HSSFRow row = sheet.createRow((int) 0);
    HSSFCellStyle style = wb.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // ?
    HSSFCell cell = row.createCell(0);
    cell.setCellValue("??");
    cell.setCellStyle(style);
    cell = row.createCell(1);

    cell.setCellValue("");
    cell.setCellStyle(style);
    cell = row.createCell(2);

    cell.setCellValue("??");
    cell.setCellStyle(style);
    cell = row.createCell(3);

    cell.setCellValue("??");
    cell.setCellStyle(style);
    cell = row.createCell(4);

    cell.setCellValue("?");
    cell.setCellStyle(style);
    cell = row.createCell(5);

    cell.setCellValue("?2");
    cell.setCellStyle(style);
    cell = row.createCell(6);

    cell.setCellValue("");
    cell.setCellStyle(style);
    cell = row.createCell(7);

    cell.setCellValue("?");
    cell.setCellStyle(style);
    cell = row.createCell(8);

    cell.setCellValue("");
    cell.setCellStyle(style);
    cell = row.createCell(9);

    cell.setCellValue("");
    cell.setCellStyle(style);
    cell = row.createCell(10);

    cell.setCellValue("?");
    cell.setCellStyle(style);
    cell = row.createCell(11);

    int flag = 0;

    while (it.hasNext()) {
        String k = it.next().toString();
        // System.out.println(jo.get(k));
        flag = processYsmcSubLevel1(k, jo.get(k).toString(), wb, sheet, row, flag);
    }

    FileOutputStream fout;
    try {
        fout = new FileOutputStream("/Users/liubin/temp/ysmc" + StringHelper.getSystime("yyyyMMdd") + ".xls");
        wb.write(fout);
        fout.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.xnradmin.core.test.TestJson.java

private static int processYsmcSubLevel2(String type1, String type2, String content, HSSFWorkbook wb,
        HSSFSheet sheet, HSSFRow row, int flag) throws JSONException, org.json.JSONException {
    JSONArray data = new JSONArray(content);

    HSSFCellStyle style = wb.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // ?

    for (int i = 0; i < data.length(); i++) {

        JSONObject temp = (JSONObject) data.get(i);

        System.out.println(temp);

        StringBuffer sb = new StringBuffer();
        sb.append("??").append(temp.get("name"));
        sb.append(" | ").append(temp.get("level"));
        sb.append(" | ??").append(temp.get("format"));
        sb.append(" | ??").append(temp.get("unit"));
        sb.append(" | ?:").append(temp.get("own_brand"));
        sb.append(" | ?2: ").append(temp.get("sell_brand"));
        sb.append(" | ").append(temp.get("class1"));
        sb.append(" | ").append(type1);
        sb.append(" | ?").append(type2);
        sb.append(" | ?").append(temp.get("price"));
        sb.append(" | ").append(temp.get("commodity_total_price"));
        System.out.println(sb.toString());

        int f = flag + 1;
        row = sheet.createRow(f);
        System.out.println("row: " + f);

        HSSFCell cell0 = row.createCell(0);
        cell0.setCellStyle(style);/*from ww w .  j  av a2s  . com*/
        cell0.setCellValue(temp.get("name").toString());

        HSSFCell cell1 = row.createCell(1);
        cell1.setCellStyle(style);
        cell1.setCellValue(temp.get("level").toString());

        HSSFCell cell2 = row.createCell(2);
        cell2.setCellStyle(style);
        cell2.setCellValue(temp.get("format").toString());

        HSSFCell cell3 = row.createCell(3);
        cell3.setCellStyle(style);
        cell3.setCellValue(temp.get("unit").toString());

        HSSFCell cell4 = row.createCell(4);
        cell4.setCellStyle(style);
        cell4.setCellValue(temp.get("own_brand").toString());

        HSSFCell cell5 = row.createCell(5);
        cell5.setCellStyle(style);
        cell5.setCellValue(temp.get("sell_brand").toString());

        HSSFCell cell6 = row.createCell(6);
        cell6.setCellStyle(style);
        cell6.setCellValue(temp.get("class1").toString());

        HSSFCell cell7 = row.createCell(7);
        cell7.setCellStyle(style);
        cell7.setCellValue(temp.get("price").toString());

        HSSFCell cell8 = row.createCell(8);
        cell8.setCellStyle(style);
        cell8.setCellValue(temp.get("commodity_total_price").toString());

        HSSFCell cell9 = row.createCell(9);
        cell9.setCellStyle(style);
        cell9.setCellValue(type1);

        HSSFCell cell10 = row.createCell(10);
        cell10.setCellStyle(style);
        cell10.setCellValue(type2);

        // row.createCell(0).setCellValue(temp.get("name").toString());
        // row.createCell(1).setCellValue(temp.get("format").toString());
        // row.createCell(2).setCellValue(temp.get("unit").toString());
        // row.createCell(3).setCellValue(temp.get("own_brand").toString());
        // row.createCell(4).setCellValue(temp.get("sell_brand").toString());
        // row.createCell(5).setCellValue(temp.get("class1").toString());
        // row.createCell(6).setCellValue(temp.get("price").toString());

        flag = f;
    }
    return flag;
}