POI設置EXCEL單元格格式為文本、小數、百分比、貨幣、日期、科學計數法和中文大寫(copy)


HSSFWorkbook demoWorkBook = new HSSFWorkbook();   

            HSSFSheet demoSheet = demoWorkBook.createSheet("The World's 500 Enterprises");   

            HSSFCell cell = demoSheet.createRow(0).createCell(0);

 

第一種:日期格式

 

            cell.setCellValue(new Date(2008,5,5));

            //set date format

            HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();

            HSSFDataFormat format= demoWorkBook.createDataFormat();

            cellStyle.setDataFormat(format.getFormat("yyyy年m月d日"));

            cell.setCellStyle(cellStyle);

 

第二種:保留兩位小數格式

            cell.setCellValue(1.2);

            HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();

            cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));

            cell.setCellStyle(cellStyle);

 

這里與上面有所不同,用的是HSSFDataFormat.getBuiltinFormat()方法,之所以用這個,是因為0.00是Excel內嵌的格式,完整的Excel內嵌格式列表大家可以看這個窗口中的自定義列表:



 這里就不一一列出了

 

第三種:貨幣格式

 

            cell.setCellValue(20000);

            HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();

            HSSFDataFormat format= demoWorkBook.createDataFormat();

            cellStyle.setDataFormat(format.getFormat("¥#,##0"));

            cell.setCellStyle(cellStyle);

 

第四種:百分比格式

 

            cell.setCellValue(20);

            HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();

            cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00%"));

            cell.setCellStyle(cellStyle);

  此種情況跟第二種一樣

 

第五種:中文大寫格式

 

            cell.setCellValue(20000);

            HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();

            HSSFDataFormat format= demoWorkBook.createDataFormat();

            cellStyle.setDataFormat(format.getFormat("[DbNum2][$-804]0"));

            cell.setCellStyle(cellStyle);

 

第六種:科學計數法格式

 

            cell.setCellValue(20000);

            HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();

            cellStyle.setDataFormat( HSSFDataFormat.getBuiltinFormat("0.00E+00"));

            cell.setCellStyle(cellStyle);

此種情況也與第二種情況一樣

===============================

  日期:

 public static Cell writeDateValue(Workbook book, Sheet sheet, int row,
   int column, Date value) {
  Row poiRow = sheet.getRow(row);
  CreationHelper createHelper = book.getCreationHelper();
  if (poiRow == null) {
   poiRow = sheet.createRow(row);
  }
  Cell poiCell = poiRow.getCell(column);

  if (poiCell == null) {
   poiCell = poiRow.createCell(column);
  }

  CellStyle cellStyle = book.createCellStyle();
  cellStyle.setDataFormat(createHelper.createDataFormat().getFormat(
    "yyyy-mm-dd"));
  if (value != null) {
   poiCell.setCellValue(value);
  } else {
   poiCell.setCellValue(new Date());
  }
  poiCell.setCellStyle(cellStyle);

  return poiCell;
 }

  數字:

 public static Cell writeNumericValue(Sheet sheet, int row, int column,
   Double value) {
  Row poiRow = sheet.getRow(row);
  if (poiRow == null) {
   poiRow = sheet.createRow(row);
  }
  Cell poiCell = poiRow.getCell(column);

  if (poiCell != null) {
   poiRow.removeCell(poiCell);
  }
  poiCell = poiRow.createCell(column);
  poiCell.setCellType(Cell.CELL_TYPE_NUMERIC);
  poiCell.setCellValue(value);
  return poiCell;
 }

 

===============================

public class CreateCells {   
    /**  
     * 文檔對象 HSSFWorkbook  ;表單對象 HSSFSheet ;行對象 HSSFRow ;列對象 HSSFCell  
     * excell的格子單元 HSSFFont excell字體 HSSFName 名稱 HSSFDataFormat 日期格式 HSSFHeader  
     * sheet頭 HSSFFooter sheet尾 HSSFCellStyle cell樣式  
     */  
    public static void main(String[] args) throws IOException { 
     // 建立新HSSFWorkbook對象   
        HSSFWorkbook workbook = new HSSFWorkbook();   
        // 建立新的sheet對象   
        // Create a row and put some cells in it.Rows are 0 based. 
        HSSFSheet sheet = workbook.createSheet("表單1");
        // 建立新行   
        // Create a cell and put a value in it.   
        HSSFRow row = sheet.createRow((short) 0); 
        //修改當前行 默認行高  列寬
        //行高
        sheet.setDefaultRowHeightInPoints(10);
        //列款寬
        sheet.setDefaultColumnWidth(10);
        //設置特定單元格的寬度
        sheet.setColumnWidth(4, 20*256);
        sheet.setColumnWidth(5, 30*256);
        sheet.setColumnWidth(6, 30*256);
        
        // 整數類型的cell樣式   
        //HSSFDataFormat.getBuiltinFormat("0.00")  字符串的內容是   Excel有的格式
        HSSFCellStyle numStyle = workbook.createCellStyle();   
        numStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0"));
        //創建1列
        HSSFCell cellNum = row.createCell(0);
        cellNum.setCellValue(1);
        cellNum.setCellStyle(numStyle);
        
        
        // 浮點類型的cell樣式 
        HSSFCellStyle doubleStyle = workbook.createCellStyle();   
        doubleStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
        
        HSSFCell cellDouble = row.createCell(1);
        cellDouble.setCellValue(1.2);
        cellDouble.setCellStyle(doubleStyle);
        
        
        //字符串類型的cell樣式 
        HSSFCellStyle stringStyle = workbook.createCellStyle();   
        stringStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("G/通用格式"));
        
        HSSFCell cellString= row.createCell(2);
        cellString.setCellValue("test");
        cellString.setCellStyle(stringStyle);
        
        //添加cell布爾類型的值  
        row.createCell(3).setCellValue(true);
        
        
        //日期類型的cell樣式   yyyy-m-d  h:mm:ss AM/PM
        HSSFCellStyle dateStyle = workbook.createCellStyle();  
        dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
        HSSFCell dCell = row.createCell(4);
        dCell.setCellValue(new Date());
        dCell.setCellStyle(dateStyle);
        
        
        //設置cell編碼解決中文高位字節截斷
        HSSFCell csCell = row.createCell(5);
        csCell.setCellType(HSSFCell.ENCODING_UTF_16);
        csCell.setCellValue("中文測試_Chinese Words Test");   
  
        // 設置  背景色     邊框
        HSSFCellStyle style1 = workbook.createCellStyle();
        //前景色和后景色都要有  否則會出網格
        style1.setFillForegroundColor(new HSSFColor.YELLOW().getIndex());   
        style1.setFillBackgroundColor(new HSSFColor.YELLOW().getIndex());   
        //設置邊框
        style1.setBorderBottom((short) 1);   
        style1.setBorderTop((short) 1);
        style1.setBorderLeft((short) 1);
        style1.setBorderRight((short) 1);   
        
        //問題:用poi將一個cell中的字體設置成了紅色,結果用excell打開后,這個cell中只有前面一個或幾個字為紅色
        //HSSFFont  font  =  workbook.createFont();  font.setColor(HSSFFont.COLOR_RED);  
        //先從Cell中把HSSFRichTextString取出來 
        //然后HSSFRichTextString對象.applyFont(font) 
        //最后再把HSSFRichTextString對象set回到cell中就行了。。。。。
        
        //設置字體樣式=====================================
        HSSFFont  font  =  workbook.createFont(); 
        //字體位置  上 下 左 右
        //font.setTypeOffset((short)0);
        //字體寬度
        font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
        //字體高度
        font.setFontHeightInPoints((short)8);
        //字體顏色
        font.setColor(HSSFFont.COLOR_RED);  
        //=================================================
        style1.setFont(font);
        
        /**  
         * 注意這句代碼, style1.setFillPattern, 如果你在你的程序中不設置fill pattern,那么  
         * 你上面設置的前景色和背景色就顯示不出來.網絡上很多文章都沒有設置fillpattern 
         * 如果不改變樣式  不需要添加(如:居中)
         */  
        style1.setFillPattern(HSSFCellStyle.SPARSE_DOTS);
        
        
        HSSFCell cellCH = row.createCell(6);
        cellCH.setCellValue("中文測試_Chinese Words Testsss");   
        cellCH.setCellStyle(style1);
        
        
        //貨幣樣式
        HSSFCellStyle moneyStyle = workbook.createCellStyle();   
        moneyStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0"));
        HSSFCell cell12 = row.createCell(7);
        cell12.setCellValue((double) 10000000);
        cell12.setCellStyle(moneyStyle);
  
        // 錯誤顯示 
        row.createCell(8).setCellType(HSSFCell.CELL_TYPE_ERROR);
        //合並單元格
        int startRowNo=0;
        int endRowNo=0;
        int startCellNo=9;
        int endCellNo=10;
        sheet.addMergedRegion(new CellRangeAddress(startRowNo, endRowNo,startCellNo, endCellNo));
  HSSFCell cell = row.createCell(9);
  cell.setCellValue("合並");
  
  //即垂直居中對齊且水平居中對齊    居中后背景顏色變化了
  HSSFCellStyle style = workbook.createCellStyle();   
  style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直   
  style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平   
  //如果不改變樣式  不需要添加
  //style.setFillPattern(HSSFCellStyle.SPARSE_DOTS);
  cell.setCellStyle(style);
  
        FileOutputStream fileOut = new FileOutputStream("e:/workbook.xls");   
        workbook.write(fileOut);   
        fileOut.close();   
    }   
}  

 

===============================


免責聲明!

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



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