java poi生成Excel文件並存儲到指定磁盤路徑下

 

描述:    這個方法呢, 是在項目中粘出來的,當然不能說是複製粘貼就能用,  這裏面只需要部分內容就可以實現需要的功能.

藍色部分爲需要導入的包 ,   紅色部分爲主題內容,用來生成Excel文件和部分格式設置.  Excel當成是一個表格,  往裏面插入數據需要 每一行每一列的插入, 所以  用兩個for循環插入數據(見黃色部分代碼) .   

然後呢, 是已經插入數據完 要把文件保存在本地磁盤某個路徑下面,  那麼 就需要判斷該路徑是否存在, 最後將文件以流的形式傳輸到指定目錄下    生成Excel還是比較簡單的  .  如果要更多瞭解   搜索  POI  即可,  我僅是在工作時用一下啦 ,然後作一個分享和記錄,方便以後取用. 若有不足之處 ,請指出,謝謝

 

 

 

 

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

 

 

 

/**

     * 生成excel
     * @throws IOException 
     */
public static void createExcel(  String list,......) throws IOException{
      //創建HSSFWorkbook對象(excel的文檔對象)  
        HSSFWorkbook wb = new HSSFWorkbook();  
        // 建立新的sheet對象(excel的表單)
        HSSFSheet sheet = wb.createSheet("sheet1");
        // 在sheet裏創建第一行,參數爲行索引(excel的行),可以是0~65535之間的任何一個
        HSSFRow row0 = sheet.createRow(0);

        // 添加表頭
        row0.createCell(0).setCellValue("測站名稱");
        row0.createCell(1).setCellValue("測站代碼");
        row0.createCell(2).setCellValue("對象名稱");
        row0.createCell(3).setCellValue("斷面名稱");
        row0.createCell(4).setCellValue("斷面級別");
        row0.createCell(5).setCellValue("檢測時間");
        row0.createCell(6).setCellValue("水期代碼");
        for(int i = 0;i<wrwmclist.size();i++){
            String wrwmc = wrwmclist.get(i);
            row0.createCell(7+i).setCellValue(wrwmc);
            if(i==wrwmclist.size()-1){
                row0.createCell(7+i+1).setCellValue("備註");
            }
        }
        
        //格式化數字
        NumberFormat nf = NumberFormat.getInstance();
        // 創建單元格(excel的單元格,參數爲列索引,可以是0~255之間的任何一個
        
        // 在sheet裏創建往下的行
        for(int i= 0;i<datalist.size();i++){
            DynaBean db = datalist.get(i);
            HSSFRow row = sheet.createRow(i+1);
            row.createCell(0).setCellValue(db.get("SSXZQ")!=null?db.get("SSXZQ").toString():"");
            row.createCell(1).setCellValue(db.get("ORGID")!=null?db.get("ORGID").toString():"");
            row.createCell(2).setCellValue(db.get("DXMC")!=null?db.get("DXMC").toString():"");
            row.createCell(3).setCellValue(db.get("CDMC")!=null?db.get("CDMC").toString():"");
            String jgjb = db.get("JGJB")!=null?db.get("JGJB").toString():"";
            row.createCell(4).setCellValue(dmjbMap(jgjb));
            row.createCell(5).setCellValue(db.get("CYSJ")!=null?db.get("CYSJ").toString():"");
            row.createCell(6).setCellValue(db.get("SQDM")!=null?db.get("SQDM").toString():"");
            for(int n = 0;n<fxxmdmlist.size();n++){
                String wrwdm = fxxmdmlist.get(n);
                String strnum = "0";
                if(db.get(wrwdm)!=null){
                    strnum = subZeroAndDot(db.get(wrwdm).toString()) ;//去掉多餘的 0 
                    strnum = new BigDecimal(strnum).toPlainString();//避免科學計數
                    strnum = nf.format(Double.parseDouble(strnum));//去掉  值爲 0 的所有小數
                }
                row.createCell(7+n).setCellValue(strnum);
                if(n==fxxmdmlist.size()-1){
                    row.createCell(7+n+1).setCellValue(db.get("BZ")!=null?db.get("BZ").toString():"");
                }
            }
            
            
        }
    
       
        //判斷是否存在目錄. 不存在則創建
        isChartPathExist(filepath);
        //輸出Excel文件1  
        FileOutputStream output=new FileOutputStream(filepath+filename);  
        wb.write(output);//寫入磁盤  
        output.close();  

        
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章