Search in sources :

Example 71 with HSSFSheet

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

the class RrdMetricsRetrieverTest method testMetricsXlsReportSummary.

@Test
public void testMetricsXlsReportSummary() throws Exception {
    String metricName = "queryCount_Gauge";
    long endTime = new DateTime(DateTimeZone.UTC).getMillis();
    List<String> metricNames = new ArrayList<String>();
    metricNames.add(metricName);
    for (RrdMetricsRetriever.SUMMARY_INTERVALS interval : RrdMetricsRetriever.SUMMARY_INTERVALS.values()) {
        long startTime = 0L;
        switch(interval) {
            case minute:
                startTime = new DateTime(DateTimeZone.UTC).minusHours(1).getMillis();
                break;
            case hour:
                startTime = new DateTime(DateTimeZone.UTC).minusDays(1).getMillis();
                break;
            case day:
                startTime = new DateTime(DateTimeZone.UTC).minusWeeks(1).getMillis();
                break;
            case week:
                startTime = new DateTime(DateTimeZone.UTC).minusMonths(1).getMillis();
                break;
            case month:
                startTime = new DateTime(DateTimeZone.UTC).minusYears(1).getMillis();
                break;
        }
        int sampleSize = (int) ((endTime - startTime) / (RRD_STEP * 1000));
        new RrdFileBuilder().rrdFileName(TEST_DIR + metricName + RRD_FILE_EXTENSION).dsType(DsType.GAUGE).numSamples(sampleSize).numRows(sampleSize).startTime(startTime).build();
        MetricsRetriever metricsRetriever = new RrdMetricsRetriever();
        OutputStream os = metricsRetriever.createXlsReport(metricNames, TEST_DIR, startTime, endTime, interval.toString());
        InputStream xls = new ByteArrayInputStream(((ByteArrayOutputStream) os).toByteArray());
        assertThat(xls, not(nullValue()));
        HSSFWorkbook wb = new HSSFWorkbook(xls);
        assertThat(wb.getNumberOfSheets(), equalTo(1));
        HSSFSheet sheet = wb.getSheetAt(0);
        assertThat(sheet, not(nullValue()));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) DateTime(org.joda.time.DateTime) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) ByteArrayInputStream(java.io.ByteArrayInputStream) MetricsRetriever(ddf.metrics.reporting.internal.MetricsRetriever) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) Test(org.junit.Test)

Example 72 with HSSFSheet

use of org.apache.poi.hssf.usermodel.HSSFSheet 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 73 with HSSFSheet

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

the class MetricsEndpointTest method testGetMetricsReportAsXls.

@Test
public void testGetMetricsReportAsXls() throws Exception {
    // Create RRD file that Metrics Endpoint will detect
    // 15 minutes in seconds
    int dateOffset = 900;
    createRrdFile(dateOffset, "uptime");
    UriInfo uriInfo = createUriInfo();
    // Get the metrics data from the endpoint
    MetricsEndpoint endpoint = getEndpoint();
    endpoint.setMetricsDir(TEST_DIR);
    Response response = endpoint.getMetricsReport("xls", null, null, Integer.toString(dateOffset), "minute", uriInfo);
    cleanupRrd();
    MultivaluedMap<String, Object> headers = response.getHeaders();
    assertTrue(headers.getFirst("Content-Disposition").toString().contains("attachment; filename="));
    InputStream is = (InputStream) response.getEntity();
    assertThat(is, not(nullValue()));
    HSSFWorkbook wb = new HSSFWorkbook(is);
    assertThat(wb.getNumberOfSheets(), equalTo(1));
    HSSFSheet sheet = wb.getSheetAt(0);
    assertThat(sheet, not(nullValue()));
}
Also used : Response(javax.ws.rs.core.Response) InputStream(java.io.InputStream) JSONObject(org.json.simple.JSONObject) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) Matchers.anyString(org.mockito.Matchers.anyString) UriInfo(javax.ws.rs.core.UriInfo) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 74 with HSSFSheet

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

the class WBSExcelWriter method addData.

public void addData(String tabName, TableColumnModel columns) {
    String safeTabName = cleanupTabName(tabName);
    HSSFSheet sheet = xls.createSheet(safeTabName);
    // WORKAROUND, for POI bug
    // ------ http://issues.apache.org/bugzilla/show_bug.cgi?id=30714
    // We should have been able to use the next line...
    // sheet.setRowSumsBelow(false);
    sheet.setAlternativeExpression(false);
    createHeaderRow(sheet, columns);
    writeDataForNodes(sheet, 1, wbs.getRoot(), columns);
    autoSizeColumns(sheet, columns);
    sheet.createFreezePane(1, 1);
}
Also used : HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString)

Example 75 with HSSFSheet

use of org.apache.poi.hssf.usermodel.HSSFSheet 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

HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)213 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)172 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)97 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)84 Test (org.junit.Test)67 FileOutputStream (java.io.FileOutputStream)34 HSSFFormulaEvaluator (org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator)27 IOException (java.io.IOException)25 HSSFCellStyle (org.apache.poi.hssf.usermodel.HSSFCellStyle)25 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)25 File (java.io.File)21 ArrayList (java.util.ArrayList)20 HSSFPatriarch (org.apache.poi.hssf.usermodel.HSSFPatriarch)20 Row (org.apache.poi.ss.usermodel.Row)18 FileInputStream (java.io.FileInputStream)17 HSSFClientAnchor (org.apache.poi.hssf.usermodel.HSSFClientAnchor)15 OutputStream (java.io.OutputStream)14 HashMap (java.util.HashMap)14 EscherAggregate (org.apache.poi.hssf.record.EscherAggregate)13 HSSFFont (org.apache.poi.hssf.usermodel.HSSFFont)12