csv文件去掉最后的逗号_csv文件中的Excel结尾逗号错误

I have an XLS file creates a CSV file with a macro on Excel 2003, I have 40+ columns and the last 3 ones are optional, therefore there are a lot of empty values on the XLS, when I run the exporting subroutine it wont put a trailing comma on all of the rows, why ? Because : 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.

here is their suggested work around:

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.

-.- way to go microsoft ! -.-

Ok so my main issue is that the generated file will be parsed by another program out of my scope which needs that specific format as it is right now I'm adding a blank every 16 row if the field is empty, this seems to do it but Data Processing Deparment is complaining about that white space... can you believe them!?

Anyway I also tried to add a flag and remove it with the find function, but ofc when you save the file it will take away the delimiters again...

thanks for reading my history ;p

Any suggestions ?

Edit: Unfortunately using Excel is must, the data is manually inputted through the excel sheet by different users, the vba codes does a lot more like template generation etc.. this is the only issue that I'm having and it is not feasible to change the whole process.

解决方案

Manual example;

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值