read xlsx file from sftp server in c# . code in .net core
Loading
read xlsx file from sftp server in c# . code in .net core
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Prasad RaveendranRank #320 · 7.8K reputationPosted Oct 16, 2025, 9:59 PM EDT
using System; using System.IO; using WinSCP; using ClosedXML.Excel; class Program { static void Main() { // SFTP session options SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Sftp, HostName = "sftp.example.com", UserName = "your_username", Password = "your_password", SshHostKeyFingerprint = "ssh-rsa 2048 xxxxx..." }; using (Session session = new Session()) { session.Open(sessionOptions); Console.WriteLine("Connected to SFTP"); string remoteFilePath = "/remote/path/to/file.xlsx"; // Download file to memory stream using (MemoryStream ms = new MemoryStream()) { session.GetFiles(remoteFilePath, ms).Check(); ms.Position = 0; // Read Excel using ClosedXML using (var workbook = new XLWorkbook(ms)) { var worksheet = workbook.Worksheet(1); // first sheet foreach (var row in worksheet.RowsUsed()) { foreach (var cell in row.Cells()) { Console.Write(cell.Value + "\t"); } Console.WriteLine(); } } } session.Dispose(); Console.WriteLine("Disconnected from SFTP"); } } }check whether this code can be used
Leon DRank #851 · 2.6K reputationPosted Oct 17, 2025, 7:28 AM EDT
You can read an
.xlsxfile from an SFTP server in C# using Free Spire.XLS for .NET by combining an SFTP client library (like SSH.NET) to download the file into a stream, and then loading it into aWorkbookobject.Before getting started, install the required libraries:
Here’s a complete example:
using Renci.SshNet; using Spire.Xls; using System; using System.IO; namespace ReadExcelFile { class Program { static void Main(string[] args) { string host = "your.sftp.server"; int port = 22; // default SFTP port string username = "yourUsername"; string password = "yourPassword"; string remoteFilePath = "/remote/path/YourFile.xlsx"; // Connect to SFTP and download file into a memory stream using (var sftp = new SftpClient(host, port, username, password)) { sftp.Connect(); using (var ms = new MemoryStream()) { sftp.DownloadFile(remoteFilePath, ms); ms.Position = 0; // Reset stream position // Load the XLSX file using Spire.XLS Workbook workbook = new Workbook(); workbook.LoadFromStream(ms); // Now you can work with workbook Worksheet sheet = workbook.Worksheets[0]; string value = sheet.Range["A1"].Value; Console.WriteLine("Value in A1: " + value); } sftp.Disconnect(); } } } }