T
T
tokyodead2021-09-20 07:38:26
PHP
tokyodead, 2021-09-20 07:38:26

Does not support Russian, xls format?

Good afternoon, I need to automatically upload data from the database to excel, everything is unloaded for me, but the Russian language does not support, for example, there should be "Product_Code" and

there

$connection = mysqli_connect("localhost", "root", "", "dbname");

  $output = '';
  $sql = "SELECT * FROM `nomen`";
  $result = mysqli_query($connection, $sql);
  if(mysqli_num_rows($result) > 0) {
    $output .= '
      <table class="table" bordered="1">
        <tr>
          <th>Код_товара</th>
          <th>Цена</th>
          <th>Количество</th>
        </tr>
    ';
    while($row = mysqli_fetch_array($result)) {
      $output .= '
        <tr>
          <td>'.$row["Artikul"].'</td>
          <td>'.$row["Price"].'</td>
          <td>'.$row["Ostatok"].'</td>
        </tr>
      ';
    }
    $output .= '</table>';
    $fp = fopen("file.xls", "w");
    fwrite($fp, $output);
    fclose($fp);
  }


Tell me please

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
Ipatiev, 2021-09-20
@Fockker

Of course, Excel perfectly supports the Russian language. It just works on the old Windows operating system, like a mammoth’s excrement, where the encoding 1251 is still considered standard. And it thinks that the data always lies in it.
Plus, you are not generating xls, but HTML disguised as xls.
And because of this masking, Excel does not offer to change the encoding when importing.
Therefore, there are two ways
1. Save data as CSV. Then, when importing, you can choose the encoding.
2. Recode data before saving,

mb_convert_encoding($output, "windows-1251", "utf-8");

R
Rsa97, 2021-09-20
@Rsa97

Firstly, you are uploading not in xls, but in HTML. There are separate libraries for working with xls and xlsx files, for example https://github.com/PHPOffice/PhpSpreadsheet .
And the problem with your encoding is that Excel expects text in a single-byte encoding (for example, Windows-1251), and you transfer it in multibyte UTF-8.
Recode the text with iconv .

R
rPman, 2021-09-20
@rPman

Firstly, you create html and not excel, and the office can open such files and doesn’t even swear, but if you want to work with documents directly, work with .xlsx or .ods (the open document standard is supported everywhere and by everyone, this is the standard for workflow, use it better) - this is in fact an archive with text xml documents
In fact, set up the encoding with which you will communicate with the set_charset database, or change your encoding in the php code to the same that the database gives out (utf-8) and write in your html in the header

S
Slava Rozhnev, 2021-09-20
@rozhnev

Why do you need HTML generation. Upload to CSV file. Excel opens them just fine.

$con = mysqli_connect("localhost", "root", "", "dbname");

$result = mysqli_query($con, 'SELECT * FROM nomen');
$fp = fopen('file.csv', 'w');
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
    fputcsv($fp, $row);  
}
fclose($fp);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question