简体   繁体   English

将数据导出到CSV文件

[英]Export Data to CSV file

We have more than 65,536 rows of data to export(write) in .csv file. 我们有65,536多行数据要导出(写入).csv文件中。 But Excel(CSV) supports only 65,536. 但是Excel(CSV)仅支持65,536。 Excel supports multiple workbook, so we can write the data in multiple workbooks . Excel支持多个工作簿,因此我们可以在多个工作簿中写入数据。 But CSV doesn't support this feature too. 但是CSV也不支持此功能。 Is there any other way to do this. 还有其他方法可以做到这一点。 Could any one help on this? 有人可以帮忙吗?

You could write the data to multiple CSV-files, if that's possible. 如果可能,您可以将数据写入多个CSV文件。 CSV is basically just a text file, so there is no stuff like multiple sheets etc. CSV基本上只是一个文本文件,因此没有诸如多张纸之类的东西。

Maybe you could use Excel files (xls) and multiple sheets. 也许您可以使用Excel文件(xls)和多个工作表。 There exist libraries to write Excel files depending on the language you are using (eg Apache POI for Java). 有一些库可以根据您使用的语言来编写Excel文件(例如, Apache POI for Java)。

If you are targeting Excel, there are many libraries to help generate xls file insted of the CSV. 如果您的目标是Excel,则有许多库可帮助生成由CSV插入的xls文件。 On of them is CarlosAg.ExcelXmlWriter.dll that is easy to use. 它们上是易于使用的CarlosAg.ExcelXmlWriter.dll

The below was originally written for Excel2003 but when I migrated to 2007, COMDLG32 was not supported so annoyingly you just get a InputBox. 以下内容最初是为Excel2003编写的,但是当我迁移到2007年时,不支持COMDLG32,因此令人讨厌的是,您只得到一个InputBox。 I haven't used it for some time so it may need a bit of reworking, but should hopefully point you in the right direction. 我已经有一段时间没有使用它了,所以它可能需要一些修改,但是希望可以为您指明正确的方向。

Sub OpenCSV_bysheet()

'No COMDLG32.OCX

    Dim fileNo As Integer
    Dim tempRow, fileNm As String
    Dim tempRowNo, x, y As Long
    Dim CommaOnOff As Boolean

    fileNm = InputBox("Please input Drive:\Path\Filename.csv", , CurDir & "\*.csv")
    If fileNm = "" Then Exit Sub
    For x = 1 To Len(fileNm)
        If Mid(fileNm, x, 1) = "*" Or Mid(fileNm, x, 1) = "?" Then Exit Sub
    Next x

'    UserForm1.CommonDialog1.CancelError = True
'    UserForm1.CommonDialog1.Flags = cdlOFNHideReadOnly
'    UserForm1.CommonDialog1.Filter = "Comma Separated Value files (*.csv)|*.csv|All Files (*.*)|*.*"

    On Error GoTo errorTrap
'    UserForm1.CommonDialog1.ShowOpen

'    fileNm = UserForm1.CommonDialog1.Filename

    fileNo = FreeFile
    tempRowNo = 0
    x = 0
    y = 0

    On Error Resume Next
    Workbooks.Add (xlWBATWorksheet)
    Application.ScreenUpdating = False

    Open fileNm For Input As fileNo
        Do While Not EOF(fileNo)
            Line Input #fileNo, tempRow

            If x Mod 65536 = 0 And x > 0 Then
                Sheets.Add
                x = 0
            End If
            x = x + 1
            y = y + 1

            ActiveCell.Cells(x, 1).Value = tempRow

            ActiveCell.Cells(x, 1).TextToColumns Destination:=ActiveCell.Cells(x, 1), _
                DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, _
                Tab:=False, Semicolon:=False, Comma:=True, Space:=False, Other:=False

            Application.StatusBar = y

        Loop
    Close fileNo

errorTrap:
    Application.ScreenUpdating = False
    Application.StatusBar = False
End Sub

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

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