c# - 从 FTP 服务器读取 Excel 文件时如何获取正确格式的数据

标签 c# excel ftp

我正在尝试从 FTP 服务器读取 excel 文件并将数据存储在列表中。
Excel 文件正在从 FTP 服务器读取,但数据即将到来

这是我的代码

 static void Main(string[] args)
    {
        //Listing all files from a folder

        string filename = getFileList("ftp://ftp.demosite.com/demoFolder/", "username", "password");


        //Here we know the file name
        FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create("ftp://ftp.demosite.com/demoFolder/"+filename);
        reqFTP.UsePassive = false;
        reqFTP.UseBinary = true;
        reqFTP.Credentials = new NetworkCredential("username", "password"); 
        reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
        reqFTP.Proxy = GlobalProxySelection.GetEmptyWebProxy();

        FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
        Stream responseStream = response.GetResponseStream();

        // for excelread
        StreamReader reader = new StreamReader(responseStream);
        string[] allLines = reader.ReadToEnd().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
        // for textfile read
        //TextReader tmpReader = new System.IO.StreamReader(responseStream);
        //string fileContents = tmpReader.ReadToEnd();
        Console.WriteLine("Now writing from file....");
        foreach (var item in allLines)
        {
            Console.WriteLine(item);                
        }
        Console.WriteLine("all file content is printed....");
        Console.ReadKey();

    }

并且数据以这种格式出现:
Current O/P Format

请帮忙!
谢谢你。

最佳答案

正如斯图尔特指出的那样。您正在将 excel 文件作为文本文件读取。如果要读取 Excel 文件,则需要将 Office.Interop.Excel 引用添加到项目中,然后添加到您的类中。如果你的机器上有excel,你可以毫无问题地做到这一点。如果您没有excel,则必须找到第三方库。我最近发布了一个答案,解释了如果您的机器上有 excel 怎么做。

Linking Excel In C#

一旦引用存在,您就可以正确访问 excel 文件。

关于c# - 从 FTP 服务器读取 Excel 文件时如何获取正确格式的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39659609/

相关文章:

c# - 单个方法中有 2 个不同的代码,可能使用 Type

c# - 如何使用 Entity Framework 从 WCF 内部提取数据到客户端

javascript - 如何在 Excel VBA 中使用 Selenium Webdriver 滚动下拉菜单

excel - 在 A 列中查找测试 1-测试 4 后如何对 B 列中的值求和

ruby-on-rails - Ruby on Rails - Encoding::UndefinedConversionError: "\xC3"从 ASCII-8BIT 到 UTF-8

caching - 当我通过 FTP 更新文件时,文件没有改变

c# - 将 int 隐式转换为 double

c# - MVC 一种类型,一页 - 两种不同的 DisplayFor

每个循环问题的 VBA Excel

python - 在 FTPS 服务器上使用 ftplib 列出目录时 sslobj.do_handshake 中的 "OSError: [Errno 0] Error"