从csv文件读取并将其写入xls文件 [英] Reading from a csv file and writing it into xls file
问题描述
在我的vb.net winform应用程序中,我需要从sample.csv文件中读取数据并将其写入demo.xls.我使用Stream Reader类将csv中的数据读取为字符串.当我使用StreatWriter类时,整行都放在单个单元格中,这不是我的目标.如何解决这个问题?
sample.csv包含:
ID名称Sal位置
1汤姆345美国
2马丁4533新泽西州
3戴尔3433 York
有人可以建议我吗?
In my vb.net winform application I need to read data from sample.csv file and write that data into demo.xls. I used Stream Reader classes to read the data from csv into a string. When I used StreatWriter class the entire row is coming in a single cell, which is not my aim. How to solve this issue?
sample.csv contains:
ID Name Sal Location
1 tom 345 USA
2 Martin 4533 NJ
3 Dell 3433 York
Can anyone suggests me please?
推荐答案
那不是csv文件. CSV文件以逗号分隔.
如果它是实际的CSV文件,则excel将已经按您期望的方式将其打开.
That''s not a csv file. A CSV file is comma-delimited.
If it WERE an actual CSV file, excel will already openit as you expect it to.
sInputArray = Split(input_csv_stiring, ",")
假定csv文件列的顺序正确.将每个数组成员写入对应的Excel列.
[更新]
ioInput是一个数组,其元素数与列数相同.
This assumes the csv file columns are in the correct order. write each array member to the correspondong Excel column.
[Update]
ioInput is an array with same number of elements as number of columns.
While Not ioLine = ""
ioLine = ioFile.ReadLine
ioInput = Split(ioLine, ",")
'--> Write to Excel Object for each line
End While
另外,要写入Excel文件,您需要从VB http://support.microsoft.com/kb/301982与Excel互操作 [^ ]
Also, to write to an Excel file you will need to interop with Excel from VB http://support.microsoft.com/kb/301982[^]
在注释中的代码中尝试一下
Try this from your code in the comments
Dim sb As StringBuilder = New StringBuilder()
Using ioFile As New System.IO.StreamReader("sample.csv")
While Not ioFile.EndOfStream
sb.Append(ioFile.ReadLine & vbCrLf)
End While
End Using
Using ioWriter As New System.IO.StreamWriter("new.csv")
ioWriter.Write(sb.ToString())
End Using
应该可以满足您的要求.
that should do what you want I think.
这篇关于从csv文件读取并将其写入xls文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!