Example usage for org.apache.poi.hssf.usermodel HSSFWorkbook createFont

List of usage examples for org.apache.poi.hssf.usermodel HSSFWorkbook createFont

Introduction

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

Prototype


@Override
public HSSFFont createFont() 

Source Link

Document

create a new Font and add it to the workbook's font table

Usage

From source file:userInterface.HospitalAdminRole.ManagePatientsJPanel.java

private void saveReportBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveReportBtnActionPerformed
    try {/*  ww  w  .j  a  v a2 s .  c om*/
        HSSFWorkbook fWorkbook = new HSSFWorkbook();
        HSSFSheet fSheet = fWorkbook.createSheet("new Sheet");
        HSSFFont sheetTitleFont = fWorkbook.createFont();
        File file = new File("C:\\Users\\Reshmi\\OneDrive\\Documents\\reports.xls");
        HSSFCellStyle cellStyle = fWorkbook.createCellStyle();

        sheetTitleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        //sheetTitleFont.setColor();
        TableModel model = vitalSignjTable.getModel();
        TableColumnModel tcm = vitalSignjTable.getColumnModel();
        HSSFRow fRow1 = fSheet.createRow((short) 0);

        for (int j = 0; j < tcm.getColumnCount(); j++) {

            HSSFCell cell = fRow1.createCell((short) j);
            cell.setCellValue(tcm.getColumn(j).getHeaderValue().toString());

        }

        for (int i = 0; i < model.getRowCount(); i++) {

            HSSFRow fRow = fSheet.createRow((short) i + 1);
            for (int j = 0; j < model.getColumnCount(); j++) {
                HSSFCell cell = fRow.createCell((short) j);
                cell.setCellValue(tcm.getColumn(j).getHeaderValue().toString());
                cell.setCellValue(model.getValueAt(i, j).toString());
                cell.setCellStyle(cellStyle);

            }

        }
        FileOutputStream fileOutputStream;
        fileOutputStream = new FileOutputStream(file);
        BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
        fWorkbook.write(bos);
        bos.close();
        fileOutputStream.close();
        JOptionPane.showMessageDialog(null, "File saved as reports.xls ", "Export",
                JOptionPane.INFORMATION_MESSAGE);
        //                Runtime run = Runtime.getRuntime();
        //                run.exec("cmd.exetart " + file);
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "File not saved", "Export", JOptionPane.INFORMATION_MESSAGE);
    }
}

From source file:util.ExcelConverter.java

public static File createXls(String[] header, String[][] data, String path) {

    try {//from  w  ww.  ja v  a  2  s  .  co  m
        HSSFWorkbook xwb = new HSSFWorkbook();
        HSSFSheet sheet = xwb.createSheet();

        CellStyle cellStyle = xwb.createCellStyle();
        cellStyle.setAlignment(CellStyle.ALIGN_LEFT);
        cellStyle.setAlignment(CellStyle.VERTICAL_TOP);
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cellStyle.setWrapText(false);

        Font bold = xwb.createFont();
        bold.setBoldweight(Font.BOLDWEIGHT_BOLD);
        bold.setFontHeightInPoints((short) 10);

        CellStyle cellStyleHeader = xwb.createCellStyle();
        cellStyleHeader.setAlignment(CellStyle.ALIGN_LEFT);
        cellStyleHeader.setAlignment(CellStyle.VERTICAL_TOP);
        cellStyleHeader.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cellStyleHeader.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cellStyleHeader.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cellStyleHeader.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cellStyleHeader.setFont(bold);
        cellStyleHeader.setWrapText(false);

        HSSFRow row;
        Cell cell;

        //header
        row = sheet.createRow(0);
        for (int i = 0; i < header.length; i++) {
            cell = row.createCell(i);
            cell.setCellStyle(cellStyleHeader);
            cell.setCellValue(header[i]);
        }

        int colCount = header.length;
        int no = 1;

        for (String[] obj : data) {
            row = sheet.createRow(no);
            for (int i = 0; i < colCount; i++) {
                cell = row.createCell(i);
                cell.setCellStyle(cellStyle);
                cell.setCellValue(obj[i]);
            }
            no++;
        }

        for (int i = 0; i < header.length; i++) {
            sheet.autoSizeColumn(i);
        }

        File newFile = new File(path);
        try (FileOutputStream fileOut = new FileOutputStream(newFile)) {
            xwb.write(fileOut);
        }

        return newFile;
    } catch (IOException e) {
        return null;
    }
}

From source file:utilesBD.servidoresDatos.JServerServidorDatosExcel.java

public static void guardar(JListDatos poList, File poFile, boolean pbCabezera) throws Throwable {

    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet(poList.msTabla);
    int lHoja = 1;
    HSSFCellStyle style = workbook.createCellStyle();
    //        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    style.setWrapText(true);/*from  w  w  w. ja va 2s  .  co  m*/
    HSSFFont font = workbook.createFont();
    font.setFontHeightInPoints((short) 10);
    font.setFontName("Courier New");
    font.setItalic(true);
    // font.setStrikeout(true);
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    //font.setColor(Short.parseShort("#ffffff"));
    style.setFont(font);

    int lFila = 0;
    if (pbCabezera) {
        lFila++;
        crearCabecera(poList, sheet, style, lFila);
    }
    if (poList.moveFirst()) {
        do {
            lFila++;
            toExcel(poList, workbook, lFila, sheet);
            if (lFila > 65524) {
                lFila = 0;
                if (pbCabezera) {
                    lFila++;
                    crearCabecera(poList, sheet, style, lFila);
                }
                lHoja++;
                sheet = workbook.createSheet(poList.msTabla + String.valueOf(lHoja));
            }
        } while (poList.moveNext());

    }

    // Escribir el fichero.
    OutputStream fileOut = new FileOutputStream(poFile);
    try {
        workbook.write(fileOut);
    } finally {
        fileOut.close();
    }
}

From source file:Utility.TestReports.java

public void setHeaderCellStyle(org.apache.poi.hssf.usermodel.HSSFSheet sheet,
        org.apache.poi.ss.usermodel.Cell cell, org.apache.poi.hssf.usermodel.HSSFWorkbook wb) {
    CellStyle s = null;/*from w w w. jav a 2  s. c o m*/
    s = sheet.getWorkbook().createCellStyle();
    cell.setCellStyle(s);

    HSSFFont font = wb.createFont();
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    s.setFont(font);
    s.setAlignment(CellStyle.ALIGN_GENERAL);
    s.setBorderBottom(CellStyle.BORDER_THIN);
    s.setBorderLeft(CellStyle.BORDER_THIN);
    s.setBorderRight(CellStyle.BORDER_THIN);
    s.setBorderTop(CellStyle.BORDER_THIN);
    s.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    s.setAlignment(CellStyle.ALIGN_LEFT);
    s.setFont(font);

}

From source file:xx.tream.chengxin.ms.action.TrainReportAction.java

@RequestMapping({ "/toExport" })
public String toExport(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response,
        FormMap formMap, Integer currentPage, Integer pageSize) throws IOException {
    Map<String, Object> qm = formMap.getFormMap();
    List<Map<String, Object>> list = this.trainService.queryForParam(qm);
    //Map<String, Object> statisticsMap = this.trainService.statistics(qm);
    String trainIds = this.getTrainIds(list);
    List<Map<String, Object>> payingList = this.payingService.queryByTrainIds(trainIds);
    List<Map<String, Object>> incomeList = this.incomeService.queryByTrainIds(trainIds);
    List<Map<String, Object>> payoutList = this.payoutService.queryByTrainIds(trainIds);
    Map<Long, List<Map<String, Object>>> payingMap = converList(payingList);
    Map<Long, List<Map<String, Object>>> incomeMap = converList(incomeList);
    Map<Long, List<Map<String, Object>>> payoutMap = converList(payoutList);

    OutputStream os = response.getOutputStream();
    response.reset();/*  w w  w.  j a v a  2 s.c  o m*/
    response.setCharacterEncoding("UTF-8");
    String title = "?" + getDateFile() + ".xls";
    response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(title, "UTF-8"));
    response.setContentType("application/vnd.ms-excel");
    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet("?");
    HSSFRow headrow = sheet.createRow(0);
    HSSFCellStyle headcell = workbook.createCellStyle();

    HSSFFont font = workbook.createFont();
    font.setFontName("");
    font.setFontHeightInPoints((short) 10);
    headcell.setFont(font);
    headrow.setRowStyle(headcell);
    String payingValue[] = { "", "paying", "createUserName", "createTime", "createTime", "", "auditUserName",
            "auditTime", "auditTime" };
    String incomeValue[] = { "type", "income", "createUserName", "createTime", "createTime", "note",
            "auditUserName", "auditTime", "auditTime" };
    String payoutValue[] = { "type", "payout", "createUserName", "createTime", "createTime", "",
            "auditUserName", "auditTime", "auditTime" };
    String[] heads = { "", "?", "?", "??", "??", "",
            "", "", "", "", "/", "/?", "C1/C2",
            "", "", "", "", "", "?", "?",
            "?", "?", "", "", "", "" };
    String[] values = { "", "id", "autumnNumber", "name", "idcard", "pay", "allpaying", "count_all", "allip",
            "canpay", "newOrOld", "type", "licenseTag", "createUserName", "createTime", "createTime", "note" };
    HSSFCellStyle headStyle = ExcelUtil.headCell(workbook);
    HSSFCell cell = null;
    //
    int cr = 0;
    for (int i = 0; i < heads.length; i++) {
        cell = headrow.createCell(cr);
        cell.setCellValue(heads[i]);
        cell.setCellStyle(headStyle);
        sheet.setColumnWidth(cr, 5000);
        cr++;
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
    HSSFCellStyle rowStyle = ExcelUtil.leftCell(workbook);
    HSSFCellStyle paleBlueStyle = ExcelUtil.paleBlueForgroundCell(workbook);
    //HSSFCellStyle redFontStyle = ExcelUtil.leftFontRedCell(workbook);
    //HSSFCellStyle redFontForegroudStyle = ExcelUtil.leftFontRedForegroudCell(workbook);
    HSSFCellStyle rightStyle = ExcelUtil.rightCell(workbook);
    HSSFCellStyle rightForegroudStyle = ExcelUtil.rightPaleBlueForgroundCell(workbook);
    HSSFCellStyle rightredFontStyle = ExcelUtil.rightFontRedCell(workbook);
    HSSFCellStyle rightredFontForegroudStyle = ExcelUtil.rightFontRedForegroudCell(workbook);
    double ac[] = new double[5];

    //
    int rn = 1;
    HSSFRow row = null;
    //??
    for (int i = 0; i < list.size(); i++) {
        int a = 0;
        //
        int pb = 0;
        //
        cr = 0;
        row = sheet.createRow(rn++);
        Map<String, Object> trainMap = (Map<String, Object>) list.get(i);
        List<Map<String, Object>> payL = (List<Map<String, Object>>) payingMap.get((Long) trainMap.get("id"));
        List<Map<String, Object>> incomeL = (List<Map<String, Object>>) incomeMap
                .get((Long) trainMap.get("id"));
        List<Map<String, Object>> payoutL = (List<Map<String, Object>>) payoutMap
                .get((Long) trainMap.get("id"));
        for (int v = 0; v < values.length; v++) {
            cell = row.createCell(cr++);
            if (trainMap.get(values[v]) != null) {
                if (v == 14) {
                    cell.setCellValue(sdf.format((Date) trainMap.get(values[v])));
                } else if (v == 15) {
                    cell.setCellValue(sdf2.format((Date) trainMap.get(values[v])));
                } else if (v == 5 || v == 6 || v == 7 || v == 8 || v == 9) {
                    Double d = trainMap.get(values[v]) == null ? 0 : (Double) trainMap.get(values[v]);
                    ac[a] += d;
                    a++;
                    cell.setCellValue((Double) trainMap.get(values[v]));
                } else if (v == 1) {
                    cell.setCellValue((Long) trainMap.get(values[v]));
                } else {
                    cell.setCellValue((String) trainMap.get(values[v]));
                }
            } else {
                if (v == 0) {
                    cell.setCellValue(i + 1);
                } else {
                    cell.setCellValue("");
                }
            }
            if (v == 5 || v == 6 || v == 7 || v == 8 || v == 9) {//?
                cell.setCellStyle(rightForegroudStyle);
            } else {
                cell.setCellStyle(paleBlueStyle);
            }

        }

        //
        if (payL != null && payL.size() > 0) {
            for (int p = 0; p < payL.size(); p++) {
                Map<String, Object> pMap = payL.get(p);
                cr = values.length;
                for (int v = 0; v < payingValue.length; v++) {
                    cell = row.createCell(cr++);
                    if (v == 0) {
                        cell.setCellValue("" + (p + 1));
                    } else {
                        if (pMap.get(payingValue[v]) != null) {
                            if (v == 3 || v == 7) {
                                cell.setCellValue(sdf.format((Date) pMap.get(payingValue[v])));
                            } else if (v == 4 || v == 8) {
                                cell.setCellValue(sdf2.format((Date) pMap.get(payingValue[v])));
                            } else if (v == 1) {
                                Double nv = (Double) pMap.get(payingValue[v]);
                                cell.setCellValue(nv);
                            } else {
                                cell.setCellValue((String) pMap.get(payingValue[v]));
                            }
                        } else {
                            cell.setCellValue("");
                        }
                    }
                    if (v == 1) {//?
                        if (pb == 0) {
                            cell.setCellStyle(rightForegroudStyle);
                        } else {
                            cell.setCellStyle(rightStyle);
                        }
                    } else {
                        if (pb == 0) {
                            cell.setCellStyle(paleBlueStyle);

                        } else {
                            cell.setCellStyle(rowStyle);
                        }
                    }
                }
                pb++;
                row = sheet.createRow(rn++);

            }

        }
        //
        if (incomeL != null && incomeL.size() > 0) {
            for (int p = 0; p < incomeL.size(); p++) {
                Map<String, Object> iMap = incomeL.get(p);
                cr = values.length;
                for (int v = 0; v < incomeValue.length; v++) {
                    cell = row.createCell(cr++);
                    if (v == 0) {
                        cell.setCellValue(iMap.get(incomeValue[v]) + "()");
                    } else {
                        if (iMap.get(incomeValue[v]) != null) {
                            if (v == 3 || v == 7) {
                                cell.setCellValue(sdf.format((Date) iMap.get(incomeValue[v])));
                            } else if (v == 4 || v == 8) {
                                cell.setCellValue(sdf2.format((Date) iMap.get(incomeValue[v])));
                            } else if (v == 1) {
                                cell.setCellValue((Double) iMap.get(incomeValue[v]));
                            } else {
                                cell.setCellValue((String) iMap.get(incomeValue[v]));
                            }
                        } else {
                            cell.setCellValue("");
                        }
                    }
                    if (v == 1) {//?
                        if (pb == 0) {
                            cell.setCellStyle(rightForegroudStyle);
                        } else {
                            cell.setCellStyle(rightStyle);
                        }
                    } else {
                        if (pb == 0) {
                            cell.setCellStyle(paleBlueStyle);

                        } else {
                            cell.setCellStyle(rowStyle);
                        }
                    }
                }
                pb++;
                row = sheet.createRow(rn++);
            }
        }
        boolean flag = false;
        //
        if (payoutL != null && payoutL.size() > 0) {
            for (int p = 0; p < payoutL.size(); p++) {
                Map<String, Object> pMap = payoutL.get(p);
                cr = values.length;
                for (int v = 0; v < payoutValue.length; v++) {
                    cell = row.createCell(cr++);
                    if (v == 0) {
                        cell.setCellValue(pMap.get(payoutValue[v]) + "()");
                    } else {
                        if (pMap.get(payoutValue[v]) != null) {
                            if (v == 3 || v == 7) {
                                cell.setCellValue(sdf.format((Date) pMap.get(payoutValue[v])));
                            } else if (v == 4 || v == 8) {
                                cell.setCellValue(sdf2.format((Date) pMap.get(payoutValue[v])));
                            } else if (v == 1) {
                                flag = true;
                                cell.setCellValue(0 - (Double) pMap.get(payoutValue[v]));
                            } else {
                                cell.setCellValue((String) pMap.get(payoutValue[v]));
                            }
                        } else {
                            cell.setCellValue("");
                        }
                    }
                    if (pb == 0 && flag) {
                        flag = false;
                        cell.setCellStyle(rightredFontForegroudStyle);
                    } else if (flag) {
                        flag = false;
                        cell.setCellStyle(rightredFontStyle);
                    } else if (pb == 0) {
                        cell.setCellStyle(paleBlueStyle);
                    } else {
                        cell.setCellStyle(rowStyle);
                    }
                }
                pb++;
                if (p != payoutL.size() - 1) {
                    row = sheet.createRow(rn++);
                }
            }
        }
    }
    if (list != null && list.size() > 0) {
        row = sheet.createRow(rn++);
        cell = row.createCell(0);
        cell.setCellValue("?");
        cell.setCellStyle(headStyle);
        for (int i = 0; i < ac.length; i++) {
            cell = row.createCell(5 + i);
            cell.setCellValue(ac[i]);
            cell.setCellStyle(rightStyle);
        }
    }

    workbook.write(os);
    os.flush();
    os.close();
    return null;
}