将每个 Excel 列导出到单独的文本或 csv 文件?

发布于 2024-09-03 03:17:27 字数 64 浏览 4 评论 0原文

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

I have an Excel worksheet with around 100 cols. Does anyone know of an easy way to write the contents of each column to a csv or txt file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

z祗昰~ 2024-09-10 03:17:27

我面前没有 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

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

I don't have Excel in front of me, but I think this code is approximately what you need, give or take some syntax errors. It should write each column into a separate file, with each cell on a different row. It will work for arbitrary column heights, though the number of columns is in a variable (for now).

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

Let me know if that helps or not, I can take another look later if you would like

孤者何惧 2024-09-10 03:17:27

也许

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

Perhaps

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
灯下孤影 2024-09-10 03:17:27

一条非常快速的线路可以帮助您开始......

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

A very quick line to get you started ...

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文