导出到Excel文件显示错误

时间:2012-08-08 11:36:50

标签: php format export-to-excel

我正在尝试使用将博客帖子转换为.xls格式的插件。当我尝试打开.xls文件时,我收到一个错误,文件格式不同或文件已损坏。这是用于创建.xls文件的代码。

<?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();
  }
?>

loop.php的代码是

<?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>";
    }
  }

创建.xls文件时是否有问题?

0 个答案:

没有答案
相关问题