everyone,
can anyone give me an direction on how to save an Excel File to Windows Server that has no Visual Studio 2022 installed?
I can save an Excel File to Windows Server 2022 that has Visual Studio 2022 installed, but I can't save an Excel file to servers that has no Visual Studio installed.
I am using Visual Studio 2022 and ClosedXML package in C#.
Below function saves Excel file to server folder.
public static string ExportToExcel(DataSet ds, string outputPath)
{
if (ds == null) throw new ArgumentNullException(nameof(ds));
if (string.IsNullOrWhiteSpace(outputPath)) throw new ArgumentException("Output path is required.", nameof(outputPath));
var fullPath = Path.GetFullPath(outputPath);
var dir = Path.GetDirectoryName(fullPath);
if (string.IsNullOrEmpty(dir))
throw new InvalidOperationException("Output path must include a directory.");
Directory.CreateDirectory(dir);
using (var wb = new XLWorkbook())
{
foreach (DataTable table in ds.Tables)
{
// Sheet name must be <= 31 chars and no invalid chars
var sheetName = string.IsNullOrWhiteSpace(table.TableName) ? "Sheet" : table.TableName;
sheetName = sheetName.Length > 31 ? sheetName.Substring(0, 31) : sheetName;
wb.Worksheets.Add(table, sheetName);
}
wb.SaveAs(fullPath);
}
if (!File.Exists(fullPath))
throw new IOException("Export failed: file not found after save.");
return fullPath;
}