use of org.apache.poi.xssf.usermodel.XSSFCell in project poi by apache.
the class XSSFExcelExtractor method handleNonStringCell.
private void handleNonStringCell(StringBuffer text, Cell cell, DataFormatter formatter) {
CellType type = cell.getCellTypeEnum();
if (type == CellType.FORMULA) {
type = cell.getCachedFormulaResultTypeEnum();
}
if (type == CellType.NUMERIC) {
CellStyle cs = cell.getCellStyle();
if (cs != null && cs.getDataFormatString() != null) {
String contents = formatter.formatRawCellContents(cell.getNumericCellValue(), cs.getDataFormat(), cs.getDataFormatString());
checkMaxTextSize(text, contents);
text.append(contents);
return;
}
}
// No supported styling applies to this cell
String contents = ((XSSFCell) cell).getRawValue();
if (contents != null) {
checkMaxTextSize(text, contents);
text.append(contents);
}
}
use of org.apache.poi.xssf.usermodel.XSSFCell in project poi by apache.
the class AligningCells method centerAcrossSelection.
/**
* Center a text over multiple columns using ALIGN_CENTER_SELECTION
*
* @param wb the workbook
* @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.
*/
private static void centerAcrossSelection(XSSFWorkbook wb, XSSFRow row, int start_column, int end_column, VerticalAlignment valign) {
CreationHelper ch = wb.getCreationHelper();
// Create cell style with ALIGN_CENTER_SELECTION
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.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(ch.createRichTextString("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);
}
use of org.apache.poi.xssf.usermodel.XSSFCell in project poi by apache.
the class AligningCells method createCell.
/**
* Creates a cell and aligns it a certain way.
*
* @param wb the workbook
* @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, int column, HorizontalAlignment halign, VerticalAlignment valign) {
CreationHelper ch = wb.getCreationHelper();
XSSFCell cell = row.createCell(column);
cell.setCellValue(ch.createRichTextString("Align It"));
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(halign);
cellStyle.setVerticalAlignment(valign);
cell.setCellStyle(cellStyle);
}
use of org.apache.poi.xssf.usermodel.XSSFCell in project poi by apache.
the class CreateTable method main.
public static void main(String[] args) throws IOException {
Workbook wb = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet) wb.createSheet();
//Create
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("ooxml-table.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();
}
use of org.apache.poi.xssf.usermodel.XSSFCell in project poi by apache.
the class XSSFSingleXmlCell method getReferencedCell.
/**
* Gets the XSSFCell referenced by the R attribute or creates a new one if cell doesn't exists
* @return the referenced XSSFCell, null if the cell reference is invalid
*/
public XSSFCell getReferencedCell() {
XSSFCell cell = null;
CellReference cellReference = new CellReference(singleXmlCell.getR());
XSSFRow row = parent.getXSSFSheet().getRow(cellReference.getRow());
if (row == null) {
row = parent.getXSSFSheet().createRow(cellReference.getRow());
}
cell = row.getCell(cellReference.getCol());
if (cell == null) {
cell = row.createCell(cellReference.getCol());
}
return cell;
}
Aggregations