简体   繁体   English

Excel 将额外的逗号导出为 CSV

[英]Excel Exports Extra Commas to CSV

I have a large macro program run through Excel 2010 that, after formatting large amounts of data into another table and exporting the workbook as a CSV file (by large amounts of data I mean thousands of rows, up to over 59,000 rows).我有一个通过 Excel 2010 运行的大型宏程序,在将大量数据格式化到另一个表中并将工作簿导出为 CSV 文件后(大量数据我指的是数千行,最多超过 59,000 行)。 Recently, my files have started ending up with an extra row of commas at the end like so:最近,我的文件开始以多一行的逗号结尾,如下所示:

data,data,data,data,number,date
data,data,data,data,number,date
,,,,,

I am exporting these files to an SQL database using a stored procedure, so ensuring that there are no extra commas to screw with the program is essential.我正在使用存储过程将这些文件导出到 SQL 数据库,因此确保没有多余的逗号与程序混淆是必不可少的。 So, with that said, what is happening and how can I prevent it?那么,话虽如此,正在发生什么,我该如何预防呢? I can provide any code or information that you believe is missing.我可以提供您认为缺少的任何代码或信息。

NOTE: It only appears to be happening on files with a couple thousand lines at least of data.注意:它似乎只发生在至少有几千行数据的文件上。 One file exported often has 2,000+ and another must have 59,000+ for the table to be exported.导出的一个文件通常有 2,000+,另一个必须有 59,000+ 才能导出表。

EDIT1: Here's the macro I'm using, just in case it would be helpful (requested by Ditto) EDIT1:这是我正在使用的宏,以防万一它会有所帮助(Ditto 要求)

Sub exportTable()
Dim varIsOpen As Boolean
Dim varSaveLocation1 As String, varSaveLocation2 As String
varIsOpen = False

If ThisWorkbook.Sheets("ControlSheet").Range("D2").value = "" Then
    varSaveLocation1 = ThisWorkbook.Path & "\CSVREVIEW\"
    varSaveLocation2 = varSaveLocation1 & Year(Now) & Month(Now) & Day(Now) & Hour(Now) & Minute(Now)
Else
    varSaveLocation1 = ThisWorkbook.Sheets("ControlSheet").Range("D2").value
    If Right(varSaveLocation1, 1) <> "\" Then varSaveLocation1 = varSaveLocation1 & "\"
    varSaveLocation2 = varSaveLocation1 & Year(Now) & Month(Now) & Day(Now) & Hour(Now) & Minute(Now)
End If
    For counter = 1 To Workbooks.Count
        If Workbooks(counter).Name = "TableBook.xls" Then varIsOpen = True

        If varIsOpen = True Then Exit For
    Next

    If varIsOpen = False Then GoTo isClosed

Workbooks("TableBook").Activate

Application.DisplayAlerts = False

'Check if TableBook is empty and don't export if so
If Workbooks("TableBook").Sheets("logFile").Range("A1").value = "" Then
    Workbooks("TableBook").Close
    GoTo isClosed
End If

'On Error Resume Next
If Len(Dir(varSaveLocation1, vbDirectory)) = 0 Then
   MkDir varSaveLocation1
End If
If Len(Dir(varSaveLocation2, vbDirectory)) = 0 Then
    MkDir varSaveLocation2
End If
'On Error GoTo 0

ActiveWorkbook.Sheets("test").Activate
ActiveWorkbook.SaveAs varSaveLocation2 + "\test", xlCSV

ActiveWorkbook.Sheets("part").Activate
ActiveWorkbook.SaveAs varSaveLocation2 + "\part", xlCSV

ActiveWorkbook.Sheets("logFile").Activate
ActiveWorkbook.SaveAs varSaveLocation2 + "\logFile", xlCSV

ActiveWorkbook.Sheets("deltaLimits").Activate
ActiveWorkbook.SaveAs varSaveLocation2 + "\deltaLimits", xlCSV

ActiveWorkbook.Close

Application.DisplayAlerts = True

isClosed:
End Sub

Tap Ctrl + End to see what Excel believes are the extents of your data.点击Ctrl + End以查看 Excel 认为数据的范围。 If it is beyond what you want to export, use Home ► Editing ► Clear ► Clear All to wipe all values and formatting from the rows below and the columns to the right of your desired data region and save the workbook.如果超出了您想要导出的范围,请使用主页 ► 编辑 ► 清除 ► 全部清除从下方的行和所需数据区域右侧的列中擦除所有值和格式,并保存工作簿。 Excel 2010 (with all SPs) will adjust to the CurrentRegion and Ctrl + End should now take you to the correct last cell . Excel 2010(带有所有 SP)将调整到 CurrentRegion 并且Ctrl + End现在应将您带到正确的最后一个单元格

Earlier versions of Excel (or XL2010 without all SPs) may require additional steps (see Unwanted extra blank pages in Excel ).早期版本的 Excel(或不带所有 SP 的 XL2010)可能需要额外的步骤(请参阅Excel 中不需要的额外空白页)。

我遇到了一个看起来相同的问题(csv 中的额外逗号),结果我在循环中导出了一个额外的行,而我使用的单元格是空的,因此我只有逗号

I had the exactly same problem.我遇到了完全相同的问题。 If the entire sheet is formatted, even just Text type or Text Height, Excel detects even empty cells as data.如果整个工作表都被格式化,即使只是文本类型或文本高度,Excel 甚至会将空单元格检测为数据。 You can delete the entire formatted columns/ rows as described above Cœur .您可以删除整个格式化的列/行,如上所述Cœur Or just create a new sheet without formatting anything and copy your code or change the address.或者只是创建一个新工作表而不设置任何格式并复制您的代码或更改地址。

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

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