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

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

Introduction

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

Prototype

@Override
public HSSFCellStyle createCellStyle() 

Source Link

Document

Create a new Cell style and add it to the workbook's style table.

Usage

From source file:cn.edu.henu.rjxy.lms.controller.TeaController.java

@RequestMapping("teacher/xz_xs_xx")
public @ResponseBody String daochuxuesheng(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    int classid = Integer.parseInt(request.getParameter("zjd_id"));
    int course_id = Integer.parseInt(request.getParameter("fjd_id"));
    int term = Integer.parseInt(request.getParameter("term"));
    String sn = getCurrentUsername();
    Teacher tec = TeacherDao.getTeacherBySn(sn);
    int tec_id = tec.getTeacherId();
    int trem_courseid = TermCourseDao.getTermCourseId(term, course_id, classid, tec_id);
    System.out.println(trem_courseid + "trem_courseid");
    PageBean<StuSelectResult> mystudent = TeacherDao.getStuSelectByTermCourseId(trem_courseid, 1, 300);
    HttpSession session = request.getSession();
    session.setAttribute("state", null);
    // ????  /* ww  w .j  ava2s .co  m*/
    response.setContentType("application/vnd.ms-excel");
    String codedFileName;
    codedFileName = "??";
    OutputStream fOut = null;
    try {
        // ????  
        codedFileName = java.net.URLEncoder.encode("", "UTF-8");
        response.setHeader("content-disposition", "attachment;filename=" + "student_messsage" + ".xls");
        HSSFWorkbook wb = new HSSFWorkbook();
        // webbooksheet,Excelsheet  
        HSSFSheet sheet = wb.createSheet("");
        // sheet0,??poiExcel?short  
        HSSFRow row = sheet.createRow((int) 0);
        // ?   
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // ? 
        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("?");
        cell.setCellStyle(style);
        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);
        cell = row.createCell((short) 5);
        cell.setCellValue("");
        cell.setCellStyle(style);
        cell = row.createCell((short) 6);
        cell.setCellValue("?");
        cell.setCellStyle(style);
        cell = row.createCell((short) 7);
        cell.setCellValue("qq");
        cell.setCellStyle(style);
        for (int i = 0; i < mystudent.getBeanList().size(); i++) {
            StuSelectResult a = (StuSelectResult) mystudent.getBeanList().get(i);
            System.out.println(getStudentById(a.getStuid()).getStudentSn());
            row = sheet.createRow((int) i + 1);
            row.createCell((short) 0).setCellValue(getStudentById(a.getStuid()).getStudentSn());
            row.createCell((short) 1).setCellValue(getStudentById(a.getStuid()).getStudentName());
            row.createCell((short) 2).setCellValue(getStudentById(a.getStuid()).getStudentIdcard());
            row.createCell((short) 3).setCellValue(getStudentById(a.getStuid()).getStudentGrade());
            row.createCell((short) 4)
                    .setCellValue((getStudentById(a.getStuid()).getStudentSex()) ? ("") : (""));
            row.createCell((short) 5).setCellValue(getStudentById(a.getStuid()).getStudentCollege());
            row.createCell((short) 6).setCellValue(getStudentById(a.getStuid()).getStudentTel());
            row.createCell((short) 7).setCellValue(getStudentById(a.getStuid()).getStudentQq());

        }

        fOut = response.getOutputStream();
        wb.write(fOut);
    } catch (UnsupportedEncodingException e1) {
    } catch (Exception e) {
    } finally {
        fOut.flush();
        fOut.close();
        session.setAttribute("state", "open");
    }
    System.out.println("?...");
    return "1";

}

From source file:cn.edu.henu.rjxy.lms.controller.Tea_Controller.java

@RequestMapping("xz_xs_xx")
public @ResponseBody String daochuxuesheng(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    int classid = Integer.parseInt(request.getParameter("zjd_id"));
    int course_id = Integer.parseInt(request.getParameter("fjd_id"));
    int term = Integer.parseInt(request.getParameter("term"));
    String sn = getCurrentUsername();
    Teacher tec = TeacherDao.getTeacherBySn(sn);
    int tec_id = tec.getTeacherId();
    int trem_courseid = TermCourseDao.getTermCourseId(term, course_id, classid, tec_id);
    System.out.println(trem_courseid + "trem_courseid");
    PageBean<StuSelectResult> mystudent = TeacherDao.getStuSelectByTermCourseId(trem_courseid, 1, 300);
    HttpSession session = request.getSession();
    session.setAttribute("state", null);
    // ????  /*w  w  w .ja v a2 s .  c  om*/
    response.setContentType("application/vnd.ms-excel");
    String codedFileName;
    codedFileName = "??";
    OutputStream fOut = null;
    try {
        // ????  
        codedFileName = java.net.URLEncoder.encode("", "UTF-8");
        response.setHeader("content-disposition", "attachment;filename=" + "student_messsage" + ".xls");
        HSSFWorkbook wb = new HSSFWorkbook();
        // webbooksheet,Excelsheet  
        HSSFSheet sheet = wb.createSheet("");
        // sheet0,??poiExcel?short  
        HSSFRow row = sheet.createRow((int) 0);
        // ?   
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // ? 
        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("?");
        cell.setCellStyle(style);
        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);
        cell = row.createCell((short) 5);
        cell.setCellValue("");
        cell.setCellStyle(style);
        cell = row.createCell((short) 6);
        cell.setCellValue("?");
        cell.setCellStyle(style);
        cell = row.createCell((short) 7);
        cell.setCellValue("qq");
        cell.setCellStyle(style);
        for (int i = 0; i < mystudent.getBeanList().size(); i++) {
            StuSelectResult a = (StuSelectResult) mystudent.getBeanList().get(i);
            System.out.println(getStudentById(a.getStuid()).getStudentSn());
            row = sheet.createRow((int) i + 1);
            row.createCell((short) 0).setCellValue(getStudentById(a.getStuid()).getStudentSn());
            row.createCell((short) 1).setCellValue(getStudentById(a.getStuid()).getStudentName());
            row.createCell((short) 2).setCellValue(getStudentById(a.getStuid()).getStudentIdcard());
            row.createCell((short) 3).setCellValue(getStudentById(a.getStuid()).getStudentGrade());
            row.createCell((short) 4)
                    .setCellValue((getStudentById(a.getStuid()).getStudentSex()) ? ("") : (""));
            row.createCell((short) 5).setCellValue(getStudentById(a.getStuid()).getStudentCollege());
            row.createCell((short) 6).setCellValue(getStudentById(a.getStuid()).getStudentTel());
            row.createCell((short) 7).setCellValue(getStudentById(a.getStuid()).getStudentQq());

        }

        fOut = response.getOutputStream();
        wb.write(fOut);
    } catch (UnsupportedEncodingException e1) {
    } catch (Exception e) {
    } finally {
        fOut.flush();
        fOut.close();
        session.setAttribute("state", "open");
    }
    System.out.println("?...");
    return "1";

}

From source file:cn.mario256.blog.ExcelView.java

License:Open Source License

/**
 * ?Excel/*from   w w  w .jav a 2 s  . c  o  m*/
 * 
 * @param model
 *            ?
 * @param workbook
 *            HSSFWorkbook
 * @param request
 *            HttpServletRequest
 * @param response
 *            HttpServletResponse
 */
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);
                    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 {
                    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:co.turnus.analysis.data.bottlenecks.io.XlsAlgoBottlenecksDataWriter.java

License:Open Source License

public void write(AlgoBottlenecksData report, File file) {
    try {/*w ww  .ja va2 s .  c  om*/
        HSSFWorkbook workbook = new HSSFWorkbook();

        titleFont = workbook.createFont();
        titleFont.setFontName("Arial");
        titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

        cellStyle = workbook.createCellStyle();
        cellStyle.setDataFormat(workbook.getCreationHelper().createDataFormat().getFormat("m/d/yy h:mm"));

        HotspotsDataAnalyser analyzer = new HotspotsDataAnalyser(report);
        writeSummary(workbook, report.getNetwork(), analyzer);
        writeActorClassesTable(workbook, analyzer);
        writeActorsTable(workbook, analyzer);
        writeActionActorClassTable(workbook, analyzer);
        writeActionActorTable(workbook, analyzer);

        // check if there are also impact analysis data
        ImpactData impactData = report.getImpactData();
        if (impactData != null) {
            writeImpactAnalysis(workbook, impactData);
        }

        OutputStream out = new FileOutputStream(file);
        out = new BufferedOutputStream(out);
        workbook.write(out);
        out.close();
    } catch (Exception e) {
        throw new TurnusRuntimeException("Error writing the excel file " + file, e.getCause());
    }

}

From source file:co.turnus.analysis.data.pipelining.io.XlsSimplePipeliningDataWriter.java

License:Open Source License

public void write(SimplePipelingData report, File file) {
    try {/*w ww .j a  v a2  s . c o  m*/

        HSSFWorkbook workbook = new HSSFWorkbook();

        titleFont = workbook.createFont();
        titleFont.setFontName("Arial");
        titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

        cellStyle = workbook.createCellStyle();
        cellStyle.setDataFormat(workbook.getCreationHelper().createDataFormat().getFormat("m/d/yy h:mm"));

        HSSFSheet sheet = workbook.createSheet("Pipelinable <Actor, Action>");
        // Action Actor Class Results
        Cell cell = sheet.createRow(0).createCell(0);
        HSSFRichTextString title = new HSSFRichTextString("Action and Actor Pipelining Results");
        title.applyFont(titleFont);
        cell.setCellValue(title);

        Row row = sheet.createRow(1);
        sheet.addMergedRegion(new CellRangeAddress(1, 3, 0, 0));
        row.createCell(0).setCellValue("Actor");
        sheet.addMergedRegion(new CellRangeAddress(1, 3, 1, 1));
        row.createCell(1).setCellValue("Action");
        sheet.addMergedRegion(new CellRangeAddress(1, 3, 2, 2));
        row.createCell(2).setCellValue("pipelinable");
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 3, 10));
        row.createCell(3).setCellValue("Consecutive Executions");
        sheet.addMergedRegion(new CellRangeAddress(1, 3, 11, 11));
        row.createCell(11).setCellValue("Splittable Actions");

        row = sheet.createRow(2);
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 3, 6));
        row.createCell(3).setCellValue("Pipelinable");
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 7, 10));
        row.createCell(7).setCellValue("Unconstrained");
        row = sheet.createRow(3);
        row.createCell(3).setCellValue("min");
        row.createCell(4).setCellValue("mean");
        row.createCell(5).setCellValue("max");
        row.createCell(6).setCellValue("var");
        row.createCell(7).setCellValue("min");
        row.createCell(8).setCellValue("mean");
        row.createCell(9).setCellValue("max");
        row.createCell(10).setCellValue("var");

        int rowId = 4;
        Map<Actor, Map<Action, SimpleActionPipeliningData>> table = report.asTable().rowMap();
        for (Map<Action, SimpleActionPipeliningData> ac : table.values()) {
            for (SimpleActionPipeliningData data : ac.values()) {
                row = sheet.createRow(rowId);
                row.createCell(0).setCellValue(data.getActor().getId());
                row.createCell(1).setCellValue(data.getAction().getId());

                row.createCell(2).setCellValue(data.isPipelinable());

                StatisticalData stat = data.getPipelinableRepetitions();
                if (stat.getSamples() > 0) {
                    row.createCell(3).setCellValue(stat.getMin());
                    row.createCell(4).setCellValue(stat.getMean());
                    row.createCell(5).setCellValue(stat.getMax());
                    row.createCell(6).setCellValue(stat.getVariance());
                } else {
                    row.createCell(3).setCellValue("-");
                    row.createCell(4).setCellValue("-");
                    row.createCell(5).setCellValue("-");
                    row.createCell(6).setCellValue("-");
                }

                stat = data.getUnconstrainedRepetitions();
                if (stat.getSamples() > 0) {
                    row.createCell(7).setCellValue(stat.getMin());
                    row.createCell(8).setCellValue(stat.getMean());
                    row.createCell(9).setCellValue(stat.getMax());
                    row.createCell(10).setCellValue(stat.getVariance());
                } else {
                    row.createCell(7).setCellValue("-");
                    row.createCell(8).setCellValue("-");
                    row.createCell(9).setCellValue("-");
                    row.createCell(10).setCellValue("-");
                }

                StringBuffer b = new StringBuffer();
                for (Action action : data.getSplittableActions()) {
                    b.append(action.getId()).append(" ");
                }
                row.createCell(11).setCellValue(b.toString());

                rowId++;
            }
        }

        OutputStream out = new FileOutputStream(file);
        out = new BufferedOutputStream(out);
        workbook.write(out);
        out.close();

    } catch (Exception e) {
        throw new TurnusRuntimeException("Error writing the excel file " + file, e.getCause());
    }
}

From source file:co.turnus.profiling.io.XlsHalsteadAnalysisWriter.java

License:Open Source License

public void write(SourceCodeData report, File file) {
    try {//from w w  w .  j av a2  s.c om
        HSSFWorkbook workbook = new HSSFWorkbook();

        titleFont = workbook.createFont();
        titleFont.setFontName("Arial");
        titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

        cellStyle = workbook.createCellStyle();
        cellStyle.setDataFormat(workbook.getCreationHelper().createDataFormat().getFormat("m/d/yy h:mm"));

        writeSummary(workbook, report.getNetwork(), report);
        writeActorClassesTable(workbook, report);

        OutputStream out = new FileOutputStream(file);
        out = new BufferedOutputStream(out);
        workbook.write(out);
        out.close();
    } catch (Exception e) {
        throw new TurnusRuntimeException("Error writing the excel file " + file, e.getCause());
    }

}

From source file:co.turnus.profiling.io.XlsProfilingDataWriter.java

License:Open Source License

public void write(File file, ProfilingData data) {
    try {/*from ww w .  j  a  va2 s. c om*/
        HSSFWorkbook workbook = new HSSFWorkbook();

        titleFont = workbook.createFont();
        titleFont.setFontName("Arial");
        titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

        cellStyle = workbook.createCellStyle();
        cellStyle.setDataFormat(workbook.getCreationHelper().createDataFormat().getFormat("m/d/yy h:mm"));

        // writeSummary(workbook, data.getNetwork(), data);
        writeFifosData(workbook, data);

        OutputStream out = new FileOutputStream(file);
        out = new BufferedOutputStream(out);
        workbook.write(out);
        out.close();
    } catch (Exception e) {
        throw new TurnusRuntimeException("Error writing the excel file " + file, e.getCause());
    }
}

From source file:com.aan.girsang.client.ui.master.barang.BarangPanel.java

private void exportExcel(List<Barang> dataList) throws IOException {
    if (dataList != null && !dataList.isEmpty()) {
        HSSFWorkbook workBook = new HSSFWorkbook();
        HSSFSheet sheet = workBook.createSheet();
        HSSFSheet worksheet = workBook.createSheet("Sheet 0");
        // Nama Field
        Row judul = sheet.createRow((short) 0);
        Cell cell = judul.createCell((short) 0);
        cell.setCellValue("This is a test of merging");
        HSSFRow headingRow = sheet.createRow((short) 2);
        headingRow.createCell((short) 0).setCellValue("ID");
        headingRow.createCell((short) 1).setCellValue("BARCODE 1");
        headingRow.createCell((short) 2).setCellValue("BARCODE 2");
        headingRow.createCell((short) 3).setCellValue("NAMA BARANG");
        headingRow.createCell((short) 4).setCellValue("GOLONGAN");
        headingRow.createCell((short) 5).setCellValue("SAT. JUAL");
        headingRow.createCell((short) 6).setCellValue("ST. TOKO");
        headingRow.createCell((short) 7).setCellValue("ST. GUDANG");
        headingRow.createCell((short) 8).setCellValue("SAT. BELI");
        headingRow.createCell((short) 9).setCellValue("ISI PEM.");
        headingRow.createCell((short) 10).setCellValue("HRG PEM.");
        headingRow.createCell((short) 11).setCellValue("HRG NORMAL");
        headingRow.createCell((short) 12).setCellValue("HRG MEMBER");
        headingRow.createCell((short) 13).setCellValue("JUAL");
        int panjang = headingRow.getLastCellNum() - 1;
        short rowNo = 3;

        sheet.addMergedRegion(new CellRangeAddress(0, //first row (0-based)
                0, //last row  (0-based)
                0, //first column (0-based)
                panjang //last column  (0-based)
        ));//from w w  w  .  ja va 2  s  .co m
        CellStyle styleData = workBook.createCellStyle();
        styleData.setBorderBottom(CellStyle.BORDER_THIN);
        styleData.setBorderRight(CellStyle.BORDER_THIN);
        styleData.setBorderLeft(CellStyle.BORDER_THIN);
        for (Barang b : dataList) {
            HSSFRow row = sheet.createRow(rowNo);
            String jual;
            if (b.getJual() == true) {
                jual = "Jual";
            } else {
                jual = "Tidak";
            }
            row.createCell((short) 0).setCellValue(b.getPlu());
            row.createCell((short) 1).setCellValue(b.getBarcode1());
            row.createCell((short) 2).setCellValue(b.getBarcode2());
            row.createCell((short) 3).setCellValue(b.getNamaBarang());
            row.createCell((short) 4).setCellValue(b.getGolonganBarang().getGolonganBarang());
            row.createCell((short) 5).setCellValue(b.getSatuan());
            row.createCell((short) 6).setCellValue(b.getStokToko());
            row.createCell((short) 7).setCellValue(b.getStokGudang());
            row.createCell((short) 8).setCellValue(b.getSatuanPembelian());
            row.createCell((short) 9).setCellValue(b.getIsiPembelian());
            row.createCell((short) 10).setCellValue(TextComponentUtils.formatNumber(b.getHargaBeli()));
            row.createCell((short) 11).setCellValue(TextComponentUtils.formatNumber(b.getHargaNormal()));
            row.createCell((short) 12).setCellValue(TextComponentUtils.formatNumber(b.getHargaMember()));
            row.createCell((short) 13).setCellValue(jual);
            for (int i = 0; i <= 13; i++) {
                row.getCell((short) i).setCellStyle(styleData);
            }
            rowNo++;
        }
        for (int i = 0; i <= 13; i++) {
            sheet.autoSizeColumn(i);
        }
        Font font = workBook.createFont();
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        //style judul
        CellStyle styleTitle = workBook.createCellStyle();
        styleTitle.setAlignment(CellStyle.ALIGN_CENTER_SELECTION);
        styleTitle.setFont(font);
        judul.getCell(0).setCellStyle(styleTitle);

        //judul field
        CellStyle styleHeading = workBook.createCellStyle();
        styleHeading.setFont(font);
        styleHeading.setAlignment(CellStyle.ALIGN_CENTER_SELECTION);
        styleHeading.setBorderBottom(CellStyle.BORDER_THIN);
        styleHeading.setBorderTop(CellStyle.BORDER_THIN);
        styleHeading.setBorderRight(CellStyle.BORDER_THIN);
        styleHeading.setBorderLeft(CellStyle.BORDER_THIN);
        for (int i = 0; i < headingRow.getLastCellNum(); i++) {//For each cell in the row 
            headingRow.getCell(i).setCellStyle(styleHeading);//Set the style
        }
        String file = "D:/Student_detais.xls";
        try {
            try (FileOutputStream fos = new FileOutputStream(file)) {
                workBook.write(fos);
            }
            JOptionPane.showMessageDialog(null, "Sukses");
        } catch (FileNotFoundException e) {
            System.out.println("Invalid directory or file not found");
        } catch (IOException e) {
            System.out.println("Error occurred while writting excel file to directory");
        }
    }
}

From source file:com.abacus.reports.ExcelBuilder.java

@Override
protected void buildExcelDocument(Map<String, Object> map, HSSFWorkbook workbook, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    // get data model which is passed by the Spring container
    List headerlist = (List) map.get("header");
    List<Object[]> data = (List) map.get("data");
    String reportname = String.valueOf(map.get("report_name"));

    // create a new Excel sheet
    HSSFSheet sheet = workbook.createSheet(reportname);
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-disposition", "attachment; filename=" + reportname + ".xls");
    OutputStream outStream = response.getOutputStream();
    sheet.setDefaultColumnWidth(30);//from w  ww .j  a  v a  2s. c  o m

    // create style for header cells
    CellStyle style = workbook.createCellStyle();
    HSSFFont font = workbook.createFont();
    HSSFPalette palette = workbook.getCustomPalette();
    HSSFColor color = palette.findSimilarColor(152, 35, 17);
    short paindex = color.getIndex();
    font.setFontName("Trebuchet MS");
    style.setFillForegroundColor(paindex);
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    font.setColor(HSSFColor.WHITE.index);
    style.setFont(font);

    // create header row
    HSSFRow header = sheet.createRow(0);
    int row = 0;
    for (Object headerlist1 : headerlist) {

        header.createCell(row).setCellValue(String.valueOf(headerlist1));
        header.getCell(row).setCellStyle(style);
        row++;
    }

    CellStyle style2 = workbook.createCellStyle();
    HSSFFont font2 = workbook.createFont();
    font2.setFontName("Trebuchet MS");
    style2.setFont(font2);

    System.out.println("data.size(): " + data.size());
    int rownum = 1;
    // create data rows         
    for (int rowCount = 0; rowCount < data.size(); rowCount++) {
        HSSFRow aRow = sheet.createRow(rownum);
        Object[] value = data.get(rowCount);
        int col = 0;
        for (Object value1 : value) {
            HSSFCell cell = aRow.createCell(col++);
            cell.setCellStyle(style2);
            if (value1 instanceof java.lang.String)
                cell.setCellValue(String.valueOf(value1));
            if (value1 instanceof java.lang.Integer)
                cell.setCellValue(Integer.parseInt(String.valueOf(value1)));
            if (value1 instanceof java.lang.Boolean)
                cell.setCellValue(Integer.parseInt(String.valueOf(value1)));
            if (value1 instanceof java.lang.Double)
                cell.setCellValue(Double.parseDouble(String.valueOf(value1)));
            if (value1 instanceof java.lang.Float)
                cell.setCellValue(Float.parseFloat(String.valueOf(value1)));
        }
        rownum++;
    }
    workbook.write(outStream);
    outStream.close();
}

From source file:com.accenture.control.GerenciaPlanilhaTS.java

public void geraNovaPlanilhaTS(String dir, List<TesteCaseTSBean> listTS)
        throws FileNotFoundException, IOException {

    FileInputStream arquivo = new FileInputStream(new File(dir));
    HSSFWorkbook workbook = new HSSFWorkbook(arquivo);
    HSSFSheet sheetTS = workbook.getSheetAt(0);

    HSSFDataFormat format = workbook.createDataFormat();
    HSSFCellStyle estilo = workbook.createCellStyle();
    String formatData = "aaaa-mm-dd\"T12:00:00-03:00\"";

    int linha = 1;

    Row row = sheetTS.getRow(linha);/*ww  w .ja v a 2  s  .co  m*/

    Cell descriptionPlan = row.getCell(0);
    Cell prj = row.getCell(1);
    Cell fase = row.getCell(2);
    Cell testPhase = row.getCell(3);
    Cell testScriptName = row.getCell(4);
    Cell testScriptDescription = row.getCell(5);
    Cell stepNo = row.getCell(6);
    Cell stepDescription = row.getCell(7);
    Cell expectedResults = row.getCell(8);
    Cell product = row.getCell(9);
    Cell dataPlanejada = row.getCell(10);

    for (int i = 0; i < listTS.size(); i++) {

        estilo.setDataFormat(format.getFormat(formatData));
        estilo.setFillBackgroundColor(HSSFColor.GREEN.index);

        row = sheetTS.getRow(linha);

        descriptionPlan = row.getCell(0);
        prj = row.getCell(1);
        fase = row.getCell(2);
        testPhase = row.getCell(3);
        testScriptName = row.getCell(4);
        testScriptDescription = row.getCell(5);
        stepNo = row.getCell(6);
        stepDescription = row.getCell(7);
        expectedResults = row.getCell(8);
        product = row.getCell(9);
        dataPlanejada = row.getCell(10);

        descriptionPlan.setCellValue(listTS.get(i).getTestPlan());
        prj.setCellValue(listTS.get(i).getSTIPRJ());
        fase.setCellValue(listTS.get(i).getFASE());
        testPhase.setCellValue(listTS.get(i).getTestPhase());
        testScriptName.setCellValue(listTS.get(i).getTestScriptName());
        testScriptDescription.setCellValue(listTS.get(i).getTestScriptDescription());
        stepNo.setCellValue(listTS.get(i).getSTEP_NUMERO());
        stepDescription.setCellValue(listTS.get(i).getStepDescription());
        expectedResults.setCellValue(listTS.get(i).getExpectedResults());
        product.setCellValue(listTS.get(i).getProduct());
        dataPlanejada.setCellValue(listTS.get(i).getDataPlanejada());

        dataPlanejada.setCellStyle(estilo);

        linha = linha + 2;

    }

    FileOutputStream fileOut = new FileOutputStream(new File(dir));
    workbook.write(fileOut);
    fileOut.close();
    arquivo.close();

}