java操作Excel之POI(3)
生活随笔
收集整理的這篇文章主要介紹了
java操作Excel之POI(3)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、字體處理
1 /** 2 * 字體處理 3 */ 4 public static void main(String[] args) throws Exception { 5 Workbook wb = new HSSFWorkbook(); 6 Sheet sheet = wb.createSheet("sheet1"); 7 Row row = sheet.createRow(1); //創建第2行 8 9 //創建一個字體處理類 10 Font font = wb.createFont(); 11 font.setFontHeightInPoints((short)24); //字體大小 12 font.setFontName("Courier New"); 13 font.setItalic(true); //斜體 14 font.setStrikeout(true); //中劃線 15 16 CellStyle style = wb.createCellStyle(); 17 style.setFont(font); 18 19 Cell cell = row.createCell(1); 20 cell.setCellValue("This is test of fonts"); 21 cell.setCellStyle(style); 22 23 FileOutputStream fileOut = new FileOutputStream("E:\\工作簿.xls"); 24 wb.write(fileOut); 25 fileOut.close(); 26 } View Code?
二、讀取和重寫工作簿
1 /** 2 * 讀取和重寫工作簿 3 * 一般用在:按照一個模板,將這個模板填充數據,再做導出來; 4 */ 5 public static void main(String[] args) throws Exception { 6 InputStream is = new FileInputStream("E:\\工作簿.xls"); 7 POIFSFileSystem pfs = new POIFSFileSystem(is); 8 Workbook wb = new HSSFWorkbook(pfs); 9 Sheet sheet = wb.getSheetAt(0); //獲取第一個sheet頁 10 Row row = sheet.getRow(0); //獲取第一行 11 12 Cell cell = row.getCell(0); //獲取第一個單元格 13 if(cell == null){ 14 cell = row.createCell(3); 15 } 16 17 cell.setCellType(Cell.CELL_TYPE_STRING); 18 cell.setCellValue("測試單元格"); 19 20 FileOutputStream fileOut = new FileOutputStream("E:\\工作簿.xls"); 21 wb.write(fileOut); 22 fileOut.close(); 23 } View Code?
三、單元格中使用換行
1 /** 2 * 單元格中使用換行 3 */ 4 public static void main(String[] args) throws Exception { 5 Workbook wb = new HSSFWorkbook(); 6 Sheet sheet = wb.createSheet("sheet1"); 7 Row row = sheet.createRow(2); //第3行 8 Cell cell = row.createCell(2); //第3列 9 cell.setCellValue("我要換行\n成功了嗎?"); 10 11 CellStyle cs = wb.createCellStyle(); 12 //設置可以換行 13 cs.setWrapText(true); 14 cell.setCellStyle(cs); 15 16 //調整下行的高度 17 row.setHeightInPoints(2*sheet.getDefaultRowHeightInPoints()); 18 //調整單元格寬度 19 sheet.autoSizeColumn(2); 20 21 FileOutputStream fileOut = new FileOutputStream("E:\\工作簿.xls"); 22 wb.write(fileOut); 23 fileOut.close(); 24 } View Code?
四、創建用戶自定義數據格式
1 /** 2 * 創建用戶自定義數據格式 3 */ 4 public static void main(String[] args) throws Exception { 5 Workbook wb = new HSSFWorkbook(); 6 Sheet sheet = wb.createSheet("sheet1"); 7 CellStyle style; 8 DataFormat format=wb.createDataFormat(); 9 Row row; 10 Cell cell; 11 short rowNum=0; 12 short colNum=0; 13 14 row=sheet.createRow(rowNum++); 15 cell=row.createCell(colNum); 16 cell.setCellValue(111111.25); 17 style=wb.createCellStyle(); 18 style.setDataFormat(format.getFormat("0.0")); //設置數據格式 19 cell.setCellStyle(style); 20 21 row=sheet.createRow(rowNum++); 22 cell=row.createCell(colNum); 23 cell.setCellValue(1111111.25); 24 style=wb.createCellStyle(); 25 style.setDataFormat(format.getFormat("#,##0.000")); 26 cell.setCellStyle(style); 27 28 FileOutputStream fileOut = new FileOutputStream("E:\\工作簿.xls"); 29 wb.write(fileOut); 30 fileOut.close(); 31 } View Code轉載于:https://www.cnblogs.com/tenWood/p/6422256.html
總結
以上是生活随笔為你收集整理的java操作Excel之POI(3)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win10输入法简体繁体切换
- 下一篇: Elasticsearch之kopf插件