ADBLOCKを無効にする

ADBlockがサイトの一部のコンテンツをブロックしています

ADBlock errore
結果が見つかりました: 

質問

I've got this very simple thing that just outputs some stuff in CSV format, but it's got to be UTF-8. I open this file in TextEdit or TextMate or Dreamweaver and it displays UTF-8 characters properly, but if I open it in Excel it's doing this silly íÄ kind of thing instead. Here's what I've got at the head of my document:

header("content-type:application/csv;charset=UTF-8");
header("Content-Disposition:attachment;filename=\"CHS.csv\"");

This all seems to have the desired effect except Excel (Mac, 2008) doesn't want to import it properly. There's no options in Excel for me to "open as UTF-8" or anything, so … I'm getting a little annoyed.

I can't seem to find any clear solutions to this anywhere, despite a lot of people having the same problem. The thing I see the most is to include the BOM, but I can't exactly figure out how to do that. As you can see above I'm just echoing this data, I'm not writing any file. I can do that if I need to, I'm just not because there doesn't seem like a need for it at this point. Any help?

Update: I tried echoing the BOM as echo pack("CCC", 0xef, 0xbb, 0xbf); which I just pulled from a site that was trying to detect the BOM. But Excel just appends those three characters to the very first cell when it imports, and still messes up the special characters.

溶液

??????????????????????

  

Mac??Excel????UTF-8

????????????

?????2017??:?????Mac??Microsoft Excel?????????????????????2016 ?? (Office 365????)???????????UTF-8???????????

???Excel????Windows?OS X?????????????????UTF-8??????????????????????????:

  1. ????UTF-16LE?????UTF-8??????????????????

    mb_convert_encoding($csv, 'UTF-16LE', 'UTF-8');
    
    ???
  2. ????UTF-16LE?????????????????????????????

    chr(255) . chr(254)
    
    ???

??OS X??Excel??????????(????????Windows?)?????Excel????1?????????????????????????CSV???????????????????????????????

?????????????????????????????????

??PHP??????(??????????????????????\???????????)????OS X?Windows????????????????

:????????????????????????????????????????????????

?

    $field_cnt = count($fields);

?

    $field_cnt = count($fields)-1;

??????????????????????OpenOffice?Calc????????????????????????Apple??????Google?????????????????????UTF-8????????????????

?????????????????href="https://stackoverflow.com/questions/6588068/which-encoding-opens-csv-files-correctly-with-excel-on-both-mac-and-windows"> ????Excel?Unicode?CSV??????????????????????????

??????????????????????????????????????????????????????????????? League\Csv ????????????? League\Csv??CSV???????????? A???????API????????

CSV??????????????League\Csv????????????

よろしければ、ご意見をお聞かせください

この記事は役に立ちましたか?正しく翻訳されていますか?

その他のヒント

I????(??????)?????????

??????????BOM????????????????:

header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename=Customers_Export.csv');
echo "\xEF\xBB\xBF"; // UTF-8 BOM

?????????????????????????????????Excel 2007??Windows??????????????????????Mac????????

???(???????????CSV????????????????)?????????????

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=file.csv');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
echo "\xEF\xBB\xBF"; // UTF-8 BOM
echo $csv_file_content;
exit();

?????????????Mac??????????????CSV??????UTF8????????????????..????????????2008??...??????

???????????????2??????????????

  1. ?? PHPExcel ???? bpeterson76 ?????????????

    • ????????????????????????????????????Excel 2008????Excel 2007??Windows????Google????????????????UTF-8????????????????????????????????
    • ?PHPExcel??????????????????????????????????????????????????????????????????????????/ CSV??????????????????????????????????????????????
    • ?????????????????????????????????????????Excel5?????????????????????TSV????????????Excel????????????PHP????????????????????????????????????????????????????????

      function excel_export($tsv_data, $filename) {
          $export_data = preg_split("/\n/", $tsv_data);
          foreach($export_data as &$row) {
              $row = preg_split("/\t/", $row);
          }
      
          include("includes/PHPExcel.php");
          include('includes/PHPExcel/Writer/Excel5.php');
      
          $objPHPExcel = new PHPExcel();          
      
          $objPHPExcel->setActiveSheetIndex(0);
          $sheet = $objPHPExcel->getActiveSheet();
          $row = '1';
          $col = "A";
          foreach($export_data as $row_cells) {
              if(!is_array($row_cells)) { continue; }
                  foreach($row_cells as $cell) {
                      $sheet->setCellValue($col.$row, $cell);
                      $col++;
                  }
              $row += 1;
              $col = "A";
          }
      
          $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
          header('Content-Type: application/vnd.ms-excel');
          header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
          header('Cache-Control: max-age=0');
          $objWriter->save('php://output');   
          exit;   
      }
      
      ???
  2. PHPExcel????????????UTF-8&???????CSV?TSV???????????????????????????

    • ?????????????????Excel 2008???????????????????Excel 2007?PC???????????????????????????????Google????????
    • ???href="https://stackoverflow.com/questions/155097/microsoft-excel-mangles-diacritics-in-csv-files"> ?????????????????????accepted???????
    • ????PHP?????(??????????????????)?TSV?????????????????????

      header ( 'HTTP/1.1 200 OK' );
      header ( 'Date: ' . date ( 'D M j G:i:s T Y' ) );
      header ( 'Last-Modified: ' . date ( 'D M j G:i:s T Y' ) );
      header ( 'Content-Type: application/vnd.ms-excel') ;
      header ( 'Content-Disposition: attachment;filename=export.csv' );
      print chr(255) . chr(254) . mb_convert_encoding($tsv_data, 'UTF-16LE', 'UTF-8');
      exit;
      
      ???

????????????????????????????

    header('Content-Encoding: UTF-8');
    header('Content-Type: text/csv; charset=utf-8' );
    header(sprintf( 'Content-Disposition: attachment; filename=my-csv-%s.csv', date( 'dmY-His' ) ) );
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');

    $df = fopen( 'php://output', 'w' );

    //This line is important:
    fputs( $df, "\xEF\xBB\xBF" ); // UTF-8 BOM !!!!!

    foreach ( $rows as $row ) {
        fputcsv( $df, $row );
    }
    fclose($df);
    exit();

Excel??UTF-8????????????????UCS-2LE?????UTF-8????????????????????

mb_convert_encoding($output, 'UCS-2LE', 'UTF-8');

?????????????:

????Mac??Excel????????????????????Excel ??????????CSV??????????????????????????????????CSV??????????????????????????????

???... ...??????????????????????????????????????????????????

???????????BOM????@Daniel Magliola?????????????????????????????????????????????????????????????????

UTF-8???CSV?????????????????????????????Excel???????????

$out = fopen('php://output', 'w');
fprintf($out, chr(0xEF).chr(0xBB).chr(0xBF));
fputcsv($out, $some_csv_strings);

0xEF 0xBB 0xBF BOM???Excel?????????????????????

????Windows??Mac OS???????Excel??????????

??????????????????????????????????Excel???????

function writeCSV($filename, $headings, $data) {   

    //Use tab as field separator
    $newTab  = "\t";
    $newLine  = "\n";

    $fputcsv  =  count($headings) ? '"'. implode('"'.$newTab.'"', $headings).'"'.$newLine : '';

    // Loop over the * to export
    if (! empty($data)) {
      foreach($data as $item) {
        $fputcsv .= '"'. implode('"'.$newTab.'"', $item).'"'.$newLine;
      }
    }

    //Convert CSV to UTF-16
    $encoded_csv = mb_convert_encoding($fputcsv, 'UTF-16LE', 'UTF-8');

    // Output CSV-specific headers
    header('Set-Cookie: fileDownload=true; path=/'); //This cookie is needed in order to trigger the success window.
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false);
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"$filename.csv\";" );
    header("Content-Transfer-Encoding: binary");
    header('Content-Length: '. strlen($encoded_csv));
    echo chr(255) . chr(254) . $encoded_csv; //php array convert to csv/excel

    exit;
}

CSV???????????????????musst???

?????????????????HTTP??????????

UTF8??????????Excel?????????????????iconv()????????????????????????????????

??????

iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $value),

??????

fprintf($file, chr(0xEF).chr(0xBB).chr(0xBF));

??????:

fprintf($file, "\xEF\xBB\xBF");
CSV?????????????????????

?:

<?php
$file = fopen( "file.csv", "w");
fprintf( $file, "\xEF\xBB\xBF");
fputcsv( $file, ["english", 122, "বাংলা"]);
fclose($file);

?mb_convert_encoding?????????UTF-8???????????????????????????????????3??????????:

$newContent = chr(239) . chr(187) . chr(191) . $originalContent

?????????CSV????????????????????

????????????Windows?-1252?????????????UTF-8??MAC???Windows????????????????????????????????????????Ubuntu???????????????????????? ?????code$valueToWrite = mb_convert_encoding($value, 'Windows-1252');???

$response->headers->set('Content-Type', $mime . '; charset=Windows-1252');
    $response->headers->set('Pragma', 'public');
    $response->headers->set('Content-Endcoding','Windows-1252');
    $response->headers->set('Cache-Control', 'maxage=1');
    $response->headers->set('Content-Disposition', $dispositionHeader);
    echo "\xEF\xBB\xBF"; // UTF-8 BOM

**This is 100% works fine in excel for both Windows7,8,10 and also All Mac OS.**
//Fix issues in excel that are not displaying characters containing diacritics, cyrillic letters, Greek letter and currency symbols.

function generateCSVFile($filename, $headings, $data) {

    //Use tab as field separator
    $newTab  = "\t";
    $newLine  = "\n";

    $fputcsv  =  count($headings) ? '"'. implode('"'.$newTab.'"', $headings).'"'.$newLine : '';

    // Loop over the * to export
    if (! empty($data)) {
      foreach($data as $item) {
        $fputcsv .= '"'. implode('"'.$newTab.'"', $item).'"'.$newLine;
      }
    }

    //Convert CSV to UTF-16
    $encoded_csv = mb_convert_encoding($fputcsv, 'UTF-16LE', 'UTF-8');

    // Output CSV-specific headers
    header('Set-Cookie: fileDownload=true; path=/'); //This cookie is needed in order to trigger the success window.
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false);
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"$filename.csv\";" );
    header("Content-Transfer-Encoding: binary");
    header('Content-Length: '. strlen($encoded_csv));
    echo chr(255) . chr(254) . $encoded_csv; //php array convert to csv/excel
    exit;
}

??????????????????????? ????????XLS???????????????????????????????????????????CSV???????????????????????????????????????????????????????CSV????????????????????????????(????????rowheights??????)????????

????iconv??????CSV????????????????  ?????:

$csvString = "Möckmühl;in Möckmühl ist die Hölle los\n";
file_put_contents('path/newTest.csv',iconv("UTF-8", "ISO-8859-1//TRANSLIT",$csvString) );

????????????? "Windows-1252" ?????????????

header('Content-Encoding: Windows-1252');
header('Content-type: text/csv; charset=Windows-1252');
header("Content-Disposition: attachment; filename={$filename}");

???????????????????????????:

private function convertToWindowsCharset($string) {
  $encoding = mb_detect_encoding($string);

  return iconv($encoding, "Windows-1252", $string);
}

????????????????????????????3???????????????????????Windows?HP -UX????????????Linux???????

FileOutputStream fStream = new FileOutputStream( f );
final byte[] bom = new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF };
OutputStreamWriter writer = new OutputStreamWriter( fStream, "UTF8" );
fStream.write( bom );

????????UTF-8 BOM(3??????EF BB BF)??????????????Excel?????????????????????????????????????(????CP1252)??????UTF-8

Excel???CSV??????????????

???????????

????Mac???????????????"sep=;\n"?????????????UTF-16LE????????????????????????

$data = "sep=;\n" .mb_convert_encoding($data, 'UTF-16LE', 'UTF-8');

????????????????

????????????????????????: PHP????????????????????:

$value = utf8_encode($value);

??????Excel?????????

?????????????????????VBA?????????????????????????? CSV?????????????????????????????????????????????????Unicode??????????????????????????????????Excel???????????????????????? Excel?????????CSV????????????????????????????????????

??????????????

????.txt???????????????????????????????????????Excel??????

?????????????????????????????????????Excel??????????CSV??????????????

?????????????????????????????????????...?????????????Excel?Mac?CSV??????????????????????Excel???????????????????????CSV????????? (UTF-8 BOM??)

??iconv's?????????Mac?????2004?????????????????????couldn't:PHPExcel?????????????????????????????????????????????????CSV???????????PHPsnippet?xls???????csv???????????? csv2xls ????XLS??Excel??????????(ä?ö?Ü?...)???????

?????????????????????????????CSV???????????????Windows 7 PC??Excel 2013????????????????(BOM)??????????????????????

    header('Content-Encoding: UTF-8');
    header('Content-type: text/csv; charset=UTF-8');
    header("Content-disposition: attachment; filename=filename.csv");
    header("Pragma: public");
    header("Expires: 0");
    echo "\xEF\xBB\xBF"; // UTF-8 BOM

?????????????

header("Content-type: application/x-download");
header("Content-Transfer-Encoding: binary");
header("Content-disposition: attachment; filename=".$fileName."");
header("Cache-control: private");

echo utf8_decode($output);

Mac?????2008 EASY???????: ????????????????????????????? ???UTF-8???????TextWrangler??.csv???????????????????????????Unicode(UTF-8)????(ISO???1)?????????????????? ?????Mac?Excel 2008?????[????]> [?????]> [??] CSV???????>???????????>????????Windows(ANSI)???????UTF-8???????????????????????????????????...

???????????????????

header('Content-Description: File Transfer');
header('Content-Type: text/csv; charset=UTF-16LE');
header('Content-Disposition: attachment; filename=file.csv');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
// output headers so that the file is downloaded rather than displayed
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
fputs( $output, "\xEF\xBB\xBF" );
// output the column headings
fputcsv($output, array('Thông tin khách hàng đăng ký'));
// fetch the data
$setutf8 = "SET NAMES utf8";
$q = $conn->query($setutf8);
$setutf8c = "SET character_set_results = 'utf8', character_set_client =
'utf8', character_set_connection = 'utf8', character_set_database = 'utf8',
character_set_server = 'utf8'";
$qc = $conn->query($setutf8c);
$setutf9 = "SET CHARACTER SET utf8";
$q1 = $conn->query($setutf9);
$setutf7 = "SET COLLATION_CONNECTION = 'utf8_general_ci'";
$q2 = $conn->query($setutf7);
$sql = "SELECT id, name, email FROM myguests";
$rows = $conn->query($sql);
$arr1= array();
if ($rows->num_rows > 0) {
// output data of each row
while($row = $rows->fetch_assoc()) {
    $rcontent = " Name: " . $row["name"]. " - Email: " . $row["email"];  
    $arr1[]["title"] =  $rcontent;
}
} else {
     echo "0 results";
}
$conn->close();
// loop over the rows, outputting them
foreach($arr1 as $result1):
   fputcsv($output, $result1);
endforeach;

Excel???????????PHP?UTF-8?CSV #output

???

//Use tab as field separator   
$sep  = "\t";  
$eol  = "\n";    
$fputcsv  =  count($headings) ? '"'. implode('"'.$sep.'"', $headings).'"'.$eol : '';

//convert UTF-8 file without BOM to UTF-16LE for excel on mac
$fileUtf8String = file_get_contents("file.ext");
file_put_contents("file.ext", "\xFF\xFE" . mb_convert_encoding($fileUtf8String, "UTF-16LE", "UTF-8"));

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow