简体   繁体   English

带Excel的StreamReader和StreamWriter

[英]StreamReader and StreamWriter with Excel

I need some guidance here. 我在这里需要一些指导。

I am reading a excel file using a StreamReader , then get the file to a string using the StreamReader.ReadToEnd(); 我正在使用StreamReader读取excel文件,然后使用StreamReader.ReadToEnd()将文件获取为字符串 method. 方法。 Then I write the string to a different location on the file system using a StreamWriter.Write() method. 然后,使用StreamWriter.Write()方法将字符串写入文件系统上的其他位置。

Then I re-read file from the location I wrote it earlier. 然后,我从之前编写的位置重新读取了文件。 However it seems I am reading some garbage values and I can't open the excel file from the new location... 但是,似乎我正在读取一些垃圾值,并且无法从新位置打开excel文件...

Am I doing something wrong here to file to get corrupted ? 我在这里做错事了以致遭到损坏吗? Am I missing something to do with encoding here ? 我在这里缺少与编码有关的东西吗?

Excel files are binary. Excel文件是二进制文件。 StreamReader is a kind of TextReader , and StreamWriter is a kind of TextWriter . StreamReaderTextReader的一种, StreamWriterTextWriter的一种。

Binary and Text - not the same thing. 二进制文本 -不一样。

Depending on what Excel format you are using, you will find it very painful to read/write directly. 根据您使用的Excel格式,您会发现直接读取/写入非常痛苦。 Libraries such as NPOI make this much easier. NPOI之类的库使此操作变得更加容易。

The version that reads/writes xlsx files is still in beta, but stable in my use. 读取/写入xlsx文件的版本仍处于beta版本,但在我使用时稳定。 If you need xlsx format files, download from their site, instead of via NuGet. 如果需要xlsx格式的文件,请从其站点下载,而不要通过NuGet下载。 NPOI on GitHub GitHub上的NPOI

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM