gpt4 book ai didi

csv 文件中的 Excel 尾随逗号错误

转载 作者:行者123 更新时间:2023-12-01 22:49:16 25 4
gpt4 key购买 nike

我有一个 XLS 文件在 Excel 2003 上创建了一个带有宏的 CSV 文件,我有 40 多个列,最后 3 个是可选的,因此当我运行导出子例程时,XLS 上有很多空值它不会在所有行上添加尾随逗号,为什么?因为:http://support.microsoft.com/kb/77295 -.-

In Microsoft Office Excel, if you save a file in the text or in the CSV (comma separated value) format, Excel puts tabs or commas between each column of the worksheet. However, certain text files may be saved with a different number of tabs or commas in 16-row blocks.

这是他们建议的解决方法:

To make sure that Excel saves tab or comma delimiters for all empty columns, verify that the last column in the file contains some data in at least every 16 rows throughout the file. If the blocks of rows do not contain data, add spaces or other characters in every 16 rows to the cells in the last column, or reorder the columns in the worksheet so that the last column on the worksheet always contains information.

-.- 微软干得好! -.-

好的,所以我的主要问题是生成的文件将由我范围之外的另一个程序解析,该程序需要特定的格式,因为现在我将每 16 行添加一个空白,如果该字段为空,这似乎这样做,但数据处理部门正在提示那个空白......你能相信他们吗!?

无论如何,我也尝试添加一个标志并使用查找功能将其删除,但是 ofc 当您保存文件时,它会再次删除分隔符...

感谢您阅读我的历史;p

有什么建议吗?

编辑:不幸的是,必须使用 Excel,数据是由不同用户通过 Excel 工作表手动输入的,vba 代码更像模板生成等。这是我遇到的唯一问题,它是改变整个流程是不可行的。

最佳答案

手动示例;

Dim r As Range: Set r = Range("A1:C10") '// or ActiveSheet.UsedRange for everything
Dim buffer As String, delimiter As String, c As Range
Dim i As Long

For Each c In r
If (i Mod r.Columns.Count) = 0 Then
If (Len(buffer)) Then delimiter = vbCrLf
Else
delimiter = ","
End If
buffer = buffer & delimiter & """" & CStr(c.Value) & """" '//or c.text to preserve formatting
i = (i + 1)
Next

Open "c:\xxx.csv" For Output As #1
Print #1, buffer
Close #1

关于csv 文件中的 Excel 尾随逗号错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7079009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com