I'm generating a csv file that looks like:
column1,column2,column3
hello,02,some comments
hello,AF,some comments
hello,15,some comments
hello,08,some comments
hello,FF,some comments
When opening into Excel, the second columns will convert automatically 02 to 2 and 08 to 8.
Recommended workaround is to tweak settings in Excel or specifying format of each columns during the file loading. (time-consuming, not user-friendly)
Another workaround for file itself, would be to append quotes around the concerned values, so they would be treated as strings.
(for some reasons, I can't do this, as the file I generate, must reflect the original source one)
But... Is there another way ?
Like adding some kind of a commentary line, containing hints for the Excel parser, directly in the CSV.
Taking Oracle SQL queries hints system as an example, which allows us to change the query optimizer way of work.
I'm not sure this exists for Excel, maybe it is, but obscure and not well documented ?
I can't figure out why they would not add such a useful feature to "preconfigure" the format instead of having the user to navigate Excel settings.
Thanks
Related
As mentioned in the title I have an encoding issue in Excel. It requires special characters and also some letters.
I will show you an example: 1st being a good one and 2nd a bad one.
1st example
After I refresh the Excel workbook my special characters become something like the following.
2nd example
Sadly my work around right now is, using one of my colleagues Mac, to go in and refresh the workbook again and the special characters get fixed.
These Excel workbooks are created by a Microsoft Power Automate flow, and checking every excel file he creates in order to fix this issue, is waste of time.
Does anyone have any suggestions about how to fix this.
Sincerely, Daniel
If you are creating the Excel files yourself via a Power Automate flow you could look into using the Byte Order Mark characters to the beginning of the file.
Below is an explanation how you could achieve that:
https://powerusers.microsoft.com/t5/Building-Flows/Create-a-csv-file-that-uses-UTF-8-character-encoding/m-p/1311587/highlight/true#M148980
This is exactly what my PAD is doing. There is a master excel file, and when the online flow finds a new row in the database is running this PAD flow.
After few refreshes the new data is updated in the excel file, and that is when the special characters get changed, like presented in the main post
I collect various data in time plots. If I copy the timeplot data and then paste it into Excel, the number format is often wrong. For example, I often get a date like Aug 94 instead of the actual number from the TimePlot. Unfortunately, I can't easily format this date into a number either, since the formatted number does not match the actual number from the timeplot. If I format the date in the same format as the number above and below, then I get the number 34547. However, this number does not correspond to the actual number of the TimePlot. Anyone know how I can prevent this problem?
You can only solve this on the Excel side, AnyLogic provides the raw data for you. Excel then interprets stuff. You can test it by pasting the chart raw data into a txt or csv file.
So either fix your Excel settings or paste into a csv, then into an xlsx.
Or better still: Do not manually paste at all. Instead, write your model results into the AnyLogic database and export to Excel from there: this takes away a lot of the pain for you. Check the example models to learn how to do that.
This is not AnyLogic question, rather an Excel & computer formatting problem. One way of resolving this is changing computer's date and time settings.
Another way is to save your output at txt file in AnyLogic. Replace all . with ,. Then open empty Excel, select Text format for the columns. Copy-paste from the txt file.
In Excel there are a few options
when you paste use paste as text only option
But this does not always work as Excel will still try to format the stuff for you
Use the Paste Special option and then choose text
Also possible this will not work, based on your Excel settings.
Paste using the text import wizard
(This works for me without fail)
On step 2 choose tab delimited
On step 3 choose Column format as text for every column (you need to select them in the little diagram below)
You will then see the data exactly as it came from AnyLogic. See the example below where I purposefully imported some text which has something that Excel will think is a date. You will now be able to see what in your data made Excel thing your data needed to be formatted the way it is and then you can fix it. (post a new question if you struggle with this conversion)
But as noted by other answers first prize is to write all the important data to external files. But I know that even I sometimes want to export data from a chart and review it in Excel. Option 3 works for me everytime
If a cell has conditional formatting that uses an Icon Set (my current situation is using the Traffic Light Icon Set), is there a way to identify in VBA what particular icon is showing in that cell?
The motivation behind it is that it will correspond to a red/amber/green value which I'm exporting in a SQL statement, so I need to find it in VBA.
I can add new rules and select icon sets just fine:
Set Newiconset = Range("H3").FormatConditions.AddIconSetCondition
It's returning the properties of an existing set of rules that has me hung up.
Thanks for your help - I scoured StackOverflow for a solution and couldn't find it. If someone's solved this, let me know and I'll gladly remove my question.
Bad news: what I'm looking to do technically isn't possible.
Here's why:
Excel data is stored in XML files in a main Zip file (you can experiment with this by renaming an xlsx file to zip and opening it). Inside the data is stored in XML files, and when you finally find your workbook, you can see that the data is stored as the actual conditions themselves, with the range values and such. Excel then takes those and computes the result on the fly every time you look at that file. States are not saved when saving the file unfortunately. It's worth noting though that the current state of formulas is stored - I'm assuming this is how accessing values from external workbooks is handled.
This explains why you can set and read the rules just fine, but since there's nothing officially to read a value from you can't "get" the data.
The format of our member numbers has changed several times over the years, such that 00008, 9538, 746, 0746, 00746, 100125, and various other permutations are valid, unique and need to be retained. Exporting from our database into the custom Excel template needed for a mass update strips the leading zeros, such that 00746 and 0746 are all truncated to 746.
Inserting the apostrophe trick, or formatting as text, does not work in our case, since the data seems to be already altered by the time we open it in Excel. Formatting as zip won't work since we have valid numbers less than five digits in length that cannot have zeros added to them. And I am not having any luck with "custom" formatting as that seems to require either adding the same number of leading zeros to a number, or adding enough zeros to every number to make them all the same length.
Any clues? I wish there was some way to set Excel to just take what it's given and leave it alone, but that does not seem to be the case! I would appreciate any suggestions or advice. Thank you all very much in advance!
UPDATE - thanks everybody for your help! Here are some more specifics. We are using a 3rd party membership management app -- we cannot access the database directly, we need to use their "query builder" tool to get the data we want to mass update. Then we export using their "template" format, which is called XLSX but there must be something going on behind the scenes, because if we try to import a regular old Excel, we get an error. Only their template works.
The data is formatted okay in the database, because all of the numbers show correctly in the web-based management tool. Also, if I export to CSV, save it as a .txt and import it into Excel, the numbers show fine.
What I have done is similar to ooo's explanation below -- I exported the template with the incorrect numbers, then exported as CSV/txt, and copied / pasted THOSE numbers into the template and re-imported. I did not get an error, which is something I guess, but I will not be able to find out if it was successful until after midnight! :-(
Assuming the data is not corrupt in the database, then try and export from the database to a csv or text file.
The following can then be done to ensure the import is formatted correctly
Text file with comma delimiter:
In Excel Data/From text and selected Delimited, then next
In step 3 of the import wizard. For each column/field you want as text, highlight the column and select Text
The data should then be placed as text and retain leading zeros.
Again, all of this assumes the database contains non-corrupt data and you are able to export a simple text or csv file. It also assumes you have Excel 2010 but it can be done with minor variation across all versions.
Hopefully, #ooo's answer works for you. I'm providing another answer mainly for informational purposes, and don't feel like dealing with the constraints on comments.
One thing to understand is that Excel is very aggressive about treating "numeric-looking" data as actual numbers. If you were to open the CSV by double-clicking and letting Excel do its thing (rather than using ooo's careful procedure), those numbers would still have come up as numbers (no leading zeros). As you've found, one way to counteract this is to append clearly nonnumeric characters onto your data (before Excel gets its grubby hands on it), to really convince Excel that what it's dealing with is text.
Now, if the thing that uploads to their software is a file ending in .xlsx, then most likely it is the current Excel format (a compressed XML document, used by Excel 2007 and later). I suppose by "regular old Excel" you mean .xls (which still works with the newer Excels in "compatibility mode").
So in case what you've tried so far doesn't work, there are still avenues to explore before resorting to appending characters to the end of your data. (I'll update this answer as needed.)
You're on the right track with the apostrophe.
You'll need to store your numbers in excel as text at the time they are added to the file.
What are you using to create the original excel file / export from database?
This will likely be where your focus needs to be regarding your export.
For example one approach is that you could potentially modify the database export to include the ' symbol prefix before the numbers so that excel will know to display them as text.
I use the formula =text(cell,"# of zeros of the field") to add preceding zeros.
Example, Cell C2 has 12345 and I need it to be 10 characters long. I would put =text(c2,"0000000000").
The result will be 0000012345.
Is this at all possible?
If I open up my file in standard text editor e.g. notepad the preceeding zeros are displayed.
e.g. 000485001 shows up.
Although this doesn't happen in excel. All that's displayed is 485001
Just wondering if there's a way around this?
Thanks,
Yes, when you're importing (or using 'Text to columns') you can explicitly indicate the data type for a column (instead of General). If you select 'Text' the zeros will not be dropped.
Unfortunately you only see the dialog to specify this option when Excel is already open and you use either File/Open or Data/Text to Columns. If you just double click a .csv in the explorer you don't get this choice.
Excel tries very hard to determine the type of value it's importing. If it looks like a number, it will treat it like a number, and drop all the leading zeros as it reads it in. There's no way to get them back once they're lost.
You might try to import the file using the wizard that lets you set the data type for each column.
Rather than writing your data as a CSV file, use the SYLK (Symbolic Link) format instead. This format includes information about the style of a column, so that Excel will not try to auto-guess the type of data.
The easiest way to get started with this format is to export a small file from Excel and use that as a template.
Ok got around this by inserting a text character before the number i.e. #000485001
Simple enough!