Search in sources :

Example 21 with CellRangeAddress

use of org.apache.poi.ss.util.CellRangeAddress in project poi by apache.

the class CalendarDemo method main.

public static void main(String[] args) throws Exception {
    Calendar calendar = Calendar.getInstance();
    boolean xlsx = true;
    for (int i = 0; i < args.length; i++) {
        if (args[i].charAt(0) == '-') {
            xlsx = args[i].equals("-xlsx");
        } else {
            calendar.set(Calendar.YEAR, Integer.parseInt(args[i]));
        }
    }
    int year = calendar.get(Calendar.YEAR);
    Workbook wb = xlsx ? new XSSFWorkbook() : new HSSFWorkbook();
    Map<String, CellStyle> styles = createStyles(wb);
    for (int month = 0; month < 12; month++) {
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        //create a sheet for each month
        Sheet sheet = wb.createSheet(months[month]);
        //turn off gridlines
        sheet.setDisplayGridlines(false);
        sheet.setPrintGridlines(false);
        sheet.setFitToPage(true);
        sheet.setHorizontallyCenter(true);
        PrintSetup printSetup = sheet.getPrintSetup();
        printSetup.setLandscape(true);
        //the following three statements are required only for HSSF
        sheet.setAutobreaks(true);
        printSetup.setFitHeight((short) 1);
        printSetup.setFitWidth((short) 1);
        //the header row: centered text in 48pt font
        Row headerRow = sheet.createRow(0);
        headerRow.setHeightInPoints(80);
        Cell titleCell = headerRow.createCell(0);
        titleCell.setCellValue(months[month] + " " + year);
        titleCell.setCellStyle(styles.get("title"));
        sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$N$1"));
        //header with month titles
        Row monthRow = sheet.createRow(1);
        for (int i = 0; i < days.length; i++) {
            //set column widths, the width is measured in units of 1/256th of a character width
            //the column is 5 characters wide
            sheet.setColumnWidth(i * 2, 5 * 256);
            //the column is 13 characters wide
            sheet.setColumnWidth(i * 2 + 1, 13 * 256);
            sheet.addMergedRegion(new CellRangeAddress(1, 1, i * 2, i * 2 + 1));
            Cell monthCell = monthRow.createCell(i * 2);
            monthCell.setCellValue(days[i]);
            monthCell.setCellStyle(styles.get("month"));
        }
        int cnt = 1, day = 1;
        int rownum = 2;
        for (int j = 0; j < 6; j++) {
            Row row = sheet.createRow(rownum++);
            row.setHeightInPoints(100);
            for (int i = 0; i < days.length; i++) {
                Cell dayCell_1 = row.createCell(i * 2);
                Cell dayCell_2 = row.createCell(i * 2 + 1);
                int day_of_week = calendar.get(Calendar.DAY_OF_WEEK);
                if (cnt >= day_of_week && calendar.get(Calendar.MONTH) == month) {
                    dayCell_1.setCellValue(day);
                    calendar.set(Calendar.DAY_OF_MONTH, ++day);
                    if (i == 0 || i == days.length - 1) {
                        dayCell_1.setCellStyle(styles.get("weekend_left"));
                        dayCell_2.setCellStyle(styles.get("weekend_right"));
                    } else {
                        dayCell_1.setCellStyle(styles.get("workday_left"));
                        dayCell_2.setCellStyle(styles.get("workday_right"));
                    }
                } else {
                    dayCell_1.setCellStyle(styles.get("grey_left"));
                    dayCell_2.setCellStyle(styles.get("grey_right"));
                }
                cnt++;
            }
            if (calendar.get(Calendar.MONTH) > month)
                break;
        }
    }
    // Write the output to a file
    String file = "calendar.xls";
    if (wb instanceof XSSFWorkbook)
        file += "x";
    FileOutputStream out = new FileOutputStream(file);
    wb.write(out);
    out.close();
    wb.close();
}
Also used : Calendar(java.util.Calendar) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) FileOutputStream(java.io.FileOutputStream) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress)

Example 22 with CellRangeAddress

use of org.apache.poi.ss.util.CellRangeAddress in project poi by apache.

the class LineChart method main.

public static void main(String[] args) throws IOException {
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet("linechart");
    final int NUM_OF_ROWS = 3;
    final int NUM_OF_COLUMNS = 10;
    // Create a row and put some cells in it. Rows are 0 based.
    Row row;
    Cell cell;
    for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {
        row = sheet.createRow((short) rowIndex);
        for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
            cell = row.createCell((short) colIndex);
            cell.setCellValue(colIndex * (rowIndex + 1));
        }
    }
    Drawing<?> drawing = sheet.createDrawingPatriarch();
    ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
    Chart chart = drawing.createChart(anchor);
    ChartLegend legend = chart.getOrCreateLegend();
    legend.setPosition(LegendPosition.TOP_RIGHT);
    LineChartData data = chart.getChartDataFactory().createLineChartData();
    // Use a category axis for the bottom axis.
    ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
    ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
    leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
    ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
    ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
    ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
    data.addSeries(xs, ys1);
    data.addSeries(xs, ys2);
    chart.plot(data, bottomAxis, leftAxis);
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("ooxml-line-chart.xlsx");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : ChartLegend(org.apache.poi.ss.usermodel.charts.ChartLegend) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) ChartAxis(org.apache.poi.ss.usermodel.charts.ChartAxis) ValueAxis(org.apache.poi.ss.usermodel.charts.ValueAxis) FileOutputStream(java.io.FileOutputStream) LineChartData(org.apache.poi.ss.usermodel.charts.LineChartData) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Chart(org.apache.poi.ss.usermodel.Chart)

Example 23 with CellRangeAddress

use of org.apache.poi.ss.util.CellRangeAddress in project poi by apache.

the class MergingCells method main.

public static void main(String[] args) throws IOException {
    //or new HSSFWorkbook();
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet("new sheet");
    Row row = sheet.createRow((short) 1);
    Cell cell = row.createCell((short) 1);
    cell.setCellValue(new XSSFRichTextString("This is a test of merging"));
    sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 2));
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("merging_cells.xlsx");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook)

Example 24 with CellRangeAddress

use of org.apache.poi.ss.util.CellRangeAddress in project poi by apache.

the class HSSFSheet method setRepeatingColumns.

@Override
public void setRepeatingColumns(CellRangeAddress columnRangeRef) {
    CellRangeAddress rowRangeRef = getRepeatingRows();
    setRepeatingRowsAndColumns(rowRangeRef, columnRangeRef);
}
Also used : CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress)

Example 25 with CellRangeAddress

use of org.apache.poi.ss.util.CellRangeAddress in project poi by apache.

the class HSSFSheetConditionalFormatting method addConditionalFormatting.

/**
     * Allows to add a new Conditional Formatting set to the sheet.
     *
     * @param regions - list of rectangular regions to apply conditional formatting rules
     * @param cfRules - set of up to three conditional formatting rules
     *
     * @return index of the newly created Conditional Formatting object
     */
public int addConditionalFormatting(CellRangeAddress[] regions, HSSFConditionalFormattingRule[] cfRules) {
    if (regions == null) {
        throw new IllegalArgumentException("regions must not be null");
    }
    for (CellRangeAddress range : regions) range.validate(SpreadsheetVersion.EXCEL97);
    if (cfRules == null) {
        throw new IllegalArgumentException("cfRules must not be null");
    }
    if (cfRules.length == 0) {
        throw new IllegalArgumentException("cfRules must not be empty");
    }
    if (cfRules.length > 3) {
        throw new IllegalArgumentException("Number of rules must not exceed 3");
    }
    CFRuleBase[] rules = new CFRuleBase[cfRules.length];
    for (int i = 0; i != cfRules.length; i++) {
        rules[i] = cfRules[i].getCfRuleRecord();
    }
    CFRecordsAggregate cfra = new CFRecordsAggregate(regions, rules);
    return _conditionalFormattingTable.add(cfra);
}
Also used : CFRuleBase(org.apache.poi.hssf.record.CFRuleBase) CFRecordsAggregate(org.apache.poi.hssf.record.aggregates.CFRecordsAggregate) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress)

Aggregations

CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)199 Test (org.junit.Test)74 Row (org.apache.poi.ss.usermodel.Row)35 Cell (org.apache.poi.ss.usermodel.Cell)34 Sheet (org.apache.poi.ss.usermodel.Sheet)23 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)21 ArrayList (java.util.ArrayList)20 Workbook (org.apache.poi.ss.usermodel.Workbook)19 HSSFConditionalFormattingRule (org.apache.poi.hssf.usermodel.HSSFConditionalFormattingRule)16 ConditionalFormattingRule (org.apache.poi.ss.usermodel.ConditionalFormattingRule)16 FileOutputStream (java.io.FileOutputStream)15 SheetConditionalFormatting (org.apache.poi.ss.usermodel.SheetConditionalFormatting)15 XSSFCellStyle (org.apache.poi.xssf.usermodel.XSSFCellStyle)14 XSSFFont (org.apache.poi.xssf.usermodel.XSSFFont)14 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)14 XSSFColor (org.apache.poi.xssf.usermodel.XSSFColor)13 HSSFConditionalFormatting (org.apache.poi.hssf.usermodel.HSSFConditionalFormatting)12 CellReference (org.apache.poi.ss.util.CellReference)12 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)8 Ptg (org.apache.poi.ss.formula.ptg.Ptg)8