PHP Export code for Excel Word

Source: Internet
Author: User

How PHP is exported to Word

Generally, there are 2 ways to export doc documents, one is to use COM, and as an extension of PHP to install to the server, and then create a COM, call its methods. Servers that have installed Office can call a COM called word.application, which can generate a Word document, but I don't recommend it because the execution is inefficient (I tested it and the server actually opens a word client when executing the code). The ideal COM should be no interface, the data conversion in the background, so the effect will be better, but these extensions generally need to charge.

The 2nd method is to use PHP to export Word document content directly to a file suffix doc. Using this approach requires no reliance on third-party extensions and is more efficient to perform.

Word itself is very powerful, it can open HTML-formatted files, and can preserve the format, even if the suffix is doc, it can also recognize the normal open. This gives us the convenience. But there is a problem, the HTML format of the file in the image only one address, the real picture is saved elsewhere, that is, if the HTML format is written in Doc, then Doc will not contain the picture. So how do we create doc documents that contain images? We can use the MHT format that is close to HTML.

The MHT format is similar to HTML, except that in MHT format, externally linked files are encoded and stored by Base64, Javascript, and CSS. As a result, a single MHT file can hold all the resources in a Web page, and of course it will be larger in size than HTML.

Can the MHT format be recognized by word? I saved a webpage as MHT, then changed the suffix to Doc, opened it with Word, Ok,word also recognized the MHT file, and can display the picture.

Well, since Doc can recognize MHT, here's how to put the picture into MHT. Since the address of the image in the HTML code is written in the SRC attribute of the img tag, the image address can be obtained as long as the SRC attribute value in the HTML code is extracted. Of course, it is possible that you get a relative path, it doesn't matter, plus the URL prefix, change to absolute path. With the image address, we can get the specific contents of the picture file through the File_get_content function, and then call the Base64_encode function to encode the contents of the file into Base64 encoding, and finally insert into the appropriate location of the MHT file.

Finally, we have two kinds of PHP export Word document method to send the file to the client, one is to first generate a DOC document on the server side, and then record the address of the doc document, and finally, through the header ("Location:xx.doc"); You can have the client download the doc. Another is to send the HTML request directly, modify the header portion of the HTML protocol, set its content-type to Application/doc, set the content-disposition to attachment, followed by the file name, After the HTML protocol is sent, the contents of the file are sent directly to the client, and the client can be downloaded to the doc document.

<?
Header ("Content-type:application/octet-stream");
Header ("Accept-ranges:bytes");
Header ("Content-type:application/vnd.ms-excel");
Header ("Content-disposition:attachment;filename=export_excel_gshjsl.xls");

$tx = ' table header ';
echo $tx. " \ n ";
echo "Number". " \ t ";
echo "Name". " \ t ";
echo "\ n";

echo "=\" 411481198507150666\ "". " \ t ";
echo "=\" 0123456\ "". "            \ t "; Enclose the quotation marks for easy string output. Rather like formatting cells as text.
echo "\ n";
?>

By the way, the simple export Word document code to paste here:

<?php
Header ("Content-type:application/msword");
Header ("content-disposition:attachment; Filename=doc.doc ");
Header ("Pragma:no-cache");
Header ("expires:0");

$output = ' <table border= ' 1 "cellspacing=" 2 "cellpadding=" 2 "width=" 90% "align=" center ">";
$output. = ' <tr bgcolor= ' #cccccc "><td align=" center "> Pictures </td></tr> ';
$output. = ' <tr bgcolor= ' #f6f7fa "><td><span style=" color: #FF0000; " ><strong> below is a picture </strong></span></td></tr> ';
$output. = ' <tr><td align= ' center ' >http://zi.csdn.net/48260_2.gif "></td></tr>";
$output. = ' </table> ';


Echo $output;
?>

PHP Export code for Excel Word

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.