I'm using PHPExcel for transferring data between MySQL DB and Excel 2007 worksheets. It works well on most situations, but I encountered one problem.
Some of the fields in DB contain HTML data. I need to preserve the formatting in Excel cells as much as possible. As I could figure out, Excel allows the following formatting inside cells (PHPExcel_RichText class supports all of these): new lines [these can be used to track <p></p> blocks], font name, size, color, bold, italic, underline, strikethrough, subscript, superscript. Suppose these are enough, so we can ignore other HTML formatting.
What is the best (easiest, fastest) way to convert HTML data to Excel Rich Text and vice versa?
One solution I've in mind is to create a function that will traverse the HTML [using DOMDocument or so], place \n after block elements, create PHPExcel_RichText_Run objects for <b>, <i> etc, and ignore all other elements. I feel this will be quite "expensive", especially when dealing with nested structures, like <b>some <i>formatted<i> text</b>
Is there any better way to do this, with or without PHPExcel?
One more idea: I noticed that when exporting in XML Spreadsheet 2003 format the following appears inside XML:
<ss:Data ss:Type="String"
xmlns="http://www.w3.org/TR/REC-html40"><Font html:Color="#000000">this is </Font><B><Font
html:Color="#000000">some </Font><I><Font html:Color="#000000">formatted</Font></I><Font
html:Color="#000000"> text</Font></B></ss:Data>
which is normal HTML4. I mean it seems that Excel can understand plain HTML. So maybe there is some way to pass HTML directly to Excel without converting it to PHPExcel_RichText objects... (although note that it would be best if I'll be able to export to .xlsx format)
HTML to Rich-Text Runs is on the PHPExcel development roadmap for the coming year: however, the planned method was to use DOMDocument to parse the markup.
Any solution that we adopt for PHPExcel itself will have to use RichText Runs to provide consistency. While MS Excel itself can handle direct imports of Excel, and (as you've noted in the SpreadSheetML xml format offered by Excel 2003), this isn't consistent across the other different Excel formats (BIFF and OfficeOpenXML).
Related
I was wondering is there a way to convert a document(doc or docx), which contains images and text into a markdown.
Ex: Document contains an Image and description for that image
I was trying to convert that document into a markdown such as follows
<img src="doument_name/media/image1.png" width="624" height="505" />
Followed by description with markdown
When I search, I only found Markdown parser's, converters which convert text data into HTML
Doc and Docx are complex proprietary formats, and markdown is not widely used. Converting one to the other directly will be difficult. It's better to use HTML as the intermediate step.
There are many PHP solutions out there to read MS-docs, but, as you perhaps have found, they're all slightly flawed. They also don't do the conversion to HTML, they read, but don't convert, or they don't include images, etc.
As an alternative you could try an online API, like:
http://apiv2.online-convert.com/
I haven't tested this, but it could be a good solution: It converts to HTML, and you have to write, and maintain, very little code yourself.
The conversion from HTML to markdown is relatively easy, you can find examples online:
https://github.com/thephpleague/html-to-markdown
https://github.com/Elephant418/Markdownify
How can i format the excel report though php application
I am able to generate the excel report in php but how can i do the formatting changes in the excel.
eg. I have to add image in excel.
Red borders to columns.
some background color to excel
Change the font color
Help appriciated
I don't think it is directly possible without any libraries.
Have a look at PHPExcel, You will be able to do what you are looking for.
Credits to Mark Baker.
Tougher Way
You will need to generate XHTML from your Excel document, After that you need to search and parse for tags and do your formatting and stuff.
I have an excel spreadsheet that contains some formulas to calculate ROI for an upcoming product of a client. Is it possible to convert this into a php script whereby a user would type into some boxes and then submit it working out the same. The excel spreadsheet is here: http://www.solidcamxpress.co.uk/roi.xls
Sure, PHP can do math just like Excel's formulas. However, there is no magic method of converting Excel to a Web page, if that's what you're asking.
#Cameron
There is a software in the market which can convert the spreadsheet to active html/php with ease. You can get the software at the following url:
http://www.spreadsheetconverter.com
Even you can also convert the spreadsheet by yourself as it is simple to convert with scripts. but you can also this software for the conversion =)
You can use PHPExcel to read an existing spreadsheet and extract its data/formulas, but converting those formulas from Excel syntax to PHP is rather non-trivial.
Unless you're doing a generalized Excel->PHP converter, the time/effort required to write such a converter will be far greater than it would take you do analyze the one spreadsheet by hand and code up some PHP stuff to recreate it.
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
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.)