我已经使用 powershell 导入了一个逗号分隔的 csv 文件。我被导入并看起来应该如此。问题是,单元格包含公式。喜欢 =20+50+70。除非我单击顶部字段,否则不会计算它。另一个问题是,某些单元格包含诸如 =50,2+70,5 之类的数字。这些单元格excel根本看不懂。它无法计算它们,除非我删除 ,或者用点 (.) 替换它。但这不是一种可能。我该如何解决这个问题?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'}
这
[threading.thread]::CurrentThread.CurrentCulture = 'en-US'
是必要的,否则我会收到错误,因为我的系统区域设置不是我们。
谢谢你。
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"
听起来你需要
a) 强制工作表计算
b) 如果您要坚持使用en-US区域设置,那么您需要将这些逗号替换为小数点。这是 GB/US 标准以及 Excel 将如何解释小数。不过,我强烈建议您坚持使用数据设置的区域设置。
(未经测试,因为我目前在 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'}
| 归档时间: | 
 | 
| 查看次数: | 837 次 | 
| 最近记录: |