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

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

Introduction

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

Prototype

private void write(POIFSFileSystem fs) throws IOException 

Source Link

Document

Writes the workbook out to a brand new, empty POIFS

Usage

From source file:de.thorstenberger.taskmodel.impl.ReportBuilderImpl.java

License:Open Source License

public void createExcelAnalysisForBlock(long taskId, String categoryId, int blockIndex, OutputStream out)
        throws TaskApiException, MethodNotSupportedException {

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet();/*from w w w . j  a va 2s  . co m*/
    TaskDef_Complex taskDef;
    try {
        taskDef = (TaskDef_Complex) taskManager.getTaskDef(taskId);
    } catch (ClassCastException e) {
        throw new MethodNotSupportedException("Only analysis of complexTasks supported (for now)!");
    }
    Category category = taskDef.getComplexTaskDefRoot().getCategories().get(categoryId);
    Block block = category.getBlock(blockIndex);

    short r = 0;
    short c = 0;

    HSSFRow row = sheet.createRow(r++);
    row.createCell(c++).setCellValue("Login");
    row.createCell(c++).setCellValue("Vorname");
    row.createCell(c++).setCellValue("Name");

    if (!block.getType().equals("mc"))
        throw new MethodNotSupportedException("Only MC analysis supported (for now)!");

    List<SubTaskDefOrChoice> stdocs = block.getSubTaskDefOrChoiceList();
    List<SubTaskDef> stds = new ArrayList<SubTaskDef>();
    // put all SubTaskDefs into stds
    for (SubTaskDefOrChoice stdoc : stdocs) {
        if (stdoc instanceof SubTaskDef) {
            stds.add((SubTaskDef) stdoc);
            row.createCell(c++).setCellValue(((SubTaskDef) stdoc).getId());
        } else {
            Choice choice = (Choice) stdoc;
            for (SubTaskDef std : choice.getSubTaskDefs()) {
                stds.add(std);
                row.createCell(c++).setCellValue(std.getId());
            }
        }
    }

    List<Tasklet> tasklets = taskManager.getTaskletContainer().getTasklets(taskId);

    for (Tasklet tasklet : tasklets) {

        if (tasklet.getStatus() == Tasklet.Status.INITIALIZED) {
            continue;
        }

        if (!(tasklet instanceof ComplexTasklet))
            throw new MethodNotSupportedException("Only analysis of complexTasklets supported (for now)!");

        row = sheet.createRow(r++);
        c = 0;

        ComplexTasklet ct = (ComplexTasklet) tasklet;
        Try actualTry = ct.getSolutionOfLatestTry();

        c = createUserInfoColumns(tasklet, c, wb, row);

        for (SubTaskDef std : stds) {

            SubTasklet_MC mcst = (SubTasklet_MC) actualTry.lookupSubTasklet(std);
            if (mcst == null) {
                row.createCell(c++).setCellValue("[n/a]");
                continue;
            }

            StringBuilder sb = new StringBuilder();
            List<SubTasklet_MC.Answer> answers = mcst.getAnswers();
            boolean first = true;
            for (SubTasklet_MC.Answer answer : answers) {
                if (answer.isSelected()) {
                    if (!first) {
                        sb.append(";");
                    }
                    sb.append(answer.getId());
                    first = false;
                }
            }
            row.createCell(c++).setCellValue(sb.toString());

        }

    }

    try {
        wb.write(out);
        out.flush();
    } catch (IOException e) {
        log.error("Error writing excel stream!", e);
        throw new TaskApiException(e);
    } finally {
        try {
            out.close();
        } catch (IOException e) {
            throw new TaskApiException(e);
        }
    }

}

From source file:demo.admin.controller.UserController.java

@RequestMapping(value = "/user/downloadData")
@VerifyAuthentication(Trader = true, Admin = true, Operation = true)
public HttpEntity<byte[]> downloadUserData(String status, String securephone,
        @RequestParam(value = "startDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
        @RequestParam(value = "endDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate)
        throws IOException, DocumentException {
    if (securephone != null && securephone != "") {
        securephone = Where.$like$(securephone);
    }//from   www. j  a v a 2 s.  com
    List<Map<String, Object>> users = userMapper.userExport(status, securephone, startDate, endDate);
    String type = status + "?";
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet(type);
    HSSFRow row = sheet.createRow(0);
    CellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
    cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

    sheet.setVerticallyCenter(true);
    sheet.setHorizontallyCenter(true);
    String[] excelHeader = { "??", "???", "??", "", "??" };
    for (int i = 0; i < excelHeader.length; i++) {
        HSSFCell cell = row.createCell(i);
        cell.setCellValue(excelHeader[i]);
        cell.setCellStyle(cellStyle);
        sheet.autoSizeColumn(i, true);
    }
    for (int i = 0; i < users.size(); i++) {
        Map<String, Object> resultSet = users.get(i);
        sheet.autoSizeColumn(i, true);
        row = sheet.createRow(i + 1);
        row.setRowStyle(cellStyle);
        row.createCell(0).setCellValue(i + 1);
        row.createCell(1).setCellValue(String.valueOf(resultSet.get("name")));
        row.createCell(2).setCellValue(String.valueOf(resultSet.get("address")));
        row.createCell(3).setCellValue(String.valueOf(resultSet.get("registertime")));
        row.createCell(4).setCellValue(String.valueOf(resultSet.get("securephone")));

    }
    File file = File.createTempFile(".xls", ".xls");
    OutputStream out = new FileOutputStream(file);
    wb.write(out);
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    headers.setContentDispositionFormData("attachment",
            URLEncoder.encode(type, "UTF-8") + LocalDate.now() + ".xls");
    return new HttpEntity<byte[]>(FileUtils.readFileToByteArray(file), headers);
}

From source file:demo.poi.BigExample.java

License:Apache License

public static void main(String[] args) throws IOException {
    int rownum;//from  ww  w  .j a  va  2 s  .  c  om

    // create a new file
    FileOutputStream out = new FileOutputStream("target/bigworkbook.xls");
    // create a new workbook
    HSSFWorkbook wb = new HSSFWorkbook();
    // create a new sheet
    HSSFSheet s = wb.createSheet();
    // declare a row object reference
    HSSFRow r = null;
    // declare a cell object reference
    HSSFCell c = null;
    // create 3 cell styles
    HSSFCellStyle cs = wb.createCellStyle();
    HSSFCellStyle cs2 = wb.createCellStyle();
    HSSFCellStyle cs3 = wb.createCellStyle();
    // create 2 fonts objects
    HSSFFont f = wb.createFont();
    HSSFFont f2 = wb.createFont();

    //set font 1 to 12 point type
    f.setFontHeightInPoints((short) 12);
    //make it red
    f.setColor(HSSFColor.RED.index);
    // make it bold
    //arial is the default font
    f.setBoldweight(Font.BOLDWEIGHT_BOLD);

    //set font 2 to 10 point type
    f2.setFontHeightInPoints((short) 10);
    //make it the color at palette index 0xf (white)
    f2.setColor(HSSFColor.WHITE.index);
    //make it bold
    f2.setBoldweight(Font.BOLDWEIGHT_BOLD);

    //set cell stlye
    cs.setFont(f);
    //set the cell format see HSSFDataFromat for a full list
    cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)"));

    //set a thin border
    cs2.setBorderBottom(CellStyle.BORDER_THIN);
    //fill w fg fill color
    cs2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    // set foreground fill to red
    cs2.setFillForegroundColor(HSSFColor.RED.index);

    // set the font
    cs2.setFont(f2);

    // set the sheet name to HSSF Test
    wb.setSheetName(0, "HSSF Test");
    // create a sheet with 300 rows (0-299)
    for (rownum = 0; rownum < 300; rownum++) {
        // create a row
        r = s.createRow(rownum);
        // on every other row
        if ((rownum % 2) == 0) {
            // make the row height bigger  (in twips - 1/20 of a point)
            r.setHeight((short) 0x249);
        }

        //r.setRowNum(( short ) rownum);
        // create 50 cells (0-49) (the += 2 becomes apparent later
        for (int cellnum = 0; cellnum < 50; cellnum += 2) {
            // create a numeric cell
            c = r.createCell(cellnum);
            // do some goofy math to demonstrate decimals
            c.setCellValue(rownum * 10000 + cellnum + (((double) rownum / 1000) + ((double) cellnum / 10000)));

            // on every other row
            if ((rownum % 2) == 0) {
                // set this cell to the first cell style we defined
                c.setCellStyle(cs);
            }

            // create a string cell (see why += 2 in the
            c = r.createCell(cellnum + 1);

            // set the cell's string value to "TEST"
            c.setCellValue("TEST");
            // make this column a bit wider
            s.setColumnWidth(cellnum + 1, (int) ((50 * 8) / ((double) 1 / 20)));

            // on every other row
            if ((rownum % 2) == 0) {
                // set this to the white on red cell style
                // we defined above
                c.setCellStyle(cs2);
            }

        }
    }

    //draw a thick black border on the row at the bottom using BLANKS
    // advance 2 rows
    rownum++;
    rownum++;

    r = s.createRow(rownum);

    // define the third style to be the default
    // except with a thick black border at the bottom
    cs3.setBorderBottom(CellStyle.BORDER_THICK);

    //create 50 cells
    for (int cellnum = 0; cellnum < 50; cellnum++) {
        //create a blank type cell (no value)
        c = r.createCell(cellnum);
        // set it to the thick black border style
        c.setCellStyle(cs3);
    }

    //end draw thick black border

    // demonstrate adding/naming and deleting a sheet
    // create a sheet, set its title then delete it
    s = wb.createSheet();
    wb.setSheetName(1, "DeletedSheet");
    wb.removeSheetAt(1);
    //end deleted sheet

    // write the workbook to the output stream
    // close our file (don't blow out our file handles
    wb.write(out);
    out.close();
}

From source file:demo.poi.Outlines.java

License:Apache License

private static void createCase1(String filename) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet("new sheet");

    sheet1.groupColumn(4, 7);/*from w  ww . j a va 2s  .com*/

    for (int row = 0; row < 200; row++) {
        HSSFRow r = sheet1.createRow(row);
        for (int column = 0; column < 200; column++) {
            HSSFCell c = r.createCell(column);
            c.setCellValue(column);
        }
    }

    FileOutputStream fileOut = new FileOutputStream(filename);
    wb.write(fileOut);
    fileOut.close();
}

From source file:demo.poi.Outlines.java

License:Apache License

private static void createCase2(String filename) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet("new sheet");

    sheet1.groupColumn(2, 10);//from   ww  w.j  a v a2s. co  m
    sheet1.groupColumn(4, 7);
    sheet1.setColumnGroupCollapsed(4, true);

    FileOutputStream fileOut = new FileOutputStream(filename);
    wb.write(fileOut);
    fileOut.close();
}

From source file:demo.poi.Outlines.java

License:Apache License

private static void createCase3(String filename) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet("new sheet");

    sheet1.groupColumn(2, 10);/*from   ww w .  j  a v  a2s .c om*/
    sheet1.groupColumn(4, 7);
    sheet1.setColumnGroupCollapsed(4, true);
    sheet1.setColumnGroupCollapsed(2, true);

    FileOutputStream fileOut = new FileOutputStream(filename);
    wb.write(fileOut);
    fileOut.close();
}

From source file:demo.poi.Outlines.java

License:Apache License

private static void createCase4(String filename) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet("new sheet");

    sheet1.groupColumn(2, 10);// www  . java2 s.  c  o m
    sheet1.groupColumn(4, 7);
    sheet1.setColumnGroupCollapsed(4, true);
    sheet1.setColumnGroupCollapsed(2, true);

    sheet1.setColumnGroupCollapsed(4, false);

    FileOutputStream fileOut = new FileOutputStream(filename);
    wb.write(fileOut);
    fileOut.close();
}

From source file:demo.poi.Outlines.java

License:Apache License

private static void createCase5(String filename) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet("new sheet");

    sheet1.groupColumn(2, 10);/* w  w w .j  a va  2 s  .  c o m*/
    sheet1.groupColumn(4, 7);
    sheet1.setColumnGroupCollapsed(4, true);
    sheet1.setColumnGroupCollapsed(2, true);

    sheet1.setColumnGroupCollapsed(4, false);
    sheet1.setColumnGroupCollapsed(3, false);

    FileOutputStream fileOut = new FileOutputStream(filename);
    wb.write(fileOut);
    fileOut.close();
}

From source file:demo.poi.Outlines.java

License:Apache License

private static void createCase6(String filename) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet("new sheet");

    sheet1.groupColumn(2, 10);//w  w w.  j a v a  2 s  .c  o  m
    sheet1.groupColumn(4, 10);
    sheet1.setColumnGroupCollapsed(4, true);
    sheet1.setColumnGroupCollapsed(2, true);

    sheet1.setColumnGroupCollapsed(3, false);

    FileOutputStream fileOut = new FileOutputStream(filename);
    wb.write(fileOut);
    fileOut.close();
}

From source file:demo.poi.Outlines.java

License:Apache License

private static void createCase7(String filename) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet("new sheet");

    sheet1.groupRow(5, 14);// w ww.  ja  v a  2s.c om
    sheet1.groupRow(7, 10);

    FileOutputStream fileOut = new FileOutputStream(filename);
    wb.write(fileOut);
    fileOut.close();
}