POI 设置

  

  1.               FileOutputStream fos = new FileOutputStream("D:\\15.xls");  
  2.   
  3. HSSFWorkbook wb = new HSSFWorkbook();  
  4.   
  5. /** 
  6.  * ======================================================== 
  7.  *                          设置cell宽度 
  8.  *  通过sheet 对象,setColumnWidth设置cell的宽度 
  9.  * ======================================================== 
  10.  */  
  11. HSSFSheet sheet = wb.createSheet("sheet1");  
  12. // api 段信息 Set the width (in units of 1/256th of a character width)  
  13. sheet.setColumnWidth(0, 20 * 256);  
  14.   
  15. /** 
  16.  * ======================================================== 
  17.  *                          设置行高度 
  18.  *  通过row 对象设置行高 
  19.  * ======================================================== 
  20.  */  
  21. HSSFRow row = sheet.createRow(0);  
  22. //heightInPoints 设置的值永远是height属性值的20倍  
  23. row.setHeightInPoints(20);  
  24.   
  25. HSSFRow row1 = sheet.createRow(5);  
  26. // Set the row's height or set to ff (-1) for undefined/default-height.  
  27. // Set the height in "twips" or  
  28. // 1/20th of a point.  
  29. row1.setHeight((short) (25 * 20));  
  30.   
  31. HSSFCell cell = row.createCell(0);  
  32.   
  33. cell.setCellValue("a1b2c3d4e5f6g7h8i9");  
  34.   
  35. //设置默认宽度、高度值          
  36. HSSFSheet sheet2 =  wb.createSheet("sheet2");  
  37.           
  38. sheet2.setDefaultColumnWidth(20);  
  39. sheet2.setDefaultRowHeightInPoints(20);  
  40.   
  41.               //格式化单元格日期信息  
  42. HSSFDataFormat dataFormat =  wb.createDataFormat();  
  43. short dataformat = dataFormat.getFormat("yyyy-mm-dd HH:MM");  
  44. HSSFCellStyle style = wb.createCellStyle();  
  45.   
  46.   
  47. style.setDataFormat(dataformat);  
  48.   
  49.   
  50. HSSFCell cell2 = sheet2.createRow(0).createCell(0);  
  51.   
  52. cell2.setCellValue(new Date());  
  53.   
  54. cell2.setCellStyle(style);  
  55.   
  56. wb.write(fos);  
  57.   
  58. fos.close();  
posted @ 2016-10-25 11:32  miMnM  阅读(237)  评论(0编辑  收藏  举报