简体   繁体   中英

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(); method. Then I write the string to a different location on the file system using a StreamWriter.Write() method.

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...

Am I doing something wrong here to file to get corrupted ? Am I missing something to do with encoding here ?

Excel files are binary. StreamReader is a kind of TextReader , and StreamWriter is a kind of TextWriter .

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. Libraries such as NPOI make this much easier.

The version that reads/writes xlsx files is still in beta, but stable in my use. If you need xlsx format files, download from their site, instead of via NuGet. NPOI on GitHub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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