How to Format XLS File From PHP

PHP is a programming language used by some dynamic websites to create specialized content. You can use it can be used to access user input, SQL databases and files on the server. PHP can be used to create an excel spreadsheet either on the web browser or save it to the server. The special PHP extension package Pear has a convenient class specifically for making excel files.

Things You'll Need

  • Wordpad or a similar text editor
Show More

Instructions

    • 1

      Open the PHP file with wordpad and located where in the code you wish to process the XLS file.

    • 2

      Include the 'Writer.php' file of the Pear Spreadsheet/Excel extension. This can be at the start of the file, or specifically where you are processing the XLS file. Insert the following code:

      require_once 'Spreadsheet/Excel/Writer.php';

    • 3

      Create a new object. I have used "$workbook" for the object's name, though it can be anything.

      Insert the following code:
      $workbook = new Spreadsheet_Excel_Writer();

      A file name can be included in the object's constructor to save the file to the server. In this case the file is "test.xls."

      Insert the following code:
      $workbook = new Spreadsheet_Excel_Writer('test.xls');

    • 4

      If you are going to display the spreadsheet in the web browser, the http headers must be set. The filename needs to be passed to the send function. It is "test.xls" in this case.

      Insert the following code:
      $workbook->send('test.xls');

    • 5

      Create a worksheet in the workbook using the addWorksheet() function.

      Insert the following code:
      $worksheet =& $workbook->addWorksheet('Worksheet1');

      'Worksheet1' is the name of the worksheet. It is a string, passed directly or as a variable.

    • 6

      Content can be written to the file using the write() function. It is performed on the worksheet, not the workbook object.

      Insert the following code:
      $worksheet->write('row', 'col', 'content');

      'row' and 'col' must be integers. 'content' must be a string. They can be passed directly or as variables. This line of code should be repeated for each cell containing content.

    • 7

      Close the file.

      Insert the following code:
      $workbook->close();

Tips & Warnings

  • Specific formatting of individual cells in the spreadsheet is accomplished using the Format class of functions. Each line of code should be on a separate line.

Related Searches:

References

Resources

Comments

You May Also Like

  • How to Read an XLS File in PHP

    PHP works often with databases and, as such, data files. Excel is commonly used in offices to store tables of data. However,...

  • How to Create an XLS File in PHP

    PHP is a programming language used for designing dynamic Web pages. It is a server-run language that creates an HTML-based page viewed...

  • How to Create an XLS Format From PHP

    The PHP scripting language commonly used to creating dynamic Web page and Web applications has the ability to create Excel spreadsheet, or...

  • How to Format XLS to CSV

    The XLS file format was the standard file format for Microsoft Excel files prior to Microsoft Excel 2007. You can still open...

  • How to Read an XLS File Using PHP

    Microsoft Excel is the normal program associate with the filetype XLS, and PHP is a much more widely used general purpose scripting...

  • How to Convert XLS to CSV With PHP

    PHP and databases go together. As such, PHP and Excel files have historically encountered compatibility issues. People often keep manually edited databases...

  • XLS to XML Format

    Converting XLS to XML format is frequently needed if you are going to upload your files to the Internet. There are many...

  • How to Format XLS Files

    Formatting an XLS (Microsoft Excel) file makes it simpler for you and your recipients to read, decipher and analyze the information. Instead...

  • XLS File Types

    XLS File Types. Microsoft Excel is a spreadsheet program that has gone through a number of enhancements with each new version. Occasionally,...

  • How to Open a File in PHP

    Reading from and writing to files is one of the most common programming tasks. You can't process data if you don't get...

  • How to Convert PDF to XLS Format

    A PDF file is useful for individuals who wish to transfer files without having to worry about any of the file's formatting...

  • What Is the File Extension PHP?

    PHP files, unlike other Web file formats, are executed entirely on the server, rather than on the user's computer through the browser....

  • How to Display XLS in HTML

    An XLS file, which is a file extension associated with Microsoft Excel, is not designed for display within an HTML file in...

  • How to Put a Checkbox in an XLS Cell

    Microsoft Excel has a number of useful options that fall outside of the basic purpose of entering and calculating data. You can...

  • How to Create XLS Files

    There are many different file extensions that you can use in the computing world. Each file extension is associated with a specific...

  • How to Save an XLS File as a PDF?

    XLS files are documents created in a spreadsheet program called Excel, which is manufactured by Microsoft. While it is a powerful program,...

  • How to Use Javascript With Excel

    The Javascript language provides developers with the tools to set dynamic content after a web page has loaded into the user's web...

  • How to Turn an XLS File Into a PDF File

    Microsoft Excel is the spreadsheet program that is part of the Office line of applications. Excel's main file extension is "XLS," and...

Related Ads

Featured