0

I am trying to use a plugin that converts the posts of a blog to to .xls format. When i try to open the .xls file i get an error that file format is different or file is corrupt. This is the code for creating .xls file.

<?php
  if (isset($_POST['Submit']) && isset($_POST['post_type']) &&  isset($_POST['ext']) ) {
    $post_type = $_POST['post_type'];
    $ext = $_POST['ext'];
    $str = '';
    if ( is_multisite() ) {
      $blog_info = get_blog_list(0, 'all');
      foreach ($blog_info as $blog) {
        switch_to_blog($blog['blog_id']);
        include('loop.php');
        restore_current_blog();
      }
    } else {
      include('loop.php');
    }
    $filename = strtolower(str_replace(' ','-',get_bloginfo('name'))) .'-urls.' . $ext;
    header("Content-type: application/vnd.ms-excel;");
    header("Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, charset=utf-8;");
    header("Content-Disposition: attachment; filename=" . $filename);

    print $str;//$str variable is used in loop.php
    wp_redirect(admin_url('admin.php?page=../export-2-excel.php&noheader=true'));
    exit();
  }
?>

the code for loop.php is

<?php
  if($post_type != 'comment_authors') {
    query_posts(array('posts_per_page' => -1, 'order'=>'DESC', 'post_type' => $post_type));//-1 is for all posts
    $str .= '<table>
              <tr>
                <td colspan=7>' . get_bloginfo('name').'
              </tr>';
    if (have_posts()) {
      $str .= '<tr>
                <th>Name</th>
                <th>Description</th>
                <th>Url</th>
                <th>Created on</th>
                <th>Author</th>
                <th>Categories</th>
                <th>Tags</th>
              </tr>';
      while (have_posts()) {
        the_post();

        $all_cats = '';
        foreach( (get_the_category()) as $category) {
          $all_cats .= $category->cat_name . ', ';
        }
        $all_cats = substr($all_cats, 0, -2);


        $all_tags = '';
        $posttags = get_the_tags();
        if ($posttags) {
          foreach( (get_the_tags()) as $tag) {
            $all_tags .= $tag->name . ', ';
          }
          $all_tags = substr($all_tags, 0, -2);
        }
        global $post;

        $str.= '
          <tr>
            <td>' . mb_convert_encoding(get_the_title(), 'HTML-ENTITIES', 'UTF-8') . '</td>
            <td>' . mb_convert_encoding($post->post_content, 'HTML-ENTITIES', 'UTF-8') . '</td>
            <td>' . get_permalink() .'</td>
            <td>' . get_the_date('Y-m-d', '', '', FALSE) .'</td>
            <td>' . get_the_author() .'</td>
            <td>' . $all_cats . '</td>
            <td>' . $all_tags . '</td>
          </tr>';
      }
      wp_reset_query();
    } else {
      $str .= '<tr colspan="6"><td>No post found.</td></tr>';
    }
    $str.= '</table><br/></br>';
  } else {
    global $wpdb,$table_prefix;
    $str .= '<table>
            <tr>
              <td colspan=6>' . get_bloginfo('name').'
            </tr>';
    $str .= '<tr>
              <th>Commenter</th>
              <th>Email Address</th>
              <th>Url</th>
              <th>Created on</th>
              <th>IP Address</th>
              <th>Comments</th>
            </tr>';
   $commenters = $wpdb->get_results("SELECT COUNT(comment_ID) AS count, comment_ID, comment_author, comment_author_email, comment_author_url, comment_date, comment_author_IP FROM $table_prefix"."comments WHERE comment_approved = 1 AND comment_type = '' GROUP BY comment_author, comment_author_email ORDER BY comment_author ASC, comment_date DESC;");

    foreach ($commenters as $row)
    {
      $str.= '<tr>
               <td>'.$row->comment_author.'</td>';
      $str.= '<td>'.$row->comment_author_email."</td>";
      $str.= '<td>'.$row->comment_author_url."</td>";
      $str.= '<td>'.$row->comment_date."</td>";
      $str.= '<td>'.$row->comment_author_IP."</td>";
      $str.= '<td>'.$row->count."</td>
             </tr>";
    }
  }

Is there something wrong while creating the .xls file?

4

0 回答 0