Search in sources :

Example 31 with HSSFRow

use of org.apache.poi.hssf.usermodel.HSSFRow in project ddf by codice.

the class RrdMetricsRetrieverTest method verifyWorksheet.

private void verifyWorksheet(HSSFSheet sheet, String metricName, int expectedNumberOfDataRows, boolean hasTotalCount) {
    // 3 = title + blank row + column headers
    int expectedTotalRows = 3 + expectedNumberOfDataRows;
    if (hasTotalCount) {
        expectedTotalRows += 2;
    }
    assertThat(sheet.getPhysicalNumberOfRows(), equalTo(expectedTotalRows));
    // first row should have title in first cell
    HSSFRow row = sheet.getRow(0);
    assertThat(row, not(nullValue()));
    assertThat(row.getCell(0).getStringCellValue(), startsWith(metricName + " for"));
    // third row should have column headers in first and second cells
    row = sheet.getRow(2);
    assertThat(row.getCell(0).getStringCellValue(), equalTo("Timestamp"));
    assertThat(row.getCell(1).getStringCellValue(), equalTo("Value"));
    // verify rows with the sample data, i.e., timestamps and values
    int endRow = 3 + expectedNumberOfDataRows;
    for (int i = 3; i < endRow; i++) {
        row = sheet.getRow(i);
        assertThat(row.getCell(0).getStringCellValue(), not(nullValue()));
        assertThat(row.getCell(1).getNumericCellValue(), not(nullValue()));
    }
    row = sheet.getRow(sheet.getLastRowNum());
    if (hasTotalCount) {
        assertThat(row.getCell(0).getStringCellValue(), startsWith("Total Count:"));
        assertThat(row.getCell(1).getNumericCellValue(), not(nullValue()));
    } else {
        assertThat(row.getCell(0).getStringCellValue(), not(startsWith("Total Count:")));
    }
}
Also used : HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow)

Example 32 with HSSFRow

use of org.apache.poi.hssf.usermodel.HSSFRow in project ddf by codice.

the class MetricsEndpointTest method testGetMetricsDataAsXls.

@Test
public void testGetMetricsDataAsXls() throws Exception {
    // Create RRD file that Metrics Endpoint will detect
    // 15 minutes in seconds
    int dateOffset = 900;
    createRrdFile(dateOffset);
    UriInfo uriInfo = createUriInfo();
    // Get the metrics data from the endpoint
    MetricsEndpoint endpoint = getEndpoint();
    endpoint.setMetricsDir(TEST_DIR);
    Response response = endpoint.getMetricsData("uptime", "xls", null, null, Integer.toString(dateOffset), "my label", "my title", uriInfo);
    cleanupRrd();
    InputStream xls = (InputStream) response.getEntity();
    assertThat(xls, not(nullValue()));
    HSSFWorkbook wb = new HSSFWorkbook(xls);
    assertThat(wb.getNumberOfSheets(), equalTo(1));
    HSSFSheet sheet = wb.getSheet("Uptime");
    assertThat(sheet, not(nullValue()));
    // Expect 7 rows: title + blank + column headers + 2 rows of samples + blank +
    // totalQueryCount
    assertThat(sheet.getPhysicalNumberOfRows(), equalTo(7));
    // first row should have title in first cell
    HSSFRow row = sheet.getRow(0);
    assertThat(row, not(nullValue()));
    assertThat(row.getCell(0).getStringCellValue(), startsWith("Uptime for"));
    // third row should have column headers in first and second cells
    row = sheet.getRow(2);
    assertThat(row.getCell(0).getStringCellValue(), equalTo("Timestamp"));
    assertThat(row.getCell(1).getStringCellValue(), equalTo("Value"));
    // should have 2 rows of samples' data
    row = sheet.getRow(3);
    assertThat(row.getCell(0).getStringCellValue(), not(nullValue()));
    assertThat(row.getCell(1).getNumericCellValue(), not(nullValue()));
    row = sheet.getRow(4);
    assertThat(row.getCell(0).getStringCellValue(), not(nullValue()));
    assertThat(row.getCell(1).getNumericCellValue(), not(nullValue()));
    // last row should have totalQueryCount in first cell
    row = sheet.getRow(sheet.getLastRowNum());
    assertThat(row.getCell(0).getStringCellValue(), startsWith("Total Count:"));
    assertThat(row.getCell(1).getNumericCellValue(), not(nullValue()));
}
Also used : Response(javax.ws.rs.core.Response) InputStream(java.io.InputStream) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) UriInfo(javax.ws.rs.core.UriInfo) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 33 with HSSFRow

use of org.apache.poi.hssf.usermodel.HSSFRow in project processdash by dtuma.

the class WBSExcelWriter method writeDataForNodes.

private int writeDataForNodes(HSSFSheet sheet, int rowNum, WBSNode node, TableColumnModel columns) {
    HSSFRow row = sheet.createRow(rowNum);
    writeCellForNodeName(node, row);
    writeCellsForNodeData(row, node, columns);
    WBSNode[] children = wbs.getChildren(node);
    if (children.length == 0)
        return rowNum;
    int childRowPos = rowNum;
    for (WBSNode child : children) {
        if (!child.isHidden())
            childRowPos = writeDataForNodes(sheet, childRowPos + 1, child, columns);
    }
    sheet.groupRow(rowNum + 1, childRowPos);
    if (!node.isExpanded())
        sheet.setRowGroupCollapsed(rowNum + 1, true);
    return childRowPos;
}
Also used : HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) WBSNode(teamdash.wbs.WBSNode)

Example 34 with HSSFRow

use of org.apache.poi.hssf.usermodel.HSSFRow in project processdash by dtuma.

the class WBSExcelWriter method createHeaderRow.

private void createHeaderRow(HSSFSheet sheet, TableColumnModel columns) {
    HSSFRow row = sheet.createRow(0);
    StyleKey style = new StyleKey();
    style.bold = true;
    for (int i = 0; i < columns.getColumnCount(); i++) {
        TableColumn col = columns.getColumn(i);
        String columnName = data.getColumnName(col.getModelIndex());
        HSSFCell cell = row.createCell(s(i + 1));
        cell.setCellValue(new HSSFRichTextString(columnName));
        styleCache.applyStyle(cell, style);
    }
}
Also used : HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) TableColumn(javax.swing.table.TableColumn)

Example 35 with HSSFRow

use of org.apache.poi.hssf.usermodel.HSSFRow in project portal by ixinportal.

the class ExcelFileGenerator method createWorkbook.

/**
 * 创建HSSFWorkbook对象
 *
 * @return HSSFWorkbook
 */
public HSSFWorkbook createWorkbook() {
    // 创建workbook对象
    workBook = new HSSFWorkbook();
    int rows = fieldData.size();
    int sheetNum = 1;
    for (int i = 1; i <= sheetNum; i++) {
        // 使用wookbook对象创建sheet对象
        HSSFSheet sheet = workBook.createSheet("Page " + i);
        // 使用HSSFSheet对象创建row,row的下标从0开始
        HSSFRow headRow = sheet.createRow(0);
        for (int j = 0; j < fieldName.size(); j++) {
            // 循环excel的标题
            // 使用HSSFRow创建cell,cell的下标从0开始
            HSSFCell cell = headRow.createCell(j);
            // 添加样式
            // 设置每一列的宽度
            sheet.setColumnWidth(j, 6000);
            // 创建样式
            HSSFCellStyle cellStyle = workBook.createCellStyle();
            // 设置字体
            // 创建字体对象
            HSSFFont font = workBook.createFont();
            // 将字体变为粗体
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            // 将字体颜色变红色
            short color = HSSFColor.RED.index;
            font.setColor(color);
            // 设置之后的字体
            cellStyle.setFont(font);
            // 添加样式
            // 设置单元格的类型
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            if (fieldName.get(j) != null) {
                cell.setCellStyle(cellStyle);
                // 赋值
                cell.setCellValue((String) fieldName.get(j));
            } else {
                cell.setCellStyle(cellStyle);
                cell.setCellValue("-");
            }
        }
        for (int k = 0; k < rows; k++) {
            // 分页显示数据
            // 使用HSSFSheet对象创建row,row的下标从0开始
            HSSFRow row = sheet.createRow((k + 1));
            // 将数据内容放入excel单元格
            // 循环数据集
            ArrayList rowList = (ArrayList) fieldData.get((i - 1) + k);
            for (int n = 0; n < rowList.size(); n++) {
                // 使用HSSFRow创建cell,cell的下标从0开始
                HSSFCell cell = row.createCell(n);
                if (rowList.get(n) != null) {
                    cell.setCellValue((String) rowList.get(n).toString());
                } else {
                    cell.setCellValue("");
                }
            }
        }
    }
    return workBook;
}
Also used : HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) ArrayList(java.util.ArrayList) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFFont(org.apache.poi.hssf.usermodel.HSSFFont) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Aggregations

HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)124 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)98 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)82 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)71 HSSFCellStyle (org.apache.poi.hssf.usermodel.HSSFCellStyle)29 FileOutputStream (java.io.FileOutputStream)24 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)24 Test (org.junit.Test)18 IOException (java.io.IOException)16 HSSFFormulaEvaluator (org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator)15 HSSFFont (org.apache.poi.hssf.usermodel.HSSFFont)14 File (java.io.File)13 ArrayList (java.util.ArrayList)12 CellValue (org.apache.poi.ss.usermodel.CellValue)10 OutputStream (java.io.OutputStream)8 HashMap (java.util.HashMap)8 Map (java.util.Map)7 AssertionFailedError (junit.framework.AssertionFailedError)7 FileInputStream (java.io.FileInputStream)6 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)6