利用POI实现简单的Excel读写操作

利用POI实现简单的Excel读写操作

java 代码
  1. package test;   
  2.   
  3. import java.io.FileInputStream;   
  4. import java.io.FileOutputStream;   
  5.   
  6. import org.apache.poi.hssf.usermodel.HSSFCell;   
  7. import org.apache.poi.hssf.usermodel.HSSFRow;   
  8. import org.apache.poi.hssf.usermodel.HSSFSheet;   
  9. import org.apache.poi.hssf.usermodel.HSSFWorkbook;   
  10. import org.apache.poi.poifs.filesystem.POIFSFileSystem;   
  11.   
  12. public class POIExcelService    
  13. {   
  14.     private POIFSFileSystem fileSystem;   
  15.     private HSSFWorkbook workbook;   
  16.     private HSSFSheet sheet;   
  17.     private HSSFRow row;   
  18.     private HSSFCell cell;   
  19.        
  20.     public POIExcelService(){}   
  21.        
  22.     /**  
  23.      *   
  24.      * @param xlsPath  
  25.      */  
  26.     public POIExcelService(String xlsPath) throws Exception   
  27.     {   
  28.         this.fileSystem = new POIFSFileSystem(new FileInputStream(xlsPath));   
  29.         this.workbook = new HSSFWorkbook(fileSystem);   
  30.     }   
  31.        
  32.     /**  
  33.      * 获取单元格数据,以String格式返回  
  34.      * @param sheet  
  35.      * @param row  
  36.      * @param cell  
  37.      * @return  
  38.      */  
  39.     public String getCellValue(int sheet,int row,int cell)   
  40.     {   
  41.         setSheet(sheet);   
  42.         setRow(row);   
  43.         setCell(cell);   
  44.         return this.getCell().getStringCellValue();   
  45.     }   
  46.        
  47.     //导出文件   
  48.     public void Export(String xlsPath, int sheet, int row, int cell, String value) throws Exception   
  49.     {   
  50.         FileOutputStream fileOut = new FileOutputStream(xlsPath);       
  51.            
  52.         this.setSheet(sheet);   
  53.         this.setRow(row);   
  54.         this.setCell(cell);   
  55.         this.getCell().setCellValue(value);   
  56.              
  57.         this.workbook.write(fileOut);       
  58.          
  59.         fileOut.close();      
  60.            
  61.     }   
  62.        
  63.     public HSSFCell getCell() {   
  64.         return cell;   
  65.     }   
  66.   
  67.     public void setCell(int cell) {   
  68.         this.cell = this.row.getCell((short)(cell-1));    
  69.     }   
  70.   
  71.     public HSSFRow getRow() {   
  72.         return row;   
  73.     }   
  74.   
  75.     public void setRow(int row) {   
  76.         this.row = this.sheet.getRow(row-1);   
  77.     }   
  78.   
  79.     public HSSFSheet getSheet() {   
  80.         return sheet;   
  81.     }   
  82.   
  83.     public void setSheet(int sheet) {   
  84.         //默认是从0开始的,这里利用n-1转换下,用1开始,调用好理解点   
  85.         this.sheet = this.workbook.getSheetAt(sheet-1);   
  86.     }   
  87.        
  88.     /*  
  89.     public String getCellStringValue(HSSFCell cell) {     
  90.         String cellValue = "";     
  91.         switch (cell.getCellType()) {     
  92.         case HSSFCell.CELL_TYPE_STRING:     
  93.             cellValue = cell.getStringCellValue();     
  94.             if(cellValue.trim().equals("")||cellValue.trim().length()<=0)     
  95.                 cellValue=" ";     
  96.             break;     
  97.         case HSSFCell.CELL_TYPE_NUMERIC:     
  98.             cellValue = String.valueOf(cell.getNumericCellValue());     
  99.             break;     
  100.         case HSSFCell.CELL_TYPE_FORMULA:     
  101.             cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);     
  102.             cellValue = String.valueOf(cell.getNumericCellValue());     
  103.             break;     
  104.         case HSSFCell.CELL_TYPE_BLANK:     
  105.             cellValue=" ";     
  106.             break;     
  107.         case HSSFCell.CELL_TYPE_BOOLEAN:     
  108.             break;     
  109.         case HSSFCell.CELL_TYPE_ERROR:     
  110.             break;     
  111.         default:     
  112.             break;     
  113.         }     
  114.         return cellValue;     
  115.     }    
  116.     */  
  117.        
  118.        
  119.     public static void main(String[] args)    
  120.     {   
  121.         try {   
  122.             POIExcelService excelOper = new POIExcelService("产品项目导入模板.xls");   
  123.             String tmp = excelOper.getCellValue(111);   
  124.             System.out.println(tmp);   
  125.             excelOper.Export("产品项目导入模板_导出.xls",1,1,1,"ABCDE");   
  126.                            
  127.                
  128.         } catch (Exception e) {   
  129.             e.printStackTrace();   
  130.         }   
  131.   
  132.     }   
  133.        
  134.   
  135. }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值