/* public static void copy(MultipartFile file,String savePath,String newname) throws Exception { try { File targetFile = new File(savePath,newname); if (!targetFile.exists()) { //判断文件夹是否存在,不存在就创建 targetFile.mkdirs(); } file.transferTo(targetFile); } catch (Exception e) { e.printStackTrace(); } } */ private static String getCellValue(HSSFCell cell) { DecimalFormat df = new DecimalFormat("#"); String cellValue = null; if (cell == null) return null; switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: if (HSSFDateUtil.isCellDateFormatted(cell)) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); cellValue = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())); break; } cellValue = df.format(cell.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_STRING: cellValue = String.valueOf(cell.getStringCellValue()); break; case HSSFCell.CELL_TYPE_FORMULA: cellValue = String.valueOf(cell.getCellFormula()); break; case HSSFCell.CELL_TYPE_BLANK: cellValue = null; break; case HSSFCell.CELL_TYPE_BOOLEAN: cellValue = String.valueOf(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_ERROR: cellValue = String.valueOf(cell.getErrorCellValue()); break; } if (cellValue != null && cellValue.trim().length() <= 0) { cellValue = null; } return cellValue; }
public void testSquareMacro() { HSSFWorkbook w = openSample("SquareMacro.xls"); HSSFSheet s0 = w.getSheetAt(0); HSSFRow[] r = {s0.getRow(0), s0.getRow(1)}; HSSFCell a1 = r[0].getCell(0); assertEquals("square(1)", a1.getCellFormula()); assertEquals(1d, a1.getNumericCellValue(), 1e-9); HSSFCell a2 = r[1].getCell(0); assertEquals("square(2)", a2.getCellFormula()); assertEquals(4d, a2.getNumericCellValue(), 1e-9); HSSFCell b1 = r[0].getCell(1); assertEquals("IF(TRUE,square(1))", b1.getCellFormula()); assertEquals(1d, b1.getNumericCellValue(), 1e-9); HSSFCell b2 = r[1].getCell(1); assertEquals("IF(TRUE,square(2))", b2.getCellFormula()); assertEquals(4d, b2.getNumericCellValue(), 1e-9); HSSFCell c1 = r[0].getCell(2); assertEquals("square(square(1))", c1.getCellFormula()); assertEquals(1d, c1.getNumericCellValue(), 1e-9); HSSFCell c2 = r[1].getCell(2); assertEquals("square(square(2))", c2.getCellFormula()); assertEquals(16d, c2.getNumericCellValue(), 1e-9); HSSFCell d1 = r[0].getCell(3); assertEquals("square(one())", d1.getCellFormula()); assertEquals(1d, d1.getNumericCellValue(), 1e-9); HSSFCell d2 = r[1].getCell(3); assertEquals("square(two())", d2.getCellFormula()); assertEquals(4d, d2.getNumericCellValue(), 1e-9); }
/** Test for bug due to attempt to convert a cached formula error result to a boolean */ public void testUpdateCachedFormulaResultFromErrorToNumber_bug46479() { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("Sheet1"); HSSFRow row = sheet.createRow(0); HSSFCell cellA1 = row.createCell(0); HSSFCell cellB1 = row.createCell(1); cellB1.setCellFormula("A1+1"); HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); cellA1.setCellErrorValue((byte) HSSFErrorConstants.ERROR_NAME); fe.evaluateFormulaCell(cellB1); cellA1.setCellValue(2.5); fe.notifyUpdateCell(cellA1); try { fe.evaluateInCell(cellB1); } catch (IllegalStateException e) { if (e.getMessage().equals("Cannot get a numeric value from a error formula cell")) { throw new AssertionFailedError("Identified bug 46479a"); } } assertEquals(3.5, cellB1.getNumericCellValue(), 0.0); }
/** * Gets the tableCellEditorComponent attribute of the SVTableCellEditor object * * @return The tableCellEditorComponent value */ public Component getTableCellEditorComponent( JTable table, Object value, boolean isSelected, int row, int column) { System.out.println("GetTableCellEditorComponent"); HSSFCell cell = (HSSFCell) value; if (cell != null) { HSSFCellStyle style = cell.getCellStyle(); HSSFFont f = wb.getFontAt(style.getFontIndex()); boolean isbold = f.getBoldweight() > HSSFFont.BOLDWEIGHT_NORMAL; boolean isitalics = f.getItalic(); int fontstyle = Font.PLAIN; if (isbold) fontstyle = Font.BOLD; if (isitalics) fontstyle = fontstyle | Font.ITALIC; int fontheight = f.getFontHeightInPoints(); if (fontheight == 9) fontheight = 10; // fix for stupid ol Windows Font font = new Font(f.getFontName(), fontstyle, fontheight); editor.setFont(font); if (style.getFillPattern() == HSSFCellStyle.SOLID_FOREGROUND) { editor.setBackground(getAWTColor(style.getFillForegroundColor(), white)); } else editor.setBackground(white); editor.setForeground(getAWTColor(f.getColor(), black)); // Set the value that is rendered for the cell switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_BLANK: editor.setText(""); break; case HSSFCell.CELL_TYPE_BOOLEAN: if (cell.getBooleanCellValue()) { editor.setText("true"); } else { editor.setText("false"); } break; case HSSFCell.CELL_TYPE_NUMERIC: editor.setText(Double.toString(cell.getNumericCellValue())); break; case HSSFCell.CELL_TYPE_STRING: editor.setText(cell.getRichStringCellValue().getString()); break; case HSSFCell.CELL_TYPE_FORMULA: default: editor.setText("?"); } switch (style.getAlignment()) { case HSSFCellStyle.ALIGN_LEFT: case HSSFCellStyle.ALIGN_JUSTIFY: case HSSFCellStyle.ALIGN_FILL: editor.setHorizontalAlignment(SwingConstants.LEFT); break; case HSSFCellStyle.ALIGN_CENTER: case HSSFCellStyle.ALIGN_CENTER_SELECTION: editor.setHorizontalAlignment(SwingConstants.CENTER); break; case HSSFCellStyle.ALIGN_GENERAL: case HSSFCellStyle.ALIGN_RIGHT: editor.setHorizontalAlignment(SwingConstants.RIGHT); break; default: editor.setHorizontalAlignment(SwingConstants.LEFT); break; } } return editor; }