【问题标题】:Exporting each Excel column to individual text or csv files?将每个 Excel 列导出到单个文本或 csv 文件?
【发布时间】:2010-05-28 02:40:13
【问题描述】:

我有一个大约 100 列的 Excel 工作表。有谁知道将每列的内容写入 csv 或 txt 文件的简单方法?

【问题讨论】:

    标签: excel text csv export


    【解决方案1】:

    我面前没有 Excel,但我认为这段代码大约是你所需要的,给出或接受一些语法错误。它应该将每一列写入一个单独的文件,每个单元格位于不同的行。它适用于任意列高,尽管列数在一个变量中(目前)。

    dim fso as FileSystemObject
    dim ts as TextStream
    dim i as Integer
    dim myCell as Range
    
    set fso = FileSystemObject
    
    for i = 0 to TotalColumnNumber
       ' last argument, True, says to create the text file if it doesnt exist, which is
       ' good for us in this case
       Set ts = fso.OpenTextFile("column_" & i, ForWriting, True)
    
       ' set mycell to the first cell in the ith column
       set myCell = SheetName.cells(1,i)
    
       ' continue looping down the column until you reach a blank cell
       ' writing each cell value as you go
       do until mycell.value = ""
           ts.writeline mycell.value
           set myCell = myCell.offset(1,0)
       loop
    
       ts.close
    next
    
    set ts = nothing
    set fso = nothing
    

    让我知道这是否有帮助,如果你愿意,我可以稍后再看看

    【讨论】:

      【解决方案2】:

      也许

      Dim cn As Object
      Dim rs As Object
      Dim strFile As String
      Dim strCon As String
      Dim strSQL As String
      Dim i As Integer
      
      ''This is not the best way to refer to the workbook
      ''you want, but it is very conveient for notes
      ''It is probably best to use the name of the workbook.
      
      strFile = ActiveWorkbook.FullName
      
      ''Note that if HDR=No, F1,F2 etc are used for column names,
      ''if HDR=Yes, the names in the first row of the range
      ''can be used.
      ''This is the Jet 4 connection string, you can get more
      ''here : http://www.connectionstrings.com/excel
      
      strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
          & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
      
      ''Late binding, so no reference is needed
      
      Set cn = CreateObject("ADODB.Connection")
      Set rs = CreateObject("ADODB.Recordset")
      
      cn.Open strCon
      
      ''WHERE 1=1 = headers only, note hdr=yes above
      
      strSQL = "SELECT * " _
             & "FROM [Sheet1$] " _
             & "WHERE 1=1"
      
      
      ''Open the recordset for more processing
      ''Cursor Type: 3, adOpenStatic
      ''Lock Type: 3, adLockOptimistic
      ''Not everything can be done with every cirsor type and
      ''lock type. See http://www.w3schools.com/ado/met_rs_open.asp
      
      rs.Open strSQL, cn, 3, 3
      
      ''Output including nulls. Note that this will fail if the file
       ''exists.
      
      For i = 0 To rs.Fields.Count - 1
          strSQL = "SELECT [" & rs.Fields(i).Name & "] " _
          & "INTO [Text;HDR=YES;FMT=Delimited;IMEX=2;DATABASE=C:\Docs\]." _
          & rs.Fields(i).Name & ".CSV " _
          & "FROM [Sheet1$] "
      
          ''To skip nulls and empty cells, add a WHERE statement
          ''& "WHERE Trim([" & rs.Fields(i).Name & "] & '')<>'' "
      
          cn.Execute strSQL
      Next
      
      
      ''Tidy up
      rs.Close
      Set rs = Nothing
      cn.Close
      Set cn = Nothing
      

      【讨论】:

        【解决方案3】:

        一个非常快速的线路让你开始......

        for i = 1 to 100   
            open "file" & i & ".txt" as #1
            for each c in columns(i).cells
               print #1, c.value
            next c
            close #1
        next i
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-17
          • 1970-01-01
          • 1970-01-01
          • 2017-12-02
          • 2019-08-18
          • 2018-02-21
          相关资源
          最近更新 更多