【问题标题】:Export Data to CSV file将数据导出到 CSV 文件
【发布时间】:2010-07-05 12:17:08
【问题描述】:

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

【问题讨论】:

  • 您为什么不直接将其写入电子表格? Microsoft.Office.Interop.Excel 命名空间。

标签: .net excel csv


【解决方案1】:

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

也许您可以使用 Excel 文件 (xls) 和多张工作表。存在用于编写 Excel 文件的库,具体取决于您使用的语言(例如,Apache POI 用于 Java)。

【讨论】:

    【解决方案2】:

    如果您以 Excel 为目标,有许多库可帮助生成 CSV 的 xls 文件。其中一个是CarlosAg.ExcelXmlWriter.dll,易于使用。

    【讨论】:

      【解决方案3】:

      下面的内容最初是为 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
      

      【讨论】:

        猜你喜欢
        • 2017-10-16
        • 2014-08-07
        • 2014-10-20
        • 1970-01-01
        • 1970-01-01
        • 2015-09-01
        • 2013-09-10
        相关资源
        最近更新 更多