Example usage for org.apache.poi.hssf.usermodel HSSFCellStyle setAlignment

List of usage examples for org.apache.poi.hssf.usermodel HSSFCellStyle setAlignment

Introduction

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

Prototype

@Override
public void setAlignment(HorizontalAlignment align) 

Source Link

Document

set the type of horizontal alignment for the cell

Usage

From source file:com.sx.finance.OldYahooFinanceJFrame.java

public void writeExcelFile(ArrayList<List<Object>> listDataRaw) {
    // webbookExcel
    HSSFWorkbook wb = new HSSFWorkbook();
    // webbooksheet,Excelsheet
    HSSFSheet sheet = wb.createSheet("Data");
    // sheet0,poiExcelshort
    HSSFRow row = sheet.createRow((int) 0);
    //  //from  w  ww  .  j ava 2 s  .c  o  m
    HSSFCellStyle style = wb.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 

    for (int i = 0; i < title.length; i++) {
        HSSFCell cell = row.createCell(i);
        cell.setCellValue(title[i]);
        cell.setCellStyle(style);
    }

    for (int i = 0; i < listDataRaw.size(); i++) {
        row = sheet.createRow(i + 1);
        for (int j = 0; j < listDataRaw.get(i).size(); j++) {
            Object cellData = listDataRaw.get(i).get(j);
            if (cellData instanceof Long) {
                row.createCell(j).setCellValue(((Long) cellData).longValue());
            } else if (cellData instanceof Double) {
                row.createCell(j).setCellValue(((Double) cellData).doubleValue());
            } else if (cellData instanceof Date) {
                row.createCell(j).setCellValue((Date) cellData);
            } else if (cellData instanceof Integer) {
                row.createCell(j).setCellValue(((Integer) cellData).intValue());
            } else {
                row.createCell(j).setCellValue((String) cellData);
            }
        }
    }
    try {
        FileOutputStream fout = new FileOutputStream("D:/work/myprojects/yahooFinance/output/data.xls");
        wb.write(fout);
        fout.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.topsec.tsm.sim.asset.web.AssetListController.java

/**
 * //  ww w .  j  av a  2  s .co m
 * @param request
 * @param response
 */
@RequestMapping("exportAssetExcel")
public void exportAssetExcel(SID sid, HttpServletRequest request, HttpServletResponse response) {
    //?
    List<AssetObject> assetList;
    SID.setCurrentUser(sid);
    try {
        assetList = AssetFacade.getInstance().getAll();
        Collections.sort(assetList, IpComparator.getInstance());
    } finally {
        SID.removeCurrentUser();
    }
    String[] column = { "??(*)", "IP(*)", "??(*)",
            "(*)", "?(*)", "?(*)",
            "??", "?", "", null, null, null, null, "?", "??" };

    // Excel
    HSSFWorkbook workbook = new HSSFWorkbook();

    // Excel??Asset??
    HSSFSheet sheet = workbook.createSheet("?");
    //?
    HSSFCellStyle cellStyle = workbook.createCellStyle();
    cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);

    //
    HSSFRow row = sheet.createRow(0);
    for (int j = 0; j < column.length; j++) {
        HSSFCell cell = row.createCell(j);
        if (column[j] == null) {
            sheet.setColumnHidden(j, true);
            continue;
        }
        sheet.setColumnWidth(j, 22 * 256);
        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
        cell.setCellValue(column[j]);
    }
    NodeMgrFacade nodeMgrFacade = (NodeMgrFacade) SpringContextServlet.springCtx.getBean("nodeMgrFacade");
    //??
    for (int i = 0; i < assetList.size(); i++) {
        AssetObject ao = assetList.get(i);
        row = sheet.createRow(i + 1);
        for (int j = 0; j < column.length; j++) {
            HSSFCell cell = row.createCell(j);
            switch (j) {
            case 0:
                cell.setCellValue(i + 1);
                cell.setCellStyle(cellStyle);
                continue;
            case 1:
                cell.setCellValue(ao.getIp());
                continue;
            case 2:
                cell.setCellValue(ao.getName());
                continue;
            case 3:
                String deviceType = ao.getDeviceType().split("/")[0];
                cell.setCellValue(
                        DeviceTypeShortKeyUtil.getInstance().getShortZhCN(deviceType) + "_" + deviceType);
                continue;
            case 4:
                String vendor = ao.getDeviceType().split("/")[1];
                cell.setCellValue(DeviceTypeShortKeyUtil.getInstance().getShortZhCN(vendor) + "_" + vendor);
                continue;
            case 5:
                Node node = nodeMgrFacade.getNodeByNodeId(ao.getScanNodeId());
                cell.setCellValue(node != null ? node.getIp() : "");
                continue;
            case 6:
                cell.setCellValue(ao.getHostName());
                continue;
            case 7:
                cell.setCellValue(ao.getOs() != null ? ao.getOs().getOsName() : "");
                continue;
            case 8:
                cell.setCellValue(ao.getSafeRank());
                continue;
            case 9:
                cell.setCellValue(ao.getAssGroup().getGroupName());
                continue;
            case 10:
            case 11:
            case 12:
                continue;
            case 13:
                cell.setCellValue(StringUtil.nvl(ao.getLinkman()));
                continue;
            case 14:
                cell.setCellValue("");
                continue;
            default:
                cell.setCellValue("");
                continue;
            }
        }
    }

    //?excel?
    //response.setContentType("application/vnd.ms-excel");
    String userAgent = request.getHeader("User-Agent");
    String fileName = "?.xls";
    if (userAgent.indexOf("Firefox") > 0) {
        response.setHeader("Content-Disposition",
                "attachment; filename*=\"utf8' '" + StringUtil.encode(fileName, "UTF-8") + "\"");
    } else {
        response.addHeader("Content-Disposition",
                "attachment; filename=\"" + StringUtil.encode(fileName, "UTF-8") + "\"");
    }
    try {
        workbook.write(response.getOutputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.util.poi.ExcelView.java

License:Open Source License

/**
 * ?Excel/*from w w w  .ja  v  a2 s  .  co  m*/
 * 
 * @param model
 *            ?
 * @param workbook
 *            workbook
 * @param request
 *            request
 * @param response
 *            response
 */
public void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    Assert.notEmpty(properties);
    HSSFSheet sheet;
    if (StringUtils.isNotEmpty(sheetName)) {
        sheet = workbook.createSheet(sheetName);
    } else {
        sheet = workbook.createSheet();
    }
    int rowNumber = 0;
    if (titles != null && titles.length > 0) {
        HSSFRow header = sheet.createRow(rowNumber);
        header.setHeight((short) 400);
        for (int i = 0; i < properties.length; i++) {
            HSSFCell cell = header.createCell(i);
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);
            cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
            HSSFFont font = workbook.createFont();
            font.setFontHeightInPoints((short) 11);
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            cellStyle.setFont(font);
            cell.setCellStyle(cellStyle);
            if (i == 0) {
                HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
                HSSFComment comment = patriarch
                        .createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1, (short) 4, 4));
                //comment.setString(new HSSFRichTextString("P" + "o" + "w" + "e" + "r" + "e" + "d" + " " + "B" + "y" + " " + "S" + "H" + "O" + "P" + "+" + "+"));
                cell.setCellComment(comment);
            }
            if (titles.length > i && titles[i] != null) {
                cell.setCellValue(titles[i]);
            } else {
                cell.setCellValue(properties[i]);
            }
            if (widths != null && widths.length > i && widths[i] != null) {
                sheet.setColumnWidth(i, widths[i]);
            } else {
                sheet.autoSizeColumn(i);
            }
        }
        rowNumber++;
    }
    if (data != null) {
        for (Object item : data) {
            HSSFRow row = sheet.createRow(rowNumber);
            for (int i = 0; i < properties.length; i++) {
                HSSFCell cell = row.createCell(i);
                if (converters != null && converters.length > i && converters[i] != null) {
                    Class<?> clazz = PropertyUtils.getPropertyType(item, properties[i]);
                    ConvertUtils.register(converters[i], clazz);
                    /*   Map<String, Object> map=toHashMap(item);
                       cell.setCellValue(map.get(properties[i]).toString());*/
                    cell.setCellValue(BeanUtils.getProperty(item, properties[i]));
                    ConvertUtils.deregister(clazz);
                    if (clazz.equals(Date.class)) {
                        DateConverter dateConverter = new DateConverter();
                        dateConverter.setPattern(DEFAULT_DATE_PATTERN);
                        ConvertUtils.register(dateConverter, Date.class);
                    }
                } else {
                    /*Map<String, Object> map=toHashMap(item);
                    cell.setCellValue(map.get(properties[i]).toString());*/
                    cell.setCellValue(BeanUtils.getProperty(item, properties[i]));
                }
                if (rowNumber == 0 || rowNumber == 1) {
                    if (widths != null && widths.length > i && widths[i] != null) {
                        sheet.setColumnWidth(i, widths[i]);
                    } else {
                        sheet.autoSizeColumn(i);
                    }
                }
            }
            rowNumber++;
        }
    }
    if (contents != null && contents.length > 0) {
        rowNumber++;
        for (String content : contents) {
            HSSFRow row = sheet.createRow(rowNumber);
            HSSFCell cell = row.createCell(0);
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            HSSFFont font = workbook.createFont();
            font.setColor(HSSFColor.GREY_50_PERCENT.index);
            cellStyle.setFont(font);
            cell.setCellStyle(cellStyle);
            cell.setCellValue(content);
            rowNumber++;
        }
    }
    response.setContentType("application/force-download");
    if (StringUtils.isNotEmpty(filename)) {
        response.setHeader("Content-disposition",
                "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
    } else {
        response.setHeader("Content-disposition", "attachment");
    }
}

From source file:com.vportal.portlet.vdoc.service.util.ReportUtil.java

License:Open Source License

public static void createCellNoBorder(HSSFRow row, short column, String value, HSSFWorkbook wb) {

    HSSFCellStyle style = wb.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_LEFT);

    HSSFCell cell = row.getCell(column);
    if (cell == null)
        cell = row.createCell(column);// w ww  .ja v  a2s.c  om
    //cell.setEncoding(wb.ENCODING_UTF_16);
    cell.setCellValue(value);
    cell.setCellStyle(style);
}

From source file:com.vportal.portlet.vdoc.service.util.ReportUtil.java

License:Open Source License

public static void createCell(HSSFRow row, short column, String value, HSSFWorkbook wb) {

    HSSFCellStyle style = wb.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    style.setBorderBottom(HSSFCellStyle.SOLID_FOREGROUND);
    style.setBorderLeft(HSSFCellStyle.SOLID_FOREGROUND);
    style.setBorderRight(HSSFCellStyle.SOLID_FOREGROUND);
    style.setBorderTop(HSSFCellStyle.SOLID_FOREGROUND);

    HSSFCell cell = row.getCell(column);
    if (cell == null)
        cell = row.createCell(column);/*from   w  w w.ja v a2 s  .  c  o  m*/
    //cell.setEncoding(wb.ENCODING_UTF_16);
    cell.setCellValue(value);
    cell.setCellStyle(style);
}

From source file:com.vportal.portlet.vdoc.service.util.ReportUtil.java

License:Open Source License

public static void createCellAlignLeft(HSSFRow row, short column, String value, HSSFWorkbook wb) {

    HSSFCellStyle style = wb.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
    style.setBorderBottom(HSSFCellStyle.SOLID_FOREGROUND);
    style.setBorderLeft(HSSFCellStyle.SOLID_FOREGROUND);
    style.setBorderRight(HSSFCellStyle.SOLID_FOREGROUND);
    style.setBorderTop(HSSFCellStyle.SOLID_FOREGROUND);

    HSSFCell cell = row.getCell(column);
    if (cell == null)
        cell = row.createCell(column);/*from   w w  w. j  av a  2s.com*/
    //cell.setEncoding(wb.ENCODING_UTF_16);
    cell.setCellValue(value);
    cell.setCellStyle(style);
}

From source file:com.vportal.portlet.vdoc.service.util.ReportUtil.java

License:Open Source License

public static void createCellBold(HSSFRow row, short column, String value, HSSFWorkbook wb) {

    HSSFCellStyle style = wb.createCellStyle();
    HSSFFont font = wb.createFont();//from   ww w  . ja  va2 s.  co m
    font.setColor((short) 0xc);
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    style.setFont(font);
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    style.setBorderBottom(HSSFCellStyle.SOLID_FOREGROUND);
    style.setBorderLeft(HSSFCellStyle.SOLID_FOREGROUND);
    style.setBorderRight(HSSFCellStyle.SOLID_FOREGROUND);
    style.setBorderTop(HSSFCellStyle.SOLID_FOREGROUND);

    HSSFCell cell = row.getCell(column);
    if (cell == null)
        cell = row.createCell(column);
    //cell.setEncoding(wb.ENCODING_UTF_16);
    cell.setCellValue(value);
    cell.setCellStyle(style);
}

From source file:com.vportal.portlet.vdoc.service.util.ReportUtil.java

License:Open Source License

public static void createCell(HSSFRow row, short column, int value, HSSFWorkbook wb) {
    HSSFCellStyle style = wb.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    style.setBorderBottom(HSSFCellStyle.SOLID_FOREGROUND);
    style.setBorderLeft(HSSFCellStyle.SOLID_FOREGROUND);
    style.setBorderRight(HSSFCellStyle.SOLID_FOREGROUND);
    style.setBorderTop(HSSFCellStyle.SOLID_FOREGROUND);

    HSSFCell cell = row.getCell(column);
    if (cell == null)
        cell = row.createCell(column);//from w  w w.  j a v a 2s. c  o m
    cell.setCellValue(value);
    cell.setCellStyle(style);
}

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  www .  j  av  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();//  w w w  . j  ava2  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();
    }
}