2010-07-05 127 views
1

我们有超过65,536行的数据导出(写入).csv文件。但Excel(CSV)仅支持65,536。 Excel支持多个工作簿,因此我们可以将数据写入多个工作簿。但CSV也不支持此功能。有没有其他的方式来做到这一点。任何人都可以帮忙吗?将数据导出到CSV文件

+0

为什么不直接写入电子表格呢? Microsoft.Office.Interop.Excel命名空间。 – 2010-07-05 12:36:33

回答

0

如果您的目标是Excel,有许多库可以帮助生成CSV文件的xls文件。对他们来说是易于使用的CarlosAg.ExcelXmlWriter.dll

3

如果可能的话,您可以将数据写入多个CSV文件。 CSV基本上只是一个文本文件,所以没有像多张纸等东西。

也许你可以使用Excel文件(xls)和多张纸。根据您使用的语言,存在用于编写Excel文件的库(例如用于Java的Apache POI)。

0

下面最初是为Excel2003编写的,但是当我迁移到2007年时,COMDLG32不被支持,所以烦恼地你只是得到一个InputBox。我有一段时间没有使用它,所以它可能需要一点修改,但希望指向正确的方向。

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