Parsing Excel XML with PHP - php

INTRODUCTION
I'm using excel downloads as a way of users downloading a score sheet,
filling out scores and then re-uploading this score sheet into the
system.
Part of this requires the data in the database to be put into a CSV.
Then its opened on the client computer.
PROBLEM
The problem I'm having is that the CSV does not allow me to use any
kind of formatting. This includes hiding cells, making text bold etc.
So parsing the data in and out of an excel format (xls) document would
make more sense... But from what I've heard and read, this would be
really difficult because of different excel versions etc etc.
QUESTION
I heard that XML is a good way of parsing data into an excel format.
I wondered if anyone had any experience of this using php?
Is there a good script anyone has heard of?
Does anyone have any comments on different versions of excel opening
ir (or even, things like open office or lotus)
What do you think if my best step forward?
Thanks very much for your help!
Either reply or email me
tom at candid sky dot com

Try out PHP Excel (not sure if both links are the same) and PHP Excel Reader.

I'm sure there are, for example, Perl modules which would help with XML, but consider also that an HTML table: <tr>, <td>, <th> tags will be opened by Excel and at least some of the formatting will translate to Excel
you just have to name it ... something.xls

Related

how would one go about converting HTML table to Spreadsheet with JavaScript and/or PHP? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I export tables to excel from a webpage
EDIT: Just to clarify, it does not HAVE to be an Excel file, but it has to be a spreadsheet file which is able to be opened/edited in Excel.. So whatever would accomplish this most easily is the answer I seek :)
I need to convert an HTML table to some sort of downloadable spreadsheet (preferably for Office Excel). I can do this in jQuery, but would prefer to do it in plain JavaScript. I've searched around and have found a lot of info about doing the reverse (spreadsheet->html), but I need to create a downloadble spreadsheet file of a dynamically built table. Any points for how I could accomplish this would be much appreciated! :)
Don't do this in JavaScript. You need to do this in a server-side scripting language like PHP.
The easiest way to prepare data for importing into a spreadsheet is going to be creating a CSV file. Here's an example CSV file which will open fine in Excel:
column_a,column_b
1,2
5,7
8.988,abcdef
CSV files are just plain text, with the fields separated by commas, so they are easy to create. If the data in your fields needs to contain commas, double quote marks, newlines, or a few other special cases, then things get more tricky.
If you really want to create an Excel-format spreadsheet, this is pretty difficult and you're going to want the help of a library. Here's how I would find a PHP Excel library: http://google.com/search?q=php+create+excel+spreadsheet
You can't do this with JavaScript because it cannot create a downloadable file.
You must do this on the server. Any HTML page that is a table will automatically be converted BY EXCEL ITSELF if you send out the correct MIME header before streaming out the HTML file.
You can't do it all in Javascript. You can write a jQuery function to convert a table to something like a CSV format, but you won't be able to tell the browser to let the user download it. You'll have to POST it to a server which can then serve back the file.
See this question for details.
I can be mistaken, but it may be possible using data: URI scheme
<a href="data:text/csv;charset=utf8;base64,...encoded_data...">
download as Excel
</a>
It is not cross-browser, but can help.

PHP Html to DOC , XLS and ODF convertor

Hi everybody
I'm using CKeditor to edit and generate some reports for my php project. But I need to export the edited texts from CkEditor to Open office and Microsoft word format. So what I need is a class or component which has a function like these :converttoodf($htmlcode) converttodoc($htmlcodes) which return odf and doc format.
However, The doc and Odf file should be without problem , and the pictures and tables should be shown nicely.
In addition, No matter if yr solution be commercial, I may buy it cause I really need it urgently
Tnx in advance and hope can get the right answer asap , here
XLS:
http://docraptor.com/ (automatic conversion from HTML)
or
http://phpexcel.codeplex.com/ (manual building of file, just like phpword from another answer)
DOC
You can get away with just saving the HTML with inline css styles applied and saving it as a .doc. Not sure about how to display images there though.
ODT
http://incubator.apache.org/zetacomponents/documentation/trunk/Document/tutorial.html
more info: http://www.slideshare.net/tobyS/presentation-5028656
You might want to take a look at phpword... i know it supports html documents but im not sure if you can jsut load some source and save it out - you may have to build the document programatically.
If you need ODT anyway, then I'd recommend using OpenOffice via commandline. There is http://www.artofsolving.com/opensource/pyodconverter which can convert between pretty much all Office formats.
HTML import shouldn't be a problem. Don't be foiled by the PDF example. Input and output formats can vary. http://www.oooninja.com/2008/02/batch-command-line-file-conversion-with.html

looking for advise on importing excel into mysql with php

Alright, see if I can pick your brains from you all.
I'm currently working on a project where all the information comes from different clients, the only thing in common is that the received data is done with excel. The excel spread sheet that they present is just a bunch of references and codes, and the problem than I'm facing is that I need the references and codes to be entered in certain format in order for the website to work.
The perfect situation will be to go to each client and teach how I would need the data, but I can't do that because of the large number of clients, and more importantly I will be interrupting their work flow. Each client has its own codes and reference model and they are not willing to change their process
The good news is that there is a standard pattern for the codes, but I'm talking close to 200 thousand codes with a bunch of combination.
They way that we are currently solving the problem is that we have a person who checks each excel sheet received, runs a few macros, and manually fixes those codes in which the macro was not able to fix.
The person that is doing this, is already burn out and frustrated and I would like to automatize this process with php.
Suggestions?
There's a nice class called PHPExcel that allows you to write and read from a variety of document types:
http://www.codeplex.com/PHPExcel
You need to enable the php_zip extension for Excel 2007 documents
you can use dbTube.org to solve your problem. You can configure the mapping
in graphical editor. The import definition can be saved and reused. It is
not neccessary to change the excel sheet.
Greetings Andreas

CSV files and multi line text cells

I am generating a simple csv file using php. The file contains some user's personal data.
When I open the generated file in office, the addresses are not displayed in full height. I have to double click on the cell for the address to be shown fully (in full width and height) otherwise I can only see the first word/number of the address.
Also, I have date of births displayed as ######, I have to expand the whole column to see them fully.
This doesn't happen in open office.
Is there any way to force MS Office to show all fields in full? Because otherwise it'll be to confusing for the people who will use (Hey where are all the details!:)
Thanks :)
I don't think you can "format" your sheets with CSV. You will have to produce some other file format that Excel understands. I would suggest XML which is really easy to generate.
Just make a sample sheet with the data you want, save it as XML and you'll see how your file should be generated.
Or you could use some ready-made PHP solution for writing excel files if you can't be bothered with analysing the XML file.
you could try the auto-size columns feature.
This is a UI issue with how Excel works, you can't force Excel or anything else how they handle it.
The quickest work around is to perhaps create an XLS file that runs a macro to retrieve the CVS file and format the cells as needed, but there's nothing you can do inside the CSV to affect what Excel is displaying.

Advantage to parsing Excel Spreadsheet data vs. CSV?

I have tabulated data in an Excel spreadsheet (file size will likely never be larger than 1 mb). I want to use PHP to parse the data and insert in to a MySQL database.
Is there any advantage to keeping the file as an .xls/.xlsx and parsing it using a PHP Excel Parsing Library? If so, what are some good libraries to use?
Obviuously, I can save the .xls/.xlsx as a CSV and handle the file that way.
Thanks!
If you are just after the values, I would save it as a CSV. This is much easier to parse programatically, especially if you are trying to do this on a non-windows box.
That being said, there will be information lost in the export to CSV. It will only save the values of the cells - not their formatting information, formulas, etc. If you need to use that information, you're better off doing this straight from Excel.
Here is a PHP Excel Reading library. If you decide to read Excel files directly, this may help get you started.
If your excel files contain strictly data and contain no formulas, scripts, macros and etc., I would say parsing through Excel will only add development overhead, and will potentially slow down processing. It would probably be best to convert the files to CSV in this case.
Also consider that MySQL's 'LOAD DATA INFILE' command can be used to import entire CSV files into a table, this can potentially further uncomplicate matters for you.
when you provide a way for customers to upload excel/csv files, you should consider that
CSV files will only export one sheet
Having multiline cells will make the CSV parsing complicated
You cannot easily detect corrupted/incomplete CSV files
CSV files do not include formatting
Besides from that, importing CSV is a lot easier than importing XLS.
Remember that if you're importing the csv file directly into Mysql, that you may have problems with the date format (as Mysql uses a different date format to Excel). I find it easier to change the date fields in Excel first (to format yyyy-mm-dd) prior to saving as a csv file.
Edit: Although I've not used it myself, others have recommended Navicat as a very good tool for converting Excel spreadsheets or Access data into Mysql databases. May be worth a look.
With Office 2003 there's an XML format called SpreadsheetML which is a bit in-between XML and Excel. I've considered using this format to import/export data to a web site but the format turns out to be a bit complex. Internally, this format turns all references into relative references. (Relative from the current location.) Worse, some cells have an index, thus you might see a row with only two cells, but the second cell might be 6 columns away from the first cell. (In which case Index=5.) Basically, if you want to use the Excel format, you will need to have a good way to calculate the position of each cell and know how to translate the references in the cells in a proper way.
If you're onlyinterested in the data, CSV would be much, much easier to implement. As an in-between solution, you could define an XML schema and add an XML mapping to your spreadsheet to export the data to an XML file. It's more complex than CSV i9mport/export, but also a bit more robust. But the Excel or Excel XML formats themselves are horrible to implement. (Or just a nice challenge, if you're a real XML expert.)

Categories