简体   繁体   English

导入的 .csv 文件和公式

[英]Imported .csv file with formulas

I have imported a comma seperated csv file using powershell.我已经使用 powershell 导入了一个逗号分隔的 csv 文件。 I gets imported and looks as it should.我被导入并看起来应该如此。 The problem is, the cells contain formulas.问题是,单元格包含公式。 Like =20+50+70.喜欢 =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.另一个问题是,某些单元格包含诸如 =50,2+70,5 之类的数字。 These cells excel doesn't understand at all.这些单元格excel根本看不懂。 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: csv 文件是使用 powershell 导入的:

[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: CSV 示例:

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 a) 强制工作表计算

b) If you're going to stick with en-US locale then you need to replace those commas with decimal points. b) 如果您要坚持使用en-US语言环境,那么您需要用小数点替换这些逗号。 That's the GB/US standard and how Excel will interpret decimals.这是 GB/US 标准以及 Excel 将如何解释小数。 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) (未经测试,因为我目前在 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.并非所有 .csv 文件都具有相同的格式,具体取决于它们编码所在的国家/地区。虽然 UTF 是标准格式,但在某些方面 CSV 是“传统格式”,即使它是使用纯文本传输数据的最轻量级、最简单的方法。

Sam already answered the majority of the difficult stuff, so I'll just add a few things. Sam 已经回答了大部分困难的问题,所以我将添加一些内容。 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;您可以走技术更熟练的路线并实现与此类似的自定义功能https://gist.github.com/jpoehls/2406504或者,因为它是 CSV,您可以进行合理的猜测,因为最常见的编码格式使用不同的分隔符; 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.我将重点介绍 Excel 导入中的那些,因为没有提到那些。 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.在第三步中,当您按 Advanced 时,它允许您告诉它源数据使用的是哪个分隔符(逗号或小数),一旦您选择它并按 Finish,它就会将结果转换为您的语言环境设置的任何内容用于 Excel 并正确评估函数。 Example picture So, the workflow for this would be open a new Excel book, select Data > From Text and proceed from there.示例图片因此,此工作流将打开一个新的 Excel 工作簿,选择数据 > 从文本并从那里继续。 It will convert the text from the locale you choose (in your case 1252 is likely) into whatever decimal format you specify.它会将您选择的语言环境中的文本(在您的情况下可能是 1252)转换为您指定的任何十进制格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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