我正在尝试从 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/