How to add data to an existing Excel file with PHP?

后端 未结 5 473
灰色年华
灰色年华 2021-01-22 14:17

I have a Excel file with forms and controls. As it is not possible to create a Excel file with forms, controls and VBA code from scratch using PHP, I thought I could upload my e

相关标签:
5条回答
  • 2021-01-22 14:33

    It's pretty easy to add with PHPExcel - load file, add data after last row, save file... unfortunately, forms and VBA code aren't supported.

    To retain those, you'll need to use COM, which restricts you to a Windows server with MS Excel installed.

    0 讨论(0)
  • 2021-01-22 14:39

    Think in the other direction!

    Instead of pushing data to excel from php, add a data connection to excel that will retrieve its data from a php endpoint taht publish xml.

    Don't know if it applies, but it's far more easier to implement.

    Moreover, you will have total control on the excel file, keeping all customisations, including VBA.

    And finally, you will avoid all messy COM object managing if you end up in this direction.

    [Edit] Steps for this solution :

    1. Build a php endpoint that publish the XML data (I have actually no php knowledge. Just ensure that calling yourdata.php will produce the data in XML).
    2. In excel, using a Data connection, insert the content of this XML file: Go to Data TAB (assuming Excel 2010), "From other source", "From Xml data import".
    3. let the wizard do the job

    After that, you will have a list in Excel, than is linked to your xml source. SImply right lick the list / refresh to get the latest data (or configure the data connection to auto refresh at opening).

    0 讨论(0)
  • 2021-01-22 14:43

    I don't think you can actually append data to an existing excel file.

    • Rather load all data from Excel into your array ( easy using PHPExcel );
    • Add data to your array ( format it as you want );
    • Create a new excel file and let user download it ( or save over the original );

    This will simulate append action.

    0 讨论(0)
  • 2021-01-22 14:49

    @SteveB Further to my comment and your response earlier.. pls read below.

    @SteveB Looks like an excellent idea.. but why use xml? i try to use xml but i don't know what format to write the xml in text file? can you explain. can we also just use .csv? as it looks easy to write a simple .csv file. How will the whole solution work.. say i am the user.. who clicks and opens a php page.. and where should the excel template be stored.. and how it will pop to user.. what happens when another user connected to same page, etc. I want to keep pivot tables, excel macros and vba, images and charts in the template file. – ihightower

    @ihightower: xml is easier to produces than CSV (at least using .Net and C#, which is my primary programming language), but this can works with CSV too I think. Providing the Excel template is easy... simply serve it as a static content, as the excel file is not dynamically generated. – Steve B


    My finidings..

    XML vs CSV

    CSV format for import is only good if Excel Table format is NOT required. You cannot have the alternate rows highlighting, column headers available during scroll, etc. in summary, for what i found you cannot have external data and table format at the same time.

    The error you will get converting the data connection table to "Format as table" is this:

     Your selection overlaps one or more external data ranges.
     Do you want to convert the selection to a table and remove
     all external connection?
    

    Of course, I do not want that.. but using xml format for import data connection... the table format can stay like this screenshot:

    xml data imported from file to Excel

    And, when you scroll past one screen full.. the screen will look like this.. you can see the column headers A, B, C is automatically converted to the data header (ISBN, date, Title, ...)

    scrolling the connected xml data past one screenfull

    So, we should go for an xml solution.. which we are figuring out how using php. But, using CSV for testing purposes with about 10 columns x 20,000 rows data.. it works very nicely. The refresh from web server also works very well.

    Put All Piece Togeter

    I have had an internal discussion with my team and we think this will really work for us. Now, we are brainstorming on the delivery and flow of user actions to deliver the Excel template reports with data which he generated from php.

    Ok.. What we have in start?

    • An Excel Template (e.g. staff_analysis_template.xlsm). This will will have all the pivot table, excel macros, buttons, images, charts, everything an excel file can have in there.
    • In the Template file there will be one sheet named "data". This is where the xml or csv import will end up being... readily available for all the pivot tables and charts in the file.
    • A php webpage which will have list boxes and text fields for the user to filter on the data.. and he will click search... and this will provide a web page result and and a link to view the same data in the excel template file with all the resulting data in sheet: data.

    Here is the Excel Template with sample data.. in this screenshot... you can see

    1. Sheet: data where the xml data generated from php will be connected and end up in.
    2. Sheet: summary. where all the formulas in Excel can be used... and presented to the user which is based on the data.
    3. Sheeet: Pivot Can have pivot tables and charts which is based on the data and can be made to automatically refresh when the file is opened..
    4. VBA is also available.. for example the button can be used to refresh the data or to do any other work.

    We can basicially use everything Excel has available for us. The resulting xml file can be left on the webserver.. and also data connection can be an URL also or as a file.

    sourceFile="http://xxxxx/test_data3.csv" or

    sourceFile="C:\Users\Fiaz\Documents\Magic Briefcase\Learn\excel\test_data3.csv"

    enter image description here

    Need to figure out

    The part that are yet to be figured out is how to connect this all together with seamless user experience.. that is the user should not be forced to save the file at first or manually connecting the xml etc.. The excel template must just open right there with the required data and all the analysis available. If user need to save it he will use save as and copy to local machine.

    The idea we have for this seamless user experience is like this:

    1. generate unique xml data file (e.g. YYYYMMDDHHMMSS_username_pagename.xml) and put it on some tmp server folder.
    2. copy the template file to a tmp server folder with update connection of the xml file (for this we need to unzip the .xlsx or .xlsm file), update the connection info in (xl/connections.xml) with the file name and folder: YYYYMMDDHHMMSS_username_pagename.xml then zip it and deliver to user through web as .xlsm or .xlsx

    I tried it manually step by step and it really worked.

    Will be much appreciated for any ideas on this to make it really better and still keep it simple.

    Feedback required from PHPExcel author @MarkBaker:

    @MarkBaker: From reading through your comments in other posts for PHPExcel, I understand that we CANNOT do the same using PHPExcel (that is to use the original excel template as it is). So, the solution suggested by SteveB and elaborated by me above seems to be much more powerful for this purpose. We can keep the original excel template file in tact with ALL THE ASSETS (pivot table, vba, etc.) with in it as it is. The server need not have Excel as well.

    So, can we expect this feature to be available with PHPExcel itself sometime in the future. Then we can be more confident in using the standard technique from the PHPExcel guru himself.

    in my opinion PHPExcel is very useful for invoice type of reports and many others but not for business intelligence type of analysis... which requires the use of pivot tables and complicated charts and code, etc.

    Please make your comments.. I will be very interested to see it from your view on how it can be made better.

    Make everything as simple as possible, but not simpler. Albert Einstein

    0 讨论(0)
  • 2021-01-22 14:51

    OpenTBS is a PHP class PHP, that enables you to build an XLSX (or XLSM, that is XLSX with VBA macros) documents with the technical of templates.

    It's very easy to populate a worksheet. You can also edit forms and VBA since you know their XML structure.

    By the way OpenTBS also supports DOCX, PPTX, ODT, ODS, ODP, ...

    • Demo
    • Doc
    0 讨论(0)
提交回复
热议问题