JAVA写Excel文件

public void set_excel(String table_name,String sheet_name,String[][] table_data) throws IOException{
        String table_path = cf.get_src_path(table_name);
        Workbook workbook = null;
        String extension_name = table_name.substring(table_name.indexOf("."));
        if(extension_name.equals(".xls")){
            workbook = new HSSFWorkbook();
        }
        else if(extension_name.equals(".xlsx")){
            workbook = new XSSFWorkbook();
        }
        Sheet sheet = workbook.createSheet(sheet_name);
        int row_count = table_data.length;
        int col_count = table_data[0].length;
        for(int i=0;i<row_count;i++){
            Row row = sheet.createRow(i);
            for(int j=0;j<col_count;j++){
                Cell cell = row.createCell(j,Cell.CELL_TYPE_STRING);
                cell.setCellValue(table_data[i][j]);
            }
        }
        FileOutputStream outputStream = new FileOutputStream(table_path);
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }

你可能感兴趣的:(java)