Drupal将数据从表格导出到Excel

发布于 2024-12-10 20:00:54 字数 1109 浏览 0 评论 0原文

我正在使用以下 2 个函数将数据从名为 eco_customers 的表导出到 excel 文件,

但我遇到阿拉伯字符问题,它们显示损坏,如何修改编码以纠正 excel 文件中的阿拉伯字符问题

function cleanData(&$str) { 
    $str = preg_replace("/\t/", "\\t", $str); 
    $str = preg_replace("/\r?\n/", "\\n", $str); 
    if(strstr($str, '"')) 
    $str = '"' . str_replace('"', '""', $str) . '"';
     }

function customer_export(){

 $filename = "cutomers_data_" . date('Ymd') . ".xls"; 
 header("Content-Disposition: attachment; filename=\"$filename\""); 
 header("Content-Type: application/vnd.ms-excel, charset=UTF-8; encoding=UTF-8"); 

 $flag = false;

 $result = db_query("SELECT name, arabic_name, phones, fax, address, country, city, email, website, registration_no FROM eco_customers ") or die('Query failed!'); 

  while(false !== ($row = db_fetch_array($result))) { 

         if(!$flag) { 
          // display field/column names as first row 
          echo implode("\t", array_keys($row)) . "\r\n"; 
          $flag = true; 
          }
         array_walk($row, 'cleanData'); 
         echo implode("\t", array_values($row)) . "\r\n";
  }

 }

I am using the following 2 functions to export data from a table called eco_customers to excel file

but I have a problem with Arabic characters, they show corrupt, how can I modify the encoding to correct the Arabic characters problem in the excel file

function cleanData(&$str) { 
    $str = preg_replace("/\t/", "\\t", $str); 
    $str = preg_replace("/\r?\n/", "\\n", $str); 
    if(strstr($str, '"')) 
    $str = '"' . str_replace('"', '""', $str) . '"';
     }

function customer_export(){

 $filename = "cutomers_data_" . date('Ymd') . ".xls"; 
 header("Content-Disposition: attachment; filename=\"$filename\""); 
 header("Content-Type: application/vnd.ms-excel, charset=UTF-8; encoding=UTF-8"); 

 $flag = false;

 $result = db_query("SELECT name, arabic_name, phones, fax, address, country, city, email, website, registration_no FROM eco_customers ") or die('Query failed!'); 

  while(false !== ($row = db_fetch_array($result))) { 

         if(!$flag) { 
          // display field/column names as first row 
          echo implode("\t", array_keys($row)) . "\r\n"; 
          $flag = true; 
          }
         array_walk($row, 'cleanData'); 
         echo implode("\t", array_values($row)) . "\r\n";
  }

 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

向地狱狂奔 2024-12-17 20:00:54

我使用了 phpExcel 库,现在一切正常

 function customer_export(){


require_once('Classes/PHPExcel.php');



    $objPHPExcel = new PHPExcel();
    $objPHPExcel->getActiveSheet()->setTitle('List of Customers');

    $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Name')
            ->setCellValue('B1', 'Arabic Name')
            ->setCellValue('C1', 'Phone')
            ->setCellValue('D1', 'Fax')
            ->setCellValue('E1', 'Address')
            ->setCellValue('F1', 'Country')
            ->setCellValue('G1', 'City')
            ->setCellValue('H1', 'Email')
            ->setCellValue('I1', 'Website')
            ->setCellValue('J1', 'Registration Number');

 $result = db_query("SELECT name, arabic_name, phones, fax, address, country, city, email, website, registration_no FROM eco_customers ") or die('Query failed!'); 

  // Loop through the result set
    $rowNumber = 2;
    while ($row = db_fetch_array($result)) {
       $col = 'A';
       foreach($row as $cell) {
          $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
          $col++;
       }
       $rowNumber++;
    }


    // Freeze pane so that the heading line won't scroll
    $objPHPExcel->getActiveSheet()->freezePane('A2');

    // Save as an Excel BIFF (xls) file
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

   header('Content-Type: application/vnd.ms-excel');
   header('Content-Disposition: attachment;filename="customers_data_' . date('Ymd') . '.xls"');
   header('Cache-Control: max-age=0');

   $objWriter->save('php://output');
   exit();

 }

I used phpExcel library and everything worked fine for me now

 function customer_export(){


require_once('Classes/PHPExcel.php');



    $objPHPExcel = new PHPExcel();
    $objPHPExcel->getActiveSheet()->setTitle('List of Customers');

    $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Name')
            ->setCellValue('B1', 'Arabic Name')
            ->setCellValue('C1', 'Phone')
            ->setCellValue('D1', 'Fax')
            ->setCellValue('E1', 'Address')
            ->setCellValue('F1', 'Country')
            ->setCellValue('G1', 'City')
            ->setCellValue('H1', 'Email')
            ->setCellValue('I1', 'Website')
            ->setCellValue('J1', 'Registration Number');

 $result = db_query("SELECT name, arabic_name, phones, fax, address, country, city, email, website, registration_no FROM eco_customers ") or die('Query failed!'); 

  // Loop through the result set
    $rowNumber = 2;
    while ($row = db_fetch_array($result)) {
       $col = 'A';
       foreach($row as $cell) {
          $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
          $col++;
       }
       $rowNumber++;
    }


    // Freeze pane so that the heading line won't scroll
    $objPHPExcel->getActiveSheet()->freezePane('A2');

    // Save as an Excel BIFF (xls) file
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

   header('Content-Type: application/vnd.ms-excel');
   header('Content-Disposition: attachment;filename="customers_data_' . date('Ymd') . '.xls"');
   header('Cache-Control: max-age=0');

   $objWriter->save('php://output');
   exit();

 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文