使用 C# 使用 EPPlus 库直接在 FTP 上创建 Excel 文件
Create Excel file directly on FTP with C# using EPPlus library
我想在 FTP 服务器上创建一个 Excel 文件。我试过在本地创建一个文件,它对问题很有效:如何在 FTP 而不是本地驱动器中创建它?
ExcelPkg.SaveAs(
new FileInfo(@"C:\ExcelTest\" + DateTime.Now.ToString("yyyy-MM-dd--hh-mm-ss") + ".xlsx"))
我想直接在 FTP 中创建此文件,而不是在本地创建然后移动它。
使用 ExcelPackage.SaveAs
overload that accepts a Stream
并向其传递一个 FTP 请求流,例如:
我现在无法测试 EPPlus,但这应该可以:
WebRequest request = WebRequest.Create("ftp://ftp.example.com/remote/path/sheet.xlsx");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
using (Stream ftpStream = request.GetRequestStream())
{
ExcelPkg.SaveAs(ftpStream);
}
我必须进行 2 种不同的操作,第一种是创建 Excel 文件,然后是上传现有文件,然后将其删除。
使用名为 WinSCP 的库
我找不到任何支持 90 年代加密的库,例如基于 TLS 的隐式 FTP 和同时流式传输 API。所以我没有办法直接在服务器上创建excel文件,而是在本地临时创建它复制过来,同时删除它,几乎是一样的结果。这是解决方法
try
{
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = @"xx.xx.x.x",
UserName = "xxxx",
Password = "xxxxxx",
PortNumber = xxxx,
FtpSecure = FtpSecure.Implicit, //Encryption protocol
GiveUpSecurityAndAcceptAnyTlsHostCertificate = true // Accepts any certificate
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Upload files
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult;
// Copy's Existing file to Connected server and delets the old one.
// change by replace "true" with "false".
transferResult =
session.PutFiles(
ExistingPath, "/ScheduleJobs/" + RemotePath, true, transferOptions);
// Throw on any error
transferResult.Check();
// Print results
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
Console.ReadLine();
}
}
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e);
}
我想在 FTP 服务器上创建一个 Excel 文件。我试过在本地创建一个文件,它对问题很有效:如何在 FTP 而不是本地驱动器中创建它?
ExcelPkg.SaveAs(
new FileInfo(@"C:\ExcelTest\" + DateTime.Now.ToString("yyyy-MM-dd--hh-mm-ss") + ".xlsx"))
我想直接在 FTP 中创建此文件,而不是在本地创建然后移动它。
使用 ExcelPackage.SaveAs
overload that accepts a Stream
并向其传递一个 FTP 请求流,例如:
我现在无法测试 EPPlus,但这应该可以:
WebRequest request = WebRequest.Create("ftp://ftp.example.com/remote/path/sheet.xlsx");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
using (Stream ftpStream = request.GetRequestStream())
{
ExcelPkg.SaveAs(ftpStream);
}
我必须进行 2 种不同的操作,第一种是创建 Excel 文件,然后是上传现有文件,然后将其删除。 使用名为 WinSCP 的库 我找不到任何支持 90 年代加密的库,例如基于 TLS 的隐式 FTP 和同时流式传输 API。所以我没有办法直接在服务器上创建excel文件,而是在本地临时创建它复制过来,同时删除它,几乎是一样的结果。这是解决方法
try
{
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = @"xx.xx.x.x",
UserName = "xxxx",
Password = "xxxxxx",
PortNumber = xxxx,
FtpSecure = FtpSecure.Implicit, //Encryption protocol
GiveUpSecurityAndAcceptAnyTlsHostCertificate = true // Accepts any certificate
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Upload files
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult;
// Copy's Existing file to Connected server and delets the old one.
// change by replace "true" with "false".
transferResult =
session.PutFiles(
ExistingPath, "/ScheduleJobs/" + RemotePath, true, transferOptions);
// Throw on any error
transferResult.Check();
// Print results
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
Console.ReadLine();
}
}
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e);
}