List of usage examples for org.apache.poi.ss.usermodel Cell getCellStyle
CellStyle getCellStyle();
From source file:org.alanwilliamson.openbd.plugin.spreadsheet.SheetUtility.java
License:Open Source License
public static void cloneCell(Cell cNew, Cell cOld) { cNew.setCellComment(cOld.getCellComment()); cNew.setCellStyle(cOld.getCellStyle()); cNew.setCellType(cOld.getCellType()); switch (cNew.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: { cNew.setCellValue(cOld.getBooleanCellValue()); break;/*from ww w . j av a2 s .c om*/ } case Cell.CELL_TYPE_NUMERIC: { cNew.setCellValue(cOld.getNumericCellValue()); break; } case Cell.CELL_TYPE_STRING: { cNew.setCellValue(cOld.getStringCellValue()); break; } case Cell.CELL_TYPE_ERROR: { cNew.setCellValue(cOld.getErrorCellValue()); break; } case Cell.CELL_TYPE_FORMULA: { cNew.setCellFormula(cOld.getCellFormula()); break; } case Cell.CELL_TYPE_BLANK: { cNew.setCellValue(cOld.getNumericCellValue()); break; } } }
From source file:org.aludratest.util.ExcelUtil.java
License:Apache License
private static void moveCell(Row row, int fromIndex, int toIndex) { Cell oldCell = row.getCell(fromIndex); if (oldCell != null) { Cell newCell = row.createCell(toIndex, oldCell.getCellType()); newCell.setCellStyle(oldCell.getCellStyle()); newCell.setCellValue(oldCell.getStringCellValue()); row.removeCell(oldCell);/*from ww w. j ava 2 s . c om*/ } }
From source file:org.apache.metamodel.excel.ExcelUtils.java
License:Apache License
public static Style getCellStyle(Workbook workbook, Cell cell) { if (cell == null) { return Style.NO_STYLE; }//www .j a va 2 s. c o m final CellStyle cellStyle = cell.getCellStyle(); final short fontIndex = cellStyle.getFontIndex(); final Font font = workbook.getFontAt(fontIndex); final StyleBuilder styleBuilder = new StyleBuilder(); // Font bold, italic, underline if (font.getBoldweight() >= Font.BOLDWEIGHT_BOLD) { styleBuilder.bold(); } if (font.getItalic()) { styleBuilder.italic(); } if (font.getUnderline() != FontUnderline.NONE.getByteValue()) { styleBuilder.underline(); } // Font size final Font stdFont = workbook.getFontAt((short) 0); final short fontSize = font.getFontHeightInPoints(); if (stdFont.getFontHeightInPoints() != fontSize) { styleBuilder.fontSize(fontSize, SizeUnit.PT); } // Font color final short colorIndex = font.getColor(); if (font instanceof HSSFFont) { if (colorIndex != HSSFFont.COLOR_NORMAL) { final HSSFWorkbook wb = (HSSFWorkbook) workbook; HSSFColor color = wb.getCustomPalette().getColor(colorIndex); if (color != null) { short[] triplet = color.getTriplet(); styleBuilder.foreground(triplet); } } } else if (font instanceof XSSFFont) { XSSFFont xssfFont = (XSSFFont) font; XSSFColor color = xssfFont.getXSSFColor(); if (color != null) { String argbHex = color.getARGBHex(); if (argbHex != null) { styleBuilder.foreground(argbHex.substring(2)); } } } else { throw new IllegalStateException( "Unexpected font type: " + (font == null ? "null" : font.getClass()) + ")"); } // Background color if (cellStyle.getFillPattern() == 1) { Color color = cellStyle.getFillForegroundColorColor(); if (color instanceof HSSFColor) { short[] triplet = ((HSSFColor) color).getTriplet(); if (triplet != null) { styleBuilder.background(triplet); } } else if (color instanceof XSSFColor) { String argb = ((XSSFColor) color).getARGBHex(); if (argb != null) { styleBuilder.background(argb.substring(2)); } } else { throw new IllegalStateException( "Unexpected color type: " + (color == null ? "null" : color.getClass()) + ")"); } } // alignment switch (cellStyle.getAlignment()) { case CellStyle.ALIGN_LEFT: styleBuilder.leftAligned(); break; case CellStyle.ALIGN_RIGHT: styleBuilder.rightAligned(); break; case CellStyle.ALIGN_CENTER: styleBuilder.centerAligned(); break; case CellStyle.ALIGN_JUSTIFY: styleBuilder.justifyAligned(); break; } return styleBuilder.create(); }
From source file:org.bbreak.excella.core.test.util.TestUtil.java
License:Open Source License
public static void checkCell(Cell expected, Cell actual) throws CheckException { List<CheckMessage> errors = new ArrayList<CheckMessage>(); // ---------------------- // ????// w w w .j a v a2 s . c om // ---------------------- if (expected == null && actual == null) { return; } if (expected == null) { errors.add(new CheckMessage("(" + actual.getRowIndex() + "," + actual.getColumnIndex() + ")", null, actual.toString())); throw new CheckException(errors); } if (actual == null) { errors.add(new CheckMessage("(" + expected.getRowIndex() + "," + expected.getColumnIndex() + ")", expected.toString(), null)); throw new CheckException(errors); } // if (expected.getCellTypeEnum() != actual.getCellTypeEnum()) { errors.add(new CheckMessage( "[" + "(" + expected.getRowIndex() + "," + expected.getColumnIndex() + ")" + "]", String.valueOf(expected.getCellTypeEnum()), String.valueOf(actual.getCellTypeEnum()))); throw new CheckException(errors); } try { checkCellStyle(expected.getRow().getSheet().getWorkbook(), expected.getCellStyle(), actual.getRow().getSheet().getWorkbook(), actual.getCellStyle()); } catch (CheckException e) { CheckMessage checkMessage = e.getCheckMessages().iterator().next(); checkMessage.setMessage("(" + expected.getRowIndex() + "," + expected.getColumnIndex() + ")" + checkMessage.getMessage()); errors.add(checkMessage); throw new CheckException(errors); } // log.error("(" + expected.getRowIndex() + "," + expected.getColumnIndex() + ")"); if (!getCellValue(expected).equals(getCellValue(actual))) { log.error(getCellValue(expected) + " / " + getCellValue(actual)); errors.add(new CheckMessage( "[" + "(" + expected.getRowIndex() + "," + expected.getColumnIndex() + ")" + "]", String.valueOf(getCellValue(actual)), String.valueOf(getCellValue(expected)))); throw new CheckException(errors); } }
From source file:org.bbreak.excella.core.util.PoiUtil.java
License:Open Source License
/** * DateUtil?Localize??(,,?)?????????//from w w w . j av a 2 s.c om * ?""???????? * DateUtil???????? * Bug 47071???? * * @param cell */ public static boolean isCellDateFormatted(Cell cell) { if (cell == null) { return false; } boolean bDate = false; double d = cell.getNumericCellValue(); if (DateUtil.isValidExcelDate(d)) { CellStyle style = cell.getCellStyle(); if (style == null) { return false; } int i = style.getDataFormat(); String fs = style.getDataFormatString(); if (fs != null) { // And '"any"' into '' while (fs.contains("\"")) { int beginIdx = fs.indexOf("\""); if (beginIdx == -1) { break; } int endIdx = fs.indexOf("\"", beginIdx + 1); if (endIdx == -1) { break; } fs = fs.replaceFirst(Pattern.quote(fs.substring(beginIdx, endIdx + 1)), ""); } } bDate = DateUtil.isADateFormat(i, fs); } return bDate; }
From source file:org.bbreak.excella.core.util.PoiUtil.java
License:Open Source License
/** * ?//from w w w . j a v a 2s .c om * * @param fromCell * @param toCell */ public static void copyCell(Cell fromCell, Cell toCell) { if (fromCell != null) { // CellType cellType = fromCell.getCellTypeEnum(); switch (cellType) { case BLANK: break; case FORMULA: String cellFormula = fromCell.getCellFormula(); toCell.setCellFormula(cellFormula); break; case BOOLEAN: toCell.setCellValue(fromCell.getBooleanCellValue()); break; case ERROR: toCell.setCellErrorValue(fromCell.getErrorCellValue()); break; case NUMERIC: toCell.setCellValue(fromCell.getNumericCellValue()); break; case STRING: toCell.setCellValue(fromCell.getRichStringCellValue()); break; default: } // if (fromCell.getCellStyle() != null && fromCell.getSheet().getWorkbook().equals(toCell.getSheet().getWorkbook())) { toCell.setCellStyle(fromCell.getCellStyle()); } // if (fromCell.getCellComment() != null) { toCell.setCellComment(fromCell.getCellComment()); } } }
From source file:org.bbreak.excella.core.util.PoiUtil.java
License:Open Source License
/** * ??//from w ww .j av a2 s .c o m * * @param cell * @param value */ public static void setCellValue(Cell cell, Object value) { CellStyle style = cell.getCellStyle(); if (value != null) { if (value instanceof String) { cell.setCellValue((String) value); } else if (value instanceof Number) { Number numValue = (Number) value; if (numValue instanceof Float) { Float floatValue = (Float) numValue; numValue = new Double(String.valueOf(floatValue)); } cell.setCellValue(numValue.doubleValue()); } else if (value instanceof Date) { Date dateValue = (Date) value; cell.setCellValue(dateValue); } else if (value instanceof Boolean) { Boolean boolValue = (Boolean) value; cell.setCellValue(boolValue); } } else { cell.setCellType(CellType.BLANK); cell.setCellStyle(style); } }
From source file:org.bbreak.excella.reports.ReportsTestUtil.java
License:Open Source License
/** * /*from w w w.j ava2 s . co m*/ * * @param expected * @param actual * @throws ReportsCheckException */ public static void checkCell(Cell expected, Cell actual) throws ReportsCheckException { List<CheckMessage> errors = new ArrayList<CheckMessage>(); // ---------------------- // ???? // ---------------------- if (expected == null && actual == null) { return; } if (expected == null) { // if(actual.getCellStyle() != null || actual.getCellType() != Cell.CELL_TYPE_BLANK){ errors.add(new CheckMessage("(" + actual.getRowIndex() + "," + actual.getColumnIndex() + ")", null, actual.toString())); throw new ReportsCheckException(errors); // } } if (actual == null) { errors.add(new CheckMessage("(" + expected.getRowIndex() + "," + expected.getColumnIndex() + ")", expected.toString(), null)); throw new ReportsCheckException(errors); } // if (expected.getCellTypeEnum() != actual.getCellTypeEnum()) { errors.add(new CheckMessage( "[" + "(" + expected.getRowIndex() + "," + expected.getColumnIndex() + ")" + "]", getCellTypeString(expected.getCellTypeEnum()), getCellTypeString(actual.getCellTypeEnum()))); throw new ReportsCheckException(errors); } try { checkCellStyle(expected.getRow().getSheet().getWorkbook(), expected.getCellStyle(), actual.getRow().getSheet().getWorkbook(), actual.getCellStyle()); } catch (ReportsCheckException e) { CheckMessage checkMessage = e.getCheckMessages().iterator().next(); checkMessage.setMessage("(" + expected.getRowIndex() + "," + expected.getColumnIndex() + ")" + checkMessage.getMessage()); errors.add(checkMessage); throw new ReportsCheckException(errors); } // if (!getCellValue(expected).equals(getCellValue(actual))) { log.error(getCellValue(expected) + " / " + getCellValue(actual)); errors.add(new CheckMessage( "[" + "(" + expected.getRowIndex() + "," + expected.getColumnIndex() + ")" + "]", String.valueOf(getCellValue(expected)), String.valueOf(getCellValue(actual)))); throw new ReportsCheckException(errors); } }
From source file:org.bbreak.excella.reports.tag.ImageParamParser.java
License:Open Source License
@Override public ParsedReportInfo parse(Sheet sheet, Cell tagCell, Object data) throws ParseException { Map<String, String> paramDef = TagUtil.getParams(tagCell.getStringCellValue()); // ?/* www .j a v a 2 s . c o m*/ if (hasComments(sheet)) { throw new ParseException( "?[" + sheet.getWorkbook().getSheetName(sheet.getWorkbook().getSheetIndex(sheet)) + "]"); } // ? checkParam(paramDef, tagCell); ReportsParserInfo reportsParserInfo = (ReportsParserInfo) data; ParamInfo paramInfo = reportsParserInfo.getParamInfo(); String paramValue = null; if (paramInfo != null) { // ? String replaceParam = paramDef.get(PARAM_VALUE); paramValue = getParamData(paramInfo, replaceParam); // ?? Integer dx1 = null; if (paramDef.containsKey(PARAM_WIDTH)) { dx1 = Integer.valueOf(paramDef.get(PARAM_WIDTH)); } // ??? Integer dy1 = null; if (paramDef.containsKey(PARAM_HEIGHT)) { dy1 = Integer.valueOf(paramDef.get(PARAM_HEIGHT)); } // ??? Double scale = 1.0; if (paramDef.containsKey(PARAM_SCALE)) { scale = Double.valueOf(paramDef.get(PARAM_SCALE)); } // ?????? if (ReportsUtil.getMergedAddress(sheet, tagCell.getRowIndex(), tagCell.getColumnIndex()) != null) { CellStyle cellStyle = tagCell.getCellStyle(); tagCell.setCellType(CellType.BLANK); tagCell.setCellStyle(cellStyle); } else { tagCell = new CellClone(tagCell); PoiUtil.clearCell(sheet, new CellRangeAddress(tagCell.getRowIndex(), tagCell.getRowIndex(), tagCell.getColumnIndex(), tagCell.getColumnIndex())); } if (log.isDebugEnabled()) { Workbook workbook = sheet.getWorkbook(); String sheetName = workbook.getSheetName(workbook.getSheetIndex(sheet)); log.debug("[??=" + sheetName + ",=(" + tagCell.getRowIndex() + "," + tagCell.getColumnIndex() + ")] " + tagCell.getStringCellValue() + " " + paramValue); } if (paramValue != null) { replaceImageValue(sheet, tagCell, paramValue, dx1, dy1, scale); } } // ???? ParsedReportInfo parsedReportInfo = new ParsedReportInfo(); parsedReportInfo.setParsedObject(paramValue); parsedReportInfo.setRowIndex(tagCell.getRowIndex()); parsedReportInfo.setColumnIndex(tagCell.getColumnIndex()); parsedReportInfo.setDefaultRowIndex(tagCell.getRowIndex()); parsedReportInfo.setDefaultColumnIndex(tagCell.getColumnIndex()); return parsedReportInfo; }
From source file:org.castafiore.groovy.Main.java
License:Open Source License
/** * @param args/*from ww w.jav a 2 s . co m*/ */ public static void main(String[] args) throws Exception { // Main m = new Main(); // m.startContainer(); // TutorialService tService = (TutorialService)m.getObject("tutorial"); // tService.testMethod(); // List<ASTNode> nodes = new AstBuilder().buildFromString("def w; def v; System.out.println(\"fsdfsdfs\");"); // // // for(ASTNode n : nodes){ // if(n instanceof BlockStatement){ // BlockStatement block = (BlockStatement)n; // // List<Statement> stms = block.getStatements(); // for(Statement s : stms){ // if(s instanceof ExpressionStatement){ // ExpressionStatement expression = (ExpressionStatement)s; // Expression exp = expression.getExpression(); // if(exp instanceof DeclarationExpression){ // DeclarationExpression declaration = (DeclarationExpression)exp; // System.out.println( declaration.getLeftExpression().getText()); // } // // //System.out.println(expression.getExpression().getClass().getName()); // // } // //System.out.println(s.getClass().getName()); // } // } // // System.out.println(n.getText()); // } ///root/users/erevolution/Applications/e-Shop/erevolution/DefaultCashBook/entries/2012/ //INSERT INTO WFS_FILE VALUES ('Account', '/root/users/erevolution/Applications/e-Shop/erevolution/DefaultCashBook/accounts/1351867186927', 'org.castafiore.accounting.Account', NULL, '2012-11-02 23:23:20', NULL, '2012-11-02 23:23:20', '\0', '1351867186927', 'erevolution', '*:users', 0, 0, '', NULL, 1, 1, '', 'Whenever there is a sales', NULL, 'Sales Account', NULL, NULL, NULL, NULL, NULL, 'SALES', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'Income', 0.00, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '/root/users/erevolution/Applications/e-Shop/erevolution/DefaultCashBook/accounts', NULL); // List<String> tier1 = new ArrayList<String>(); // List<String> tier2 = new ArrayList<String>(); // Sheet s = new HSSFWorkbook(new FileInputStream(new File("c:\\java\\erevolution\\tiers.xls"))).getSheetAt(0); // for(int i =1; i <= s.getLastRowNum();i++){ // // Row r = s.getRow(i); // if(r != null && r.getCell(1) != null && r.getCell(1).getNumericCellValue() >0){ // //tier 1 agent for erevolution // //System.out.println("insert into SECU_RELATIONSHIP values ("+(i+11000)+",'erevolution', 'Tier 2 Agent', '"+new Double(r.getCell(1).getNumericCellValue()).intValue()+"');"); // tier2.add(new Double(r.getCell(1).getNumericCellValue()).intValue() + ""); // // } // // if(r != null && r.getCell(0) != null && r.getCell(0).getNumericCellValue() >0){ // //tier 1 agent for erevolution // //System.out.println("insert into SECU_RELATIONSHIP values ("+(i+11000)+",'erevolution', 'Tier 2 Agent', '"+new Double(r.getCell(1).getNumericCellValue()).intValue()+"');"); // tier1.add(new Double(r.getCell(0).getNumericCellValue()).intValue() + ""); // // } //// if(s.getRow(i).getCell(0).getStringCellValue().equalsIgnoreCase("merchant")) //// System.out.println("insert into SECU_RELATIONSHIP values ("+(i+10000)+",'erevolution', '"+s.getRow(i).getCell(3).getStringCellValue()+"', '"+s.getRow(i).getCell(1).getStringCellValue()+"');"); // } //FileOutputStream fout = new FileOutputStream(new File("c:\\java\\erevolution\\tiers.sql")); //FileWriter writer = new FileWriter(new File("c:\\java\\erevolution\\tiers.sql")); //int i = 12000; // for(String t1 : tier1){ // for(String t2 : tier2){ // //writer.write(str) // writer.write("insert into SECU_RELATIONSHIP values ("+(i++)+",'"+t1+"', 'Customer', '"+t2+"');\n"); // writer.write("insert into SECU_RELATIONSHIP values ("+(i++)+",'"+t2+"', 'Supplier', '"+t1+"');\n"); // } // } //writer.flush(); //writer.close(); // FileWriter writer = new FileWriter(new File("c:\\java\\erevolution\\tiers.sql")); // tier2.add("erevolution"); // String[] accs = new String[]{"Sales:SALES","Purchases:PURCHASES", // "Travelling Expenses:TRAVELLING", "Salary:SALARY", "Tax:TAX", "Misc:MISC", // "Electricity:ELEC,Water,Telecom","NPS,NPF:NPS", "Bank charges and Interest:BNKCHGS","Loan:LOAN","Petty cash:PETTY", "Vehicle expenses:VEHICLE"}; // // // // for(String t1 : tier1){ // // for(String ss : accs){ // String[] parts = StringUtil.split(ss, ":"); // String code = parts[1]; // String acco = parts[0]; // String name = System.currentTimeMillis()+StringUtil.nextString(10); // writer.write("INSERT INTO WFS_FILE VALUES ('Account', '/root/users/"+t1+"/Applications/e-Shop/"+t1+"/DefaultCashBook/accounts/"+name+ "', 'org.castafiore.accounting.Account', NULL, '2012-11-02 23:23:20', NULL, '2012-11-02 23:23:20', '\0', '1351867186927', '"+t1+"', '*:users', 0, 0, ' ', NULL, 1, 1, ' ', '"+acco+"', NULL, '"+acco+"', NULL, NULL, NULL, NULL, NULL, '"+code+"', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'Income', 0.00, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '/root/users/"+t1+"/Applications/e-Shop/"+t1+"/DefaultCashBook/accounts', NULL);\n"); // } // // // // } // // for(String t1 : tier2){ // for(String ss : accs){ // String[] parts = StringUtil.split(ss, ":"); // String code = parts[1]; // String acco = parts[0]; // String name = System.currentTimeMillis()+StringUtil.nextString(10); // writer.write("INSERT INTO WFS_FILE VALUES ('Account', '/root/users/"+t1+"/Applications/e-Shop/"+t1+"/DefaultCashBook/accounts/"+name+ "', 'org.castafiore.accounting.Account', NULL, '2012-11-02 23:23:20', NULL, '2012-11-02 23:23:20', '\0', '1351867186927', '"+t1+"', '*:users', 0, 0, ' ', NULL, 1, 1, ' ', '"+acco+"', NULL, '"+acco+"', NULL, NULL, NULL, NULL, NULL, '"+code+"', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'Income', 0.00, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '/root/users/"+t1+"/Applications/e-Shop/"+t1+"/DefaultCashBook/accounts', NULL);\n"); // } // } // // writer.flush(); // writer.close(); Workbook hs = new HSSFWorkbook(); Cell c = hs.createSheet().createRow(0).createCell(0); c.setCellValue("45"); c.getCellStyle().setDataFormat(HSSFDataFormat.getBuiltinFormat("$#,##0_);($#,##0)")); DataFormatter d = new HSSFDataFormatter(); System.out.println(d.formatCellValue(c)); //MessageFormat.format(pattern, arguments) }