Example usage for org.apache.poi.xssf.usermodel XSSFRow createCell

List of usage examples for org.apache.poi.xssf.usermodel XSSFRow createCell

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFRow createCell.

Prototype

@Override
public XSSFCell createCell(int columnIndex) 

Source Link

Document

Use this to create new cells within the row and return it.

Usage

From source file:org.tdl.vireo.model.jpa.JpaSubmissionImpl.java

private Attachment generateActionLogAttachment() {
    // create an Excel Workbook to store the action log as an attachment
    Attachment actionLogAttachment = null;
    String sheetName = "ActionLog";
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet = wb.createSheet(sheetName);
    XSSFRow header = sheet.createRow(0);
    int rowNum = 1;
    XSSFRow row = sheet.createRow(rowNum);
    int colNum = 0;
    for (ActionLog actionLog : actionLogs) {
        if (rowNum == 1) {
            header.createCell(colNum).setCellValue("Action Date");
        }/*w w  w . ja va 2 s  .  c o m*/
        row.createCell(colNum).setCellValue(actionLog.getActionDate());
        colNum++;

        if (rowNum == 1) {
            header.createCell(colNum).setCellValue("Attachment Type");
        }
        if (actionLog.getAttachment() != null) {
            row.createCell(colNum).setCellValue(actionLog.getAttachment().getType().name());
        }
        colNum++;

        if (rowNum == 1) {
            header.createCell(colNum).setCellValue("Attachment Date");
        }
        if (actionLog.getAttachment() != null) {
            row.createCell(colNum).setCellValue(actionLog.getAttachment().getDate());
        }
        colNum++;

        if (rowNum == 1) {
            header.createCell(colNum).setCellValue("Attachment Name");
        }
        if (actionLog.getAttachment() != null) {
            row.createCell(colNum).setCellValue(actionLog.getAttachment().getName());
        }
        colNum++;

        if (rowNum == 1) {
            header.createCell(colNum).setCellValue("Attachment Size");
        }
        if (actionLog.getAttachment() != null) {
            row.createCell(colNum).setCellValue(actionLog.getAttachment().getSize());
        }
        colNum++;

        if (rowNum == 1) {
            header.createCell(colNum).setCellValue("Action Entry");
        }
        row.createCell(colNum).setCellValue(actionLog.getEntry());
        colNum++;

        if (rowNum == 1) {
            header.createCell(colNum).setCellValue("Submission State");
        }
        row.createCell(colNum).setCellValue(actionLog.getSubmissionState().getBeanName());
        colNum++;

        rowNum++;
        row = sheet.createRow(rowNum);
        colNum = 0;
    }
    try {
        File actionLogFile = File.createTempFile("actionlog-", ".xlsx");
        actionLogFile.deleteOnExit();
        FileOutputStream actionLogFileOS = new FileOutputStream(actionLogFile);
        wb.write(actionLogFileOS);
        actionLogFileOS.flush();
        actionLogFileOS.close();
        actionLogAttachment = new ActionLogAttachment(this, actionLogFile);
    } catch (IOException e) {
        play.Logger.error("Error while generating Action Log Attachment! [%s]", e);
    }
    return actionLogAttachment;
}

From source file:org.tsukuba_bunko.lilac.helper.port.impl.ExportDataHelperBase.java

License:Open Source License

/**
 * /*w  w w  .j av  a  2 s  .c om*/
 */
protected XSSFCell createHeaderCell(XSSFRow row, int index, String label) {
    XSSFCell cell = row.createCell(index);
    cell.setCellStyle(headerCellStyle);
    setCellValue(cell, label);
    return cell;
}

From source file:org.tsukuba_bunko.lilac.helper.port.impl.ExportDataHelperBase.java

License:Open Source License

/**
 * ????//from  w  w w .j  av a 2s .  c  o m
 * @param row 
 * @param index 
 * @param style
 * @return 
 */
protected XSSFCell createCell(XSSFRow row, int index, XSSFCellStyle style) {
    XSSFCell cell = row.createCell(index);
    if (style != null) {
        cell.setCellStyle(style);
    }
    return cell;
}

From source file:packtest.AligningCells.java

License:Apache License

/**
 * Creates a cell and aligns it a certain way.
 *
 * @param wb     the workbook/*w  w w  . ja va2 s  . com*/
 * @param row    the row to create the cell in
 * @param column the column number to create the cell in
 * @param halign the horizontal alignment for the cell.
 */
private static void createCell(XSSFWorkbook wb, XSSFRow row, short column, short halign, short valign) {
    XSSFCell cell = row.createCell(column);
    cell.setCellValue(new XSSFRichTextString("Align It"));
    CellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment(halign);
    cellStyle.setVerticalAlignment(valign);
    cell.setCellStyle(cellStyle);
}

From source file:packtest.AligningCells.java

License:Apache License

/**
 * Center a text over multiple columns using ALIGN_CENTER_SELECTION
 *
 * @param wb the workbook//ww  w. ja v  a2 s .  c  o m
 * @param row the row to create the cell in
 * @param start_column  the column number to create the cell in and where the selection starts
 * @param end_column    the column number where the selection ends
 * @param valign the horizontal alignment for the cell.
 *
 * @author Cristian Petrula, Romania
 */
private static void centerAcrossSelection(XSSFWorkbook wb, XSSFRow row, short start_column, short end_column,
        short valign) {

    // Create cell style with ALIGN_CENTER_SELECTION
    XSSFCellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER_SELECTION);
    cellStyle.setVerticalAlignment(valign);

    // Create cells over the selected area
    for (int i = start_column; i <= end_column; i++) {
        XSSFCell cell = row.createCell(i);
        cell.setCellStyle(cellStyle);
    }

    // Set value to the first cell
    XSSFCell cell = row.getCell(start_column);
    cell.setCellValue(new XSSFRichTextString("Align It"));

    // Make the selection
    CTRowImpl ctRow = (CTRowImpl) row.getCTRow();

    // Add object with format start_coll:end_coll. For example 1:3 will span from
    // cell 1 to cell 3, where the column index starts with 0
    //
    // You can add multiple spans for one row
    Object span = start_column + ":" + end_column;

    List<Object> spanList = new ArrayList<Object>();
    spanList.add(span);

    //add spns to the row
    ctRow.setSpans(spanList);
}

From source file:packtest.CalendarDemo.java

License:Apache License

public static void main(String[] args) throws Exception {

    Calendar calendar = Calendar.getInstance();
    if (args.length > 0)
        calendar.set(Calendar.YEAR, Integer.parseInt(args[0]));

    int year = calendar.get(Calendar.YEAR);

    XSSFWorkbook wb = new XSSFWorkbook();
    Map<String, XSSFCellStyle> 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
        XSSFSheet sheet = wb.createSheet(months[month]);

        //turn off gridlines
        sheet.setDisplayGridlines(false);
        sheet.setPrintGridlines(false);/*from w  w  w  . j  a v a  2s  .c o  m*/
        XSSFPrintSetup printSetup = sheet.getPrintSetup();
        printSetup.setOrientation(PrintOrientation.LANDSCAPE);
        sheet.setFitToPage(true);
        sheet.setHorizontallyCenter(true);

        //the header row: centered text in 48pt font
        XSSFRow headerRow = sheet.createRow(0);
        headerRow.setHeightInPoints(80);
        XSSFCell 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
        XSSFRow monthRow = sheet.createRow(1);
        for (int i = 0; i < days.length; i++) {
            //for compatibility with HSSF we have to set column width in units of 1/256th of a character width
            sheet.setColumnWidth(i * 2, 5 * 256); //the column is 5 characters wide
            sheet.setColumnWidth(i * 2 + 1, 13 * 256); //the column is 13 characters wide
            sheet.addMergedRegion(new CellRangeAddress(1, 1, i * 2, i * 2 + 1));
            XSSFCell 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++) {
            XSSFRow row = sheet.createRow(rownum++);
            row.setHeightInPoints(100);
            for (int i = 0; i < days.length; i++) {
                XSSFCell dayCell_1 = row.createCell(i * 2);
                XSSFCell 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
    FileOutputStream out = new FileOutputStream("calendar-" + year + ".xlsx");
    wb.write(out);
    out.close();

    wb.close();
}

From source file:packtest.CreateTable.java

License:Apache License

public static void main(String[] args) throws FileNotFoundException, IOException {

    Workbook wb = new XSSFWorkbook();
    XSSFSheet sheet = (XSSFSheet) wb.createSheet();

    //Create /*  w  ww.  j  a v a  2  s  . c om*/
    XSSFTable table = sheet.createTable();
    table.setDisplayName("Test");
    CTTable cttable = table.getCTTable();

    //Style configurations
    CTTableStyleInfo style = cttable.addNewTableStyleInfo();
    style.setName("TableStyleMedium2");
    style.setShowColumnStripes(false);
    style.setShowRowStripes(true);

    //Set which area the table should be placed in
    AreaReference reference = new AreaReference(new CellReference(0, 0), new CellReference(2, 2));
    cttable.setRef(reference.formatAsString());
    cttable.setId(1);
    cttable.setName("Test");
    cttable.setTotalsRowCount(1);

    CTTableColumns columns = cttable.addNewTableColumns();
    columns.setCount(3);
    CTTableColumn column;
    XSSFRow row;
    XSSFCell cell;
    for (int i = 0; i < 3; i++) {
        //Create column
        column = columns.addNewTableColumn();
        column.setName("Column");
        column.setId(i + 1);
        //Create row
        row = sheet.createRow(i);
        for (int j = 0; j < 3; j++) {
            //Create cell
            cell = row.createCell(j);
            if (i == 0) {
                cell.setCellValue("Column" + j);
            } else {
                cell.setCellValue("0");
            }
        }
    }

    FileOutputStream fileOut = new FileOutputStream(Utils.getPath("ooxml-table.xlsx"));
    wb.write(fileOut);
    fileOut.close();
}

From source file:packtest.WorkingWithRichText.java

License:Apache License

public static void main(String[] args) throws Exception {

    XSSFWorkbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
    try {//ww  w . j  a  v a 2s  . c  om
        XSSFSheet sheet = wb.createSheet();
        XSSFRow row = sheet.createRow((short) 2);

        XSSFCell cell = row.createCell(1);
        XSSFRichTextString rt = new XSSFRichTextString("The quick brown fox");

        XSSFFont font1 = wb.createFont();
        font1.setBold(true);
        font1.setColor(new XSSFColor(new java.awt.Color(255, 0, 0)));
        rt.applyFont(0, 10, font1);

        XSSFFont font2 = wb.createFont();
        font2.setItalic(true);
        font2.setUnderline(XSSFFont.U_DOUBLE);
        font2.setColor(new XSSFColor(new java.awt.Color(0, 255, 0)));
        rt.applyFont(10, 19, font2);

        XSSFFont font3 = wb.createFont();
        font3.setColor(new XSSFColor(new java.awt.Color(0, 0, 255)));
        rt.append(" Jumped over the lazy dog", font3);

        cell.setCellValue(rt);

        // Write the output to a file
        OutputStream fileOut = new FileOutputStream(Utils.getPath("xssf-richtext.xlsx"));
        try {
            wb.write(fileOut);
        } finally {
            fileOut.close();
        }
    } finally {
        wb.close();
    }
}

From source file:poi.xslf.usermodel.PieChartDemo.java

License:Apache License

public static void main(String[] args) throws Exception {
    if (args.length < 2) {
        usage();/*from ww  w .j av  a 2 s. c om*/
        return;
    }

    BufferedReader modelReader = new BufferedReader(new FileReader(args[1]));

    String chartTitle = modelReader.readLine(); // first line is chart title

    XMLSlideShow pptx = new XMLSlideShow(new FileInputStream(args[0]));
    XSLFSlide slide = pptx.getSlides()[0];

    // find chart in the slide
    XSLFChart chart = null;
    for (POIXMLDocumentPart part : slide.getRelations()) {
        if (part instanceof XSLFChart) {
            chart = (XSLFChart) part;
            break;
        }
    }

    if (chart == null)
        throw new IllegalStateException("chart not found in the template");

    // embedded Excel workbook that holds the chart data
    POIXMLDocumentPart xlsPart = chart.getRelations().get(0);
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet = wb.createSheet();

    CTChart ctChart = chart.getCTChart();
    CTPlotArea plotArea = ctChart.getPlotArea();

    CTPieChart pieChart = plotArea.getPieChartArray(0);
    //Pie Chart Series
    CTPieSer ser = pieChart.getSerArray(0);

    // Series Text
    CTSerTx tx = ser.getTx();
    tx.getStrRef().getStrCache().getPtArray(0).setV(chartTitle);
    sheet.createRow(0).createCell(1).setCellValue(chartTitle);
    String titleRef = new CellReference(sheet.getSheetName(), 0, 1, true, true).formatAsString();
    tx.getStrRef().setF(titleRef);

    // Category Axis Data
    CTAxDataSource cat = ser.getCat();
    CTStrData strData = cat.getStrRef().getStrCache();

    // Values
    CTNumDataSource val = ser.getVal();
    CTNumData numData = val.getNumRef().getNumCache();

    strData.setPtArray(null); // unset old axis text
    numData.setPtArray(null); // unset old values

    // set model
    int idx = 0;
    int rownum = 1;
    String ln;
    while ((ln = modelReader.readLine()) != null) {
        String[] vals = ln.split("\\s+");
        CTNumVal numVal = numData.addNewPt();
        numVal.setIdx(idx);
        numVal.setV(vals[1]);

        CTStrVal sVal = strData.addNewPt();
        sVal.setIdx(idx);
        sVal.setV(vals[0]);

        idx++;
        XSSFRow row = sheet.createRow(rownum++);
        row.createCell(0).setCellValue(vals[0]);
        row.createCell(1).setCellValue(Double.valueOf(vals[1]));
    }
    numData.getPtCount().setVal(idx);
    strData.getPtCount().setVal(idx);

    String numDataRange = new CellRangeAddress(1, rownum - 1, 1, 1).formatAsString(sheet.getSheetName(), true);
    val.getNumRef().setF(numDataRange);
    String axisDataRange = new CellRangeAddress(1, rownum - 1, 0, 0).formatAsString(sheet.getSheetName(), true);
    cat.getStrRef().setF(axisDataRange);

    // updated the embedded workbook with the data
    OutputStream xlsOut = xlsPart.getPackagePart().getOutputStream();
    wb.write(xlsOut);
    xlsOut.close();

    // save the result
    FileOutputStream out = new FileOutputStream("pie-chart-demo-output.pptx");
    pptx.write(out);
    out.close();
}

From source file:poi.xssf.usermodel.examples.AligningCells.java

License:Apache License

/**
 * Center a text over multiple columns using ALIGN_CENTER_SELECTION
 *
 * @param wb the workbook/*from  w ww .  j  a va  2 s . co  m*/
 * @param row the row to create the cell in
 * @param start_column  the column number to create the cell in and where the selection starts
 * @param end_column    the column number where the selection ends
 * @param valign the horizontal alignment for the cell.
 *
 * @author Cristian Petrula, Romania
 */
private static void centerAcrossSelection(XSSFWorkbook wb, XSSFRow row, short start_column, short end_column,
        short valign) {

    // Create cell style with ALIGN_CENTER_SELECTION
    XSSFCellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER_SELECTION);
    cellStyle.setVerticalAlignment(valign);

    // Create cells over the selected area
    for (int i = start_column; i <= end_column; i++) {
        XSSFCell cell = row.createCell(i);
        cell.setCellStyle(cellStyle);
    }

    // Set value to the first cell
    XSSFCell cell = row.getCell(start_column);
    cell.setCellValue(new XSSFRichTextString("Align It"));

    // Make the selection
    CTRowImpl ctRow = (CTRowImpl) row.getCTRow();
    List spanList = new ArrayList();

    // Add object with format start_coll:end_coll. For example 1:3 will span from
    // cell 1 to cell 3, where the column index starts with 0
    //
    // You can add multiple spans for one row
    Object span = start_column + ":" + end_column;
    spanList.add(span);

    //add spns to the row
    ctRow.setSpans(spanList);
}