This is a very similar question to others asked, but I'm using Excel 2011 on a MAC and don't seem to be getting the desired result.
I have been exporting each sheet in an excel file manually as a windows comma separated CSV file (not MS DOS CSV) and using a bit of php my tables display perfectly on my site.
Using the code below in a Macro I can successfully export individual sheets as individual CSV files with one click. However, they don't have the .CSV extension at the end of the file name, and although the look okay with commas in the right place, when I upload them to my web site, they are displayed in one long row, instead of several rows.
Is this a MAC excel issue, or is there something that can be added to the below to fix?
Sub asdf()
Dim ws As Worksheet, newWb As Workbook
Application.ScreenUpdating = False
For Each ws In Sheets(Array("Sheet1", "Sheet2"))
ws.Copy
Set newWb = ActiveWorkbook
With newWb
.SaveAs ws.Name, xlCSV
.Close (False)
End With
Next ws
Application.ScreenUpdating = True
End Sub
Try building the extension into your filename:
.SaveAs ws.Name & ".csv", xlCSV
When you check the original file listing in the Finder, it reports "Simple Text Document", when you add the .csv extension it reports "comma-separated values" as the file type (Kind). Maybe this will fix your browser import issue?
If not an option may be to write out your data cell-by-cell. This similar post may be helpful to you.
Related
So, I have the following scenario.
I am working on a system for academical papers. I have several inputs that are for stuff like author name, coauthors, title, type of paper, introduction, objectives and so on. I store all that information in a database. The user has a Preview button which when clicked, generates a Word asynchronously and sends the file location back to the user and that file is afterwards shown to the user in an iframe using Google Doc Viewer.
There's a specific use case where the user/author of the paper can attach a .docx file with a table, or a .jpeg file for a figure. That table/figure has to be included inside the final .docx file.
For the .docx generation process I am using PHPWord.
So up until this point everything works fine, but my issues start when I try to mix everything and put together the .docx file.
Approach Number One
My first approach on doing this was to do everything with PHPWord. I create the file, add the texts where required and in the case of the image just insert the image and after that the figure caption below the image.
Things get tricky though, when I try doing the same thing with the .docx table file. My only option was to get the table XML using this. It did the trick, but the problem I ran into was that when I opened the resulting Word file, the table was there, but had lost all of its styling and had transparent borders. Because of those transparent borders, afterwards when converting it to PDF the borders were ignored and the table info is just scrambled text.
Approach Number Two (current one)
After fighting with Approach Number One and just complicating stuff more, I decided to do something different. Since I already generated one docx file with the main paper information and I needed to add another docx file, I decided to use the DocX Merge Library.
So, what i basically did was I have three generated word files, one for the main paper information, one for the table and one for the table caption (that last one is mainly to not overcomplicated the order of information). Also, that data is not in the table .docx file.
Then I run this:
$dm->merge( [
'paper-info.docx',
'attached-table.docx',
'attached-table-caption.docx'
], 'complete-file.docx');
So, afterwards, I check and the Word file is generated just as I need it with the table maintaining its original styles and dimensions.
If I open it in LibreOffice though, I get this error message:
Then if I continue and open the file, the file opens correctly with all the data with the only exception that it no longer respects the fonts of the file as they appear in Word.
So, the problem comes in the next step. Since I need to present a preview of the file using Google Doc Viewer using this syntax:
<iframe src="https://docs.google.com/gview?embedded=true&hl=es_LA&url=https://usersite.net/complete-file.docx?pid=explorer&efh=false&a=v&chrome=false&embedded=true" width="100%" height="600" style="border: none;"></iframe>
The document gets loaded fine, but when I review it what I see is that it only shows the content of the first paper-info.docx file and ends right where the table and table caption should appear. I open the exact same file in Word and it shows the table and caption.
The other issue is when I try to convert the file to PDF.
If I use PHPWord's method of conversion in combination with DomPDF I get the exact same issue as with the Google Docs Viewer, I just have the content of the first file, using this code:
$phpWordPDF = \PhpOffice\PhpWord\IOFactory::load('complete-file.docx');
$xmlWriterPDF = \PhpOffice\PhpWord\IOFactory::createWriter($phpWordPDF, 'PDF');
$xmlWriterPDF->save('complete-file-pdf');
So my only other viable route was to use LibreOffice's command line using this command:
soffice --headless --convert-to pdf complete-file.docx
This converts the file correctly, but has the issue mentioned when trying to open the .docx file in LibreOffice, the font styles are disconfigured.
Also weird part is that if I try to run this in my PHP script:
shell_exec('soffice --headless --convert-to pdf complete-file.docx');
Nothing happens.
I am running Apache 2.4.25, PHP 7.4.11 on Windows 10 x64.
Conclusion
Until now my best result was by merging the files, but it also caused this issue. So maybe the issue is coming from the merging process I am using. What would be ideal is to be able to just insert the table with styles and everything using PHPWord, but I haven't been able to and haven't found any examples on how to do that.
Another option that I've seen is this library, but the merge features is only in the license that's $599 USD, and since I am pretty close to solving this, I am not sure if it would solve my issue. If it does, I'd invest in it since I need to get this done ASAP, but I wanted to check with you guys what your recommendations would be for this case. Maybe another merging library or doing everything via PHPWord.
Help is appreciated!
After a lot of attempts to fix it, I wasn't able to achieve what I wanted with PHPWord and the merging library I mentioned.
Since I needed to fix this I decided to invest in the paid library I mentioned in my question. It was an expensive purchase, but for those who are interested, it does exactly what was required and it does it perfectly.
The two main functions I required were document merging and importing of content to a .docx file.
So I had to purchase the Premium package. Once there, the library literally does everything for you.
Example for docx files merge code:
require_once 'classes/MultiMerge.php';
$merge = new MultiMerge();
$merge->mergeDocx('document.docx', array('second.docx', 'other.docx'), 'output.docx', array());
Example for how to import a table from another docx file
require_once 'classes/CreateDocx.php';
$docx = new CreateDocxFromTemplate('document.docx');
// import tables
$referenceNode = array(
'type' => 'table',
);
$docx->importContents('document_1.docx', $referenceNode);
$docx->createDocx('output');
As you can see it is pretty easy. This answer is by no means an ad for this library, but for those that have the same problem as me, this is a life saver.
I wonder how to download some .csv file by using SAS.
Browsing on the web, I found it is possible to do that by running the following script:
filename NAME url "http://.../NAME_OF_THE_FILE.csv"
Particularly, I want to understand how such statement works and in which case I cannot use that.
For instance, let's assume one has to download a .csv file that is uploaded on a web page, as, for example, in the web site, where one can find football match data available.
In such case case, by using the following script to download the file:
filename csv url "http://www.football-data.co.uk/mmz4281/1617/E0.csv";
and the following one to import data in SAS:
proc import file = csv
out = junk_00
dbms = csv replace;
delimiter = ",";
run;
everything works fine. This file corresponds to the season 2016/2017 and contains the Premier League data, one can find on the first link.
Instead, in the case of the championship data for the 2016/2017 season, by using the same script as follows:
filename csv url "http://www.football-data.co.uk/mmz4281/1617/E1.csv";
proc import file = csv
out = junk_00
dbms = csv replace;
delimiter = ",";
run;
you get the following error:
Import unsuccessful. See SAS Log for details.
Browsing at the LOG window you can see among the LOG lines the following note/warning:
Invalid data for Date, even if the file is formatted correctly.
I don't understand the reason sometimes the script works and sometimes not, since this happened with other file, although the file are not corrupted and formatted correctly and in the same way.
What's wrong?
Can someone help me to understand why this happens?
Thanks all in advance!
Proc Import has to guess at data types. For some reason it thinks the date field is formatted as MMDDYY, but it's actually DDMMYY. Or maybe it's used inconsistently, I didn't check all, but could see the source of the error immediately.
The solution is to not use PROC IMPORT but to use a data step. If all the files are structured the same, then this works as a solution, but if each file is different then it's not a feasible solution.
Another possible workaround, is to download the data and then set GUESSINGROWS to a large number and then read the files. It will read all the values before guessing at the types so it can be better. This solution does not appear to work when using filename URL, but I don't know why.
I don't think this is a full answer, but it should shed some light on what's happening for you.
i have a html dynamic html table, i want to download this table as excel file.And after downloading i could make some changes in it and again upload it to a folder where it would change the database data according to fields..I was able to download the html table to an excel file, but have no idea how after changing the contents in the excel file would update the database according to it.I have heard of tongue twisters but this nerve twisting.
You may want to take a look at the PHPExcel library, available at phpexcel.codeplex.com.
If you want to do this with JS (I not recommend) you could copy/paste the excel content to a textarea in your HTML and then with JS you can do:
var temp = document.getElementById("yourtextarea").value.split(" ");
Since excel by default separates columns with '(tab)' you can explode it and save in the DB. You will also have to explode the newlines. It could be \r\n or just \n depending on your OS. After doing so, you can commit it to the database, row by row using Ajax.
If you can do it with PHP then would be much more easier. You could you explode() using newline at first (to break lines) and tab at finally (to break columns).
I am looking for a way to open a CSV file, that was created with a PHP script, in Excel - in such a way that Excel knows how to parse the file. Currently when i double click on a CSV file created with PHP, Excel opens the content in a single column so it does not parse each line. Also, if i do CTRL-O in Excel and select the CSV file to be opened, Excel launches a wizard where i am able to select parsing and encoding option.
Are there any 'headers' or flag characters that i could prepend to the CSV output in PHP to let Excel know how to open a file? I know, for example, that in order for Excel to handle UTF8 encoding, a U+FEFF character needs to be included as the first character in the CSV file, so maybe there is something similar for parsing?
Thanks.
Beware that depending on your xcel version you will or will not have the csv options dialog in Excel when opening direclty the csv file.
latest versions of Excel I've tested needs a special menu usage to get this dialog.
So you should provides what Excels wants.
And what he wants is a tab-separated csv (not comas, funny enough when he save a csv file he use comas but not in his auto-import), without " and without carriage returns in cells, and not in utf8.
Some says he need some sort of UTF16, I can't remember exactly, certainly the UTF-32LE BOM cited by Mark Baker. You will certainly have to transcode your chars.
Then do not forget to set tes text/csv mime type header.
When I see this broken auto-import csv without dialog of the new Excel I wonder if they didn't want to avoid complelty csv usage :-)
Ho, and I saw somewhere in past that there some mysterious formating commands you can use in an pure HTML table export that Excel will understand really better than the csv format.
You should search a lttle about it, maybe really simplier.
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.