java excel導入導出工具類

    /**
     * 導出Excel
     * @param sheetName sheet名稱
     * @param title 標題
     * @param values 內容
     * @param wb HSSFWorkbook對象
     * @return
     */
    public static HSSFWorkbook getHSSFWorkbook(String sheetName,String []title,String [][]values, HSSFWorkbook wb){

        // 第一步,創建一個HSSFWorkbook,對應一個Excel文件
        if(wb == null){
            wb = new HSSFWorkbook();
        }

        // 第二步,在workbook中添加一個sheet,對應Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(sheetName);
        sheet.setDefaultColumnWidth(10);
        // 第三步,在sheet中添加表頭第0行,注意老版本poi對Excel的行數列數有限制
        HSSFRow row = sheet.createRow(0);
        // 第四步,創建單元格,並設置值表頭 設置表頭居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 創建一個居中格式
        HSSFCellStyle headerStyle = wb.createCellStyle();
        headerStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
        headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //設置字體
        HSSFFont font = wb.createFont();
        font.setFontHeightInPoints((short) 14);
        font.setFontName("黑體");
        // 把字體應用到當前的樣式
        headerStyle.setFont(font);
        //聲明列對象
        HSSFCell cell = null;
        //創建標題
        for(int i=0;i<title.length;i++){
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(headerStyle);
        }
        //創建內容
        for(int i=0;i<values.length;i++){
            row = sheet.createRow(i + 1);
            for(int j=0;j<values[i].length;j++){
                //將內容按順序賦給對應的列對象
            	cell = row.createCell(j);
            	cell.setCellValue(values[i][j]);
            	cell.setCellStyle(style);
            }
        }
        //文字自適應
        for(int i=0;i<title.length;i++) {
        	sheet.autoSizeColumn(i+1);
        }
        return wb;
    }
  • //發送響應流方法
    	 public static void setResponseHeader(HttpServletResponse response, String fileName) {
    	     try {
    	         try {
    	             fileName = new String(fileName.getBytes(),"ISO8859-1");
    	         } catch (UnsupportedEncodingException e) {
    	             // TODO Auto-generated catch block
    	             e.printStackTrace();
    	         }
    	         response.setContentType("application/octet-stream;charset=ISO8859-1");
    	         response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
    	         response.addHeader("Pargam", "no-cache");
    	         response.addHeader("Cache-Control", "no-cache");
    	     } catch (Exception ex) {
    	         ex.printStackTrace();
    	     }
    	 }
           /**
    	     * 根據fileType不同讀取excel文件
    	     *
    	     * @param path
    	     * @param path
    	     * @throws IOException
    	     */
    	    public static List<List<String>> readExcel(String path) {
    	        String fileType = path.substring(path.lastIndexOf(".") + 1);
    	        // return a list contains many list
    	        List<List<String>> lists = new ArrayList<List<String>>();
    	        //讀取excel文件
    	        InputStream is = null;
    	        try {
    	            is = new FileInputStream(path);
    	            //獲取工作薄
    	            Workbook wb = null;
    	            if (fileType.equals("xls")) {
    	                wb = new HSSFWorkbook(is);
    	            } else if (fileType.equals("xlsx")) {
    	                wb = new XSSFWorkbook(is);
    	            } else {
    	                return null;
    	            }
    
    	            //讀取第一個工作頁sheet
    	            Sheet sheet = wb.getSheetAt(0);
    	            //第一行爲標題
    	            for (Row row : sheet) {
    	                ArrayList<String> list = new ArrayList<String>();
    	                for (Cell cell : row) {
    	                    //根據不同類型轉化成字符串
    	                    cell.setCellType(Cell.CELL_TYPE_STRING);
    	                    list.add(cell.getStringCellValue());
    	                }
    	                lists.add(list);
    	            }
    	        } catch (IOException e) {
    	            e.printStackTrace();
    	        } finally {
    	            try {
    	                if (is != null) is.close();
    	            } catch (IOException e) {
    	                e.printStackTrace();
    	            }
    	        }
    	        return lists;
    	    }
    /**
    	  * 設置excel表頭樣式
    	  * @param headerStyle
    	  * @param wb
    	  * @return
    	  */
    	 public static HSSFCellStyle setHeadCss(HSSFCellStyle headerStyle,HSSFWorkbook wb) {
    		 headerStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
    	     headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    	     headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    	     headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    	     headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
    	     headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
    	     headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    	     HSSFFont font = wb.createFont();
    	     font.setFontHeightInPoints((short) 13);
    	     font.setFontName("黑體");
    	     // 把字體應用到當前的樣式
    	     headerStyle.setFont(font);
    		return headerStyle;
    	 }

     

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