POI讀取單元格信息及單元格公式


Java操作EXCEL的利器一般都是POI和JXL,鄙人只是POI的忠實粉絲。(其實我是沒有用過JXL)。

現在大多數的excel都是07以上的版本,所以我一般是用07的基礎上使用POI。

  1. 一、讀取單元格

單元格有樣式和值,以及值得類型。

樣式復制封裝成一個函數:

public XSSFCellStyle cloneAllCellStyle(XSSFCell sourceCell, XSSFWorkbook targetWb){

        //創建一個樣式
        XSSFCellStyle tempStyle = targetWb.createCellStyle(); //樣式
        //數值格式,創建字符及數字格式
        DataFormat format= targetWb.createDataFormat();
        //字體
        XSSFFont font= targetWb.createFont();
        try{
            tempStyle.setDataFormat(format.getFormat( sourceCell.getCellStyle().getDataFormatString()));
        }catch(NullPointerException e){
            tempStyle.setDataFormat((short)0);
        }
        font.setColor(sourceCell.getCellStyle().getFont().getXSSFColor());
        font.setBold(sourceCell.getCellStyle().getFont().getBold());
        font.setBoldweight(sourceCell.getCellStyle().getFont().getBoldweight());
        try{
            font.setCharSet(sourceCell.getCellStyle().getFont().getCharSet());
        }catch(POIXMLException e){
            font.setCharSet(0);
        }
        //        font.setCharSet(sourceCell.getCellStyle().getFont().getCharSet());

        font.setFamily(sourceCell.getCellStyle().getFont().getFamily());
        font.setFontHeight(sourceCell.getCellStyle().getFont().getFontHeight());
        font.setFontHeightInPoints(sourceCell.getCellStyle().getFont().getFontHeightInPoints());
        font.setFontName(sourceCell.getCellStyle().getFont().getFontName());
        font.setItalic(sourceCell.getCellStyle().getFont().getItalic());
        font.setStrikeout(sourceCell.getCellStyle().getFont().getStrikeout());
        //        font.setThemeColor(sourceCell.getCellStyle().getFont().getThemeColor());
        font.setTypeOffset(sourceCell.getCellStyle().getFont().getTypeOffset());
        font.setUnderline(sourceCell.getCellStyle().getFont().getUnderline());

        tempStyle.setAlignment( sourceCell.getCellStyle().getAlignment());
        tempStyle.setVerticalAlignment(sourceCell.getCellStyle().getVerticalAlignment());
        tempStyle.setBorderBottom(sourceCell.getCellStyle().getBorderBottom());
        tempStyle.setBorderLeft(sourceCell.getCellStyle().getBorderLeft());
        tempStyle.setBorderRight(sourceCell.getCellStyle().getBorderRight());
        tempStyle.setBorderTop(sourceCell.getCellStyle().getBorderTop());
        tempStyle.setBottomBorderColor(sourceCell.getCellStyle().getBottomBorderXSSFColor());
        tempStyle.setLeftBorderColor(sourceCell.getCellStyle().getLeftBorderXSSFColor());
        tempStyle.setRightBorderColor(sourceCell.getCellStyle().getRightBorderXSSFColor());
        tempStyle.setTopBorderColor(sourceCell.getCellStyle().getTopBorderXSSFColor());
        tempStyle.setFillBackgroundColor(sourceCell.getCellStyle().getFillBackgroundColorColor());
        tempStyle.setFont(font);
        try{
            tempStyle.setFillForegroundColor(sourceCell.getCellStyle().getFillForegroundColorColor());
        }catch(NullPointerException e){
            tempStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        }
        tempStyle.setFillPattern(sourceCell.getCellStyle().getFillPattern());
        tempStyle.setRotation(sourceCell.getCellStyle().getRotation());
        tempStyle.setHidden(sourceCell.getCellStyle().getHidden());
        tempStyle.setWrapText(sourceCell.getCellStyle().getWrapText());
        tempStyle.setIndention(sourceCell.getCellStyle().getIndention());
        tempStyle.setLocked(sourceCell.getCellStyle().getLocked());

        return tempStyle;

    }

調用直接獲取單元格的樣式內容。

獲取單元格值的類型:cell.getCellType()

根據值類型不同獲取不同的值:

    switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_BLANK:
                            tempValue.add("");
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            tempValue.add(cell.getBooleanCellValue());
                            break;
                        case Cell.CELL_TYPE_ERROR:
                            tempValue.add(cell.getErrorCellString());
                            break;
                        case Cell.CELL_TYPE_FORMULA:
                            tempValue.add(cell.getCellFormula());
                            map.put("formulaFlag", true);
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            tempValue.add(cell.getNumericCellValue());
                            break;
                        case Cell.CELL_TYPE_STRING:
                            tempValue.add(cell.getStringCellValue());
                            break;
                        default:
                            break;
                        }

創建內容

//工作空間
        XSSFWorkbook targetWb = new XSSFWorkbook();        
     //sheet
        XSSFSheet targetSheet = targetWb.createSheet("行匯總");
     //       刪除sheet
           targetWb.removeSheetAt(index);  //index表示第幾個sheet,從0開始計數
          //row
XSSFRow row=targetSheet.createRow(i+num1-startRow+1); 
 //cell
      XSSFCell  cell=row.createCell(j);    //j 行

二、 操作單元格函數

POI能夠讀取函數,然后再把函數寫入到單元格中,excel自己計算函數。而函數操作單元格的位置,一般是固定的,所以操作的單元格無法改變。

1、讀取函數和寫入函數

cell.getCellFormula()

 

       上面的代碼中,獲取函數的內容,類型為string。

       寫入函數:

        

cell.setCellFormula((String)cellValues.get(j));

 

 

2、獲取函數計算之后的值:

  有的地方直接寫:

   cell.getNumberValue();這樣有時候會報錯,當cell的內容不是值得時候。

  最后做一個異常拋出。

當然有時候也讀不出值,讀出的值是0.0(double)

  讀取函數值得另一種方法:

  XSSFFormulaEvaluator evaluator=new XSSFFormulaEvaluator(targetWb);

                                CellValue tempCellValue = evaluator.evaluate(cell); 

                                 

                                double cellValue1 =

tempCellValue.getNumberValue(); 

                                  

 

       

       

     如何你是獲取excel的值之后,再寫入另一個單元格,建議寫入值之前,先改變單元的值類型,變成數值型:

    

   cell.set(XSSFCell.CELL_TYPE_NUMERIC);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM