mk-j / PHP_XLSXWriter

Lightweight XLSX Excel Spreadsheet Writer in PHP
MIT License
1.84k stars 665 forks source link

Excel found unreadable content again #279

Closed JaroslavDuska closed 3 years ago

JaroslavDuska commented 4 years ago

Hello,

I have problem with generate large (over 20k rows) xlsx file with customer prices from our e-shop. After download and open generated xlsx, MS Excel says "Excel found unreadable content in 'pricelist_20200525.xls'. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes.". Please, can anybody help me with solution, because I tried everything, what I found here and nothing works for me. Thanks a lot.

My code looks like this:

$filename = "pricelist_".date("Ymd").".xlsx";

// there is some unicode characters $header = array( 'Číslo zboží'=>'string', 'Název zboží'=>'string', 'MJ'=>'string', 'Základní cena bez DPH'=>'price', 'Moje cena bez DPH'=>'price', 'RP'=>'price', 'Dodavatel'=>'string', 'Kód dodavatele'=>'string', );

$rows = CustomerPriceList();

$writer = new XLSXWriter(); $writer->setTempDir(sys_get_temp_dir());

$writer->writeSheetHeader('Cenik', $header, $col_options = ['widths'=>[30,60,5,25,25,10,30,30], 'font'=>'Arial', 'font-size'=>11, 'font-style'=>'bold', 'fill'=>'#eee', 'halign'=>'center', 'valign'=>'center', 'freeze_rows'=>1, 'border'=>'left,right,top,bottom'], $row_options = ['height'=>20]); foreach ($rows as $row) { $writer->writeSheetRow('Cenik', $row, $row_options = ['height'=>20, ['font'=>'Arial', 'font-size'=>11, 'valign'=>'center', 'halign'=>'left'], ['font'=>'Arial', 'font-size'=>11, 'valign'=>'center', 'halign'=>'left'], ['font'=>'Arial', 'font-size'=>11, 'valign'=>'center', 'halign'=>'center'], ['font'=>'Arial', 'font-size'=>11, 'valign'=>'center', 'halign'=>'right'], ['font'=>'Arial', 'font-size'=>11, 'valign'=>'center', 'halign'=>'right'], ['font'=>'Arial', 'font-size'=>11, 'valign'=>'center', 'halign'=>'right'], ['font'=>'Arial', 'font-size'=>11, 'valign'=>'center', 'halign'=>'center'], ['font'=>'Arial', 'font-size'=>11, 'valign'=>'center', 'halign'=>'left']]); }

//$writer->writeToStdOut();

$xlsxcontent = $writer->writeToString();

header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"'); header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); header('Content-Transfer-Encoding: binary'); header('Content-length: '.strlen($xlsxcontent)); header('Cache-Control: must-revalidate'); header('Pragma: public');

echo $xlsxcontent;

exit(0);

uhlyarikp commented 3 years ago

Hi JaroslavDuska!

I see this is an old post, but if you haven’t found a solution yet, i will try to help.

I don't know what '$ rows' contains, but i think there is something wrong. For example, the 'price' column in the header can only be a number, without commas, spaces, letters. Try this sample array: $rows = array( array('CB12', 'Název zboží 1', 'MJ1', 12, 123, 147.6, 'Dodavatel 1', 'D12'), array('CB123', 'Název zboží 2', 'MJ2', 123, 1234, 1480.8, 'Dodavatel 2', 'D123'), array('CB1234', 'Název zboží 3', 'MJ3', 1234, 12345, 14814, 'Dodavatel 3', 'D1234') );

JaroslavDuska commented 3 years ago

Thanks for your reply. You are right, the problem was in bad input datas, thanks again.