PHP XLSXWriter - 不同的格式
PHP XLSXWriter - Different Formatting
我正在使用 PHP_XLSXWriter 代码生成 XLSX 文件。文件生成正常,打开 Excel 正常,但我正在使用此文件上传到 DHL 服务器以生成发货单。问题是,他们的服务抱怨无效行。该行中的数据实际上非常好,Excel 可以像我提到的那样读取它。然而,作为测试,我所做的是将 PHP 生成的 spreadsheet 保存为一个单独的文件,从而使 Microsoft Excel 保存为它自己的格式。当我将 Microsoft sheet 上传到 DHL 服务器时,它随后毫无问题地接受了它。
在对两者进行十六进制比较后,它们完全不同,这让我相信 PHP 生成的 XLSX 文件使用的是 DHL 未使用的 OpenOffice 或 LibreOffice 格式。
有什么办法可以解决这个问题吗?
我的一个想法是放弃 PHP XLSXWriter 功能,将数据保存为带有制表符分隔数据的原始文本文件(DHL 也接受)。
这是我的代码中主要功能的一个简短示例:
$filename = "test.xlsx";
header('Access-Control-Allow-Origin: mysite.com');
header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
$rows = array(
array('Pick-up Account Number', 'Sales Channel', 'Shipment Order ID', 'Tracking Number', 'Shipping Service Code', 'Company', 'Consignee Name', 'Address Line 1', 'Address Line 2', 'Address Line 3', 'City', 'State', 'Postal Code', 'Destination Country Code', 'Phone Number', 'Email Address', 'Shipment Weight (g)', 'Length (cm)', 'Width (cm)', 'Height (cm)', 'Currency Code', 'Total Declared Value', 'Incoterm', 'Freight', 'Is Insured', 'Insurance', 'Is COD', 'Cash on Delivery Value', 'Recipient ID', 'Recipient ID Type', 'Duties', 'Taxes', 'Workshare Indicator', 'Shipment Description', 'Shipment Import Description', 'Shipment Export Description', 'Shipment Content Indicator', 'Content Description', 'Content Import Description', 'Content Export Description', 'Content Unit Price', 'Content Origin Country', 'Content Quantity', 'Content Weight (g)', 'Content Code', 'HS Code', 'Content Indicator', 'Remarks', 'Shipper Company', 'Shipper Name', 'Shipper Address1', 'Shipper Address2', 'Shipper Address3', 'Shipper City', 'Shipper State', 'Shipper Postal Code', 'Shipper CountryCode', 'Shipper Phone Number', 'Shipper Email address', 'Return Company', 'Return Name', 'Return Address Line 1', 'Return Address Line 2', 'Return Address Line 3', 'Return City', 'Return State', 'Return Postal Code', 'Return Destination Country Code', 'Return Phone Number', 'Return Email Address', 'Service1', 'Service2', 'Service3', 'Service4', 'Service5', 'Grouping Reference1', 'Grouping Reference2', 'Customer Reference 1', 'Customer Reference 2',),
array('123456789', '', $order_id, '', 'PPS', $order->shipping_company, $order->shipping_first_name.' '.$order->shipping_last_name, $order->shipping_address_1, $order->shipping_address_2, $order->shipping_address_3, $order->shipping_city, $order->shipping_state, $order->shipping_postcode, $order->shipping_country, $order->billing_phone, $order->billing_email, '160', '18', '13', '6', 'AUD', '70', '', '', '', '', '', '', '', '', '', '', '', 'Printer Cartridge', '', '', '', 'Printer Cartridge', '', '', '70', 'AU', '1', '', 'TEST_2214', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',),
);
$writer = new XLSXWriter();
$writer->setAuthor('TEST');
foreach($rows as $row)
$writer->writeSheetRow('Sheet1', $row);
$writer->writeToStdOut();`
请注意,DHL 服务器似乎在抱怨包含 'PPS' 的行中的数据。
谢谢。
通过编写制表符分隔文件解决:
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=example.txt");
header("Pragma: no-cache");
header("Expires: 0");
$rows = array(
array('Pick-up Account Number', 'Sales Channel', 'Shipment Order ID', 'Tracking Number', 'Shipping Service Code', 'Company', 'Consignee Name', 'Address Line 1', 'Address Line 2', 'Address Line 3', 'City', 'State', 'Postal Code', 'Destination Country Code', 'Phone Number', 'Email Address', 'Shipment Weight (g)', 'Length (cm)', 'Width (cm)', 'Height (cm)', 'Currency Code', 'Total Declared Value', 'Incoterm', 'Freight', 'Is Insured', 'Insurance', 'Is COD', 'Cash on Delivery Value', 'Recipient ID', 'Recipient ID Type', 'Duties', 'Taxes', 'Workshare Indicator', 'Shipment Description', 'Shipment Import Description', 'Shipment Export Description', 'Shipment Content Indicator', 'Content Description', 'Content Import Description', 'Content Export Description', 'Content Unit Price', 'Content Origin Country', 'Content Quantity', 'Content Weight (g)', 'Content Code', 'HS Code', 'Content Indicator', 'Remarks', 'Shipper Company', 'Shipper Name', 'Shipper Address1', 'Shipper Address2', 'Shipper Address3', 'Shipper City', 'Shipper State', 'Shipper Postal Code', 'Shipper CountryCode', 'Shipper Phone Number', 'Shipper Email address', 'Return Company', 'Return Name', 'Return Address Line 1', 'Return Address Line 2', 'Return Address Line 3', 'Return City', 'Return State', 'Return Postal Code', 'Return Destination Country Code', 'Return Phone Number', 'Return Email Address', 'Service1', 'Service2', 'Service3', 'Service4', 'Service5', 'Grouping Reference1', 'Grouping Reference2', 'Customer Reference 1', 'Customer Reference 2',),
array('123456789', '', $order_id, '', 'PPS', $order->shipping_company, $order->shipping_first_name.' '.$order->shipping_last_name, $order->shipping_address_1, $order->shipping_address_2, $order->shipping_address_3, $order->shipping_city, $order->shipping_state, $order->shipping_postcode, $order->shipping_country, $order->billing_phone, $order->billing_email, '160', '18', '13', '6', 'AUD', '70', '', '', '', '', '', '', '', '', '', '', '', 'Printer Cartridge', '', '', '', 'Printer Cartridge', '', '', '70', 'AU', '1', '', 'TEST_2214', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',),
);
foreach ($rows as $arr)
{
$row = implode("\t", $arr);
$row.="\n";
echo $row;
}
这会生成正确的数据并且 DHL 会接受它。谢谢
我正在使用 PHP_XLSXWriter 代码生成 XLSX 文件。文件生成正常,打开 Excel 正常,但我正在使用此文件上传到 DHL 服务器以生成发货单。问题是,他们的服务抱怨无效行。该行中的数据实际上非常好,Excel 可以像我提到的那样读取它。然而,作为测试,我所做的是将 PHP 生成的 spreadsheet 保存为一个单独的文件,从而使 Microsoft Excel 保存为它自己的格式。当我将 Microsoft sheet 上传到 DHL 服务器时,它随后毫无问题地接受了它。
在对两者进行十六进制比较后,它们完全不同,这让我相信 PHP 生成的 XLSX 文件使用的是 DHL 未使用的 OpenOffice 或 LibreOffice 格式。
有什么办法可以解决这个问题吗? 我的一个想法是放弃 PHP XLSXWriter 功能,将数据保存为带有制表符分隔数据的原始文本文件(DHL 也接受)。
这是我的代码中主要功能的一个简短示例:
$filename = "test.xlsx";
header('Access-Control-Allow-Origin: mysite.com');
header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
$rows = array(
array('Pick-up Account Number', 'Sales Channel', 'Shipment Order ID', 'Tracking Number', 'Shipping Service Code', 'Company', 'Consignee Name', 'Address Line 1', 'Address Line 2', 'Address Line 3', 'City', 'State', 'Postal Code', 'Destination Country Code', 'Phone Number', 'Email Address', 'Shipment Weight (g)', 'Length (cm)', 'Width (cm)', 'Height (cm)', 'Currency Code', 'Total Declared Value', 'Incoterm', 'Freight', 'Is Insured', 'Insurance', 'Is COD', 'Cash on Delivery Value', 'Recipient ID', 'Recipient ID Type', 'Duties', 'Taxes', 'Workshare Indicator', 'Shipment Description', 'Shipment Import Description', 'Shipment Export Description', 'Shipment Content Indicator', 'Content Description', 'Content Import Description', 'Content Export Description', 'Content Unit Price', 'Content Origin Country', 'Content Quantity', 'Content Weight (g)', 'Content Code', 'HS Code', 'Content Indicator', 'Remarks', 'Shipper Company', 'Shipper Name', 'Shipper Address1', 'Shipper Address2', 'Shipper Address3', 'Shipper City', 'Shipper State', 'Shipper Postal Code', 'Shipper CountryCode', 'Shipper Phone Number', 'Shipper Email address', 'Return Company', 'Return Name', 'Return Address Line 1', 'Return Address Line 2', 'Return Address Line 3', 'Return City', 'Return State', 'Return Postal Code', 'Return Destination Country Code', 'Return Phone Number', 'Return Email Address', 'Service1', 'Service2', 'Service3', 'Service4', 'Service5', 'Grouping Reference1', 'Grouping Reference2', 'Customer Reference 1', 'Customer Reference 2',),
array('123456789', '', $order_id, '', 'PPS', $order->shipping_company, $order->shipping_first_name.' '.$order->shipping_last_name, $order->shipping_address_1, $order->shipping_address_2, $order->shipping_address_3, $order->shipping_city, $order->shipping_state, $order->shipping_postcode, $order->shipping_country, $order->billing_phone, $order->billing_email, '160', '18', '13', '6', 'AUD', '70', '', '', '', '', '', '', '', '', '', '', '', 'Printer Cartridge', '', '', '', 'Printer Cartridge', '', '', '70', 'AU', '1', '', 'TEST_2214', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',),
);
$writer = new XLSXWriter();
$writer->setAuthor('TEST');
foreach($rows as $row)
$writer->writeSheetRow('Sheet1', $row);
$writer->writeToStdOut();`
请注意,DHL 服务器似乎在抱怨包含 'PPS' 的行中的数据。
谢谢。
通过编写制表符分隔文件解决:
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=example.txt");
header("Pragma: no-cache");
header("Expires: 0");
$rows = array(
array('Pick-up Account Number', 'Sales Channel', 'Shipment Order ID', 'Tracking Number', 'Shipping Service Code', 'Company', 'Consignee Name', 'Address Line 1', 'Address Line 2', 'Address Line 3', 'City', 'State', 'Postal Code', 'Destination Country Code', 'Phone Number', 'Email Address', 'Shipment Weight (g)', 'Length (cm)', 'Width (cm)', 'Height (cm)', 'Currency Code', 'Total Declared Value', 'Incoterm', 'Freight', 'Is Insured', 'Insurance', 'Is COD', 'Cash on Delivery Value', 'Recipient ID', 'Recipient ID Type', 'Duties', 'Taxes', 'Workshare Indicator', 'Shipment Description', 'Shipment Import Description', 'Shipment Export Description', 'Shipment Content Indicator', 'Content Description', 'Content Import Description', 'Content Export Description', 'Content Unit Price', 'Content Origin Country', 'Content Quantity', 'Content Weight (g)', 'Content Code', 'HS Code', 'Content Indicator', 'Remarks', 'Shipper Company', 'Shipper Name', 'Shipper Address1', 'Shipper Address2', 'Shipper Address3', 'Shipper City', 'Shipper State', 'Shipper Postal Code', 'Shipper CountryCode', 'Shipper Phone Number', 'Shipper Email address', 'Return Company', 'Return Name', 'Return Address Line 1', 'Return Address Line 2', 'Return Address Line 3', 'Return City', 'Return State', 'Return Postal Code', 'Return Destination Country Code', 'Return Phone Number', 'Return Email Address', 'Service1', 'Service2', 'Service3', 'Service4', 'Service5', 'Grouping Reference1', 'Grouping Reference2', 'Customer Reference 1', 'Customer Reference 2',),
array('123456789', '', $order_id, '', 'PPS', $order->shipping_company, $order->shipping_first_name.' '.$order->shipping_last_name, $order->shipping_address_1, $order->shipping_address_2, $order->shipping_address_3, $order->shipping_city, $order->shipping_state, $order->shipping_postcode, $order->shipping_country, $order->billing_phone, $order->billing_email, '160', '18', '13', '6', 'AUD', '70', '', '', '', '', '', '', '', '', '', '', '', 'Printer Cartridge', '', '', '', 'Printer Cartridge', '', '', '70', 'AU', '1', '', 'TEST_2214', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',),
);
foreach ($rows as $arr)
{
$row = implode("\t", $arr);
$row.="\n";
echo $row;
}
这会生成正确的数据并且 DHL 会接受它。谢谢