简体   繁体   中英

Imported .csv file with formulas

I have imported a comma seperated csv file using powershell. I gets imported and looks as it should. The problem is, the cells contain formulas. Like =20+50+70. It doesn't get calculated unless i click enter i the top field. Another problem is, that some of the cells contains numbers like =50,2+70,5. These cells excel doesn't understand at all. It can't caltulate them, unless i remove the , or replace it with a dot (.). But this is not a possibility. How to i fix this? The csv file is imported with powershell using this:

[threading.thread]::CurrentThread.CurrentCulture = 'en-US'
$wbpath=Join-Path "$psscriptroot" 'file.xlsx'
$importcsv=Join-Path "$psscriptroot" 'file.csv'
$xl = New-Object -ComObject Excel.Application
$xl.Visible = $false
$xl.Workbooks.OpenText($importcsv)
$xl.DisplayAlerts = $false
[threading.thread]::CurrentThread.CurrentCulture = 'en-US'
$xl.ActiveWorkbook.SaveAs($wbpath,51)
$xl.Quit()
while([System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)){'released'}

The

[threading.thread]::CurrentThread.CurrentCulture = 'en-US'

is necessary or i will get errors because my system locale is not us.

Thank you.

CSV Sample:

name1.name1.name1,"=20","=7,65","=20,01"
name2.name2.name2,"=20+10","=4,96+0,65","=20,01+10"
name3.name3.name3,"=20","=4,96+0,88","=21,01+11"

Sounds like you need to

a) Force the worksheet to calculate

b) If you're going to stick with en-US locale then you need to replace those commas with decimal points. That's the GB/US standard and how Excel will interpret decimals. I'd strongly advise however that you stick to the locale that your data is set up in.

(untested as I'm currently on a Mac)

[threading.thread]::CurrentThread.CurrentCulture = 'en-US'
$wbpath=Join-Path "$psscriptroot" 'file.xlsx'
$importcsv=Join-Path "$psscriptroot" 'file.csv'
$xl = New-Object -ComObject Excel.Application
$xl.Visible = $false
$wb = $xl.Workbooks.OpenText($importcsv)
$xl.DisplayAlerts = $false
[threading.thread]::CurrentThread.CurrentCulture = 'en-US'
$sh = $wb.Sheets.Item(1)
# loop through the used range and replace any commas with decimals
foreach ($cell in $sh.usedRange)
{
    [string]$formula = $cell.formula
    $formula -replace ',','.'
    $cell.formula = $formula
}
# force the sheet to calculate
$sh.Calculate()
$xl.ActiveWorkbook.SaveAs($wbpath,51)
$xl.Quit()
while([System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)){'released'}

As with the previous answer, you have to account for locale; not all .csv files are the same formatting based on what country locale they were encoded in. While UTF is standard, in some respects CSV is a "legacy format", even if it's the most lightweight, simple way to transfer data using plaintext.

Sam already answered the majority of the difficult stuff, so I'll just add a few things. If you are making an automated solution and work with multiple countries, there's a few ways you can determine how it's encoded. You can go the more technically proficient route and implement a custom function similar to this one https://gist.github.com/jpoehls/2406504 or, because it's a CSV, you can make a decent guess since the most common encoding formats use different delimiters; I believe the one you are mentioning uses tabs as encoding.

I'll focus on the ones within Excel importing because those weren't mentioned. There's a fairly neat function in the Data tab that allows you to customize your import based on what delimiters it uses. In the third step when you press Advanced, it allows you to tell it which separator (comma or decimal) that the source data is using, and once you select that and press Finish, it will convert the result to whatever your locale is set to for Excel and properly evaluate functions. Example picture So, the workflow for this would be open a new Excel book, select Data > From Text and proceed from there. It will convert the text from the locale you choose (in your case 1252 is likely) into whatever decimal format you specify.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM