获取Excel工作簿的path或文件名

如何在下面的代码中获取工作簿的path或文件名,以便可以获取编辑的工作簿。 我在这里使用了ExpertXLS – 用于.NET的Excel电子表格库 – C#/ VB.NET

using ExpertXls; namespace IQC { public class CSFB { public static string GenerateTemplateForCurrentGridView(IQO[] items, string colname, int icol) { /*Some Code here*/ string pathSource = HttpContext.Current.Server.MapPath("~/pdf/ExportTemplate.xlsx"); ExpertXls.ExcelLib.ExcelWorkbook workbook = new ExpertXls.ExcelLib.ExcelWorkbook(@pathSource); workbook.LicenseKey = Inq.Configuration.Settings.Value("ExpertPdfLicenseKey"); ExpertXls.ExcelLib.ExcelWorksheet ws = workbook.Worksheets["ImportTemplate"]; ExpertXls.ExcelLib.ExcelCellStyle Style1 = workbook.Styles.AddStyle("Style1"); Style1.Fill.FillType = ExpertXls.ExcelLib.ExcelCellFillType.SolidFill; Style1.Fill.SolidFillOptions.BackColor = Color.Yellow; foreach (string cols in colname.Split(',')) { ws[cols].Style = Style1; } /*Some Code here*/ } } } 

如果工作簿是活动工作簿,则可以使用Application.ActiveWorkbook.FullName 。 你也可以尝试使用workbook.Path 。 请参阅链接 。

添加下面的代码工作。 我已经保存在“pathsource”工作簿

 System.IO.FileStream fs = new FileStream(pathSource, FileMode.OpenOrCreate, FileAccess.ReadWrite); string fileName = fs.Name; try { workbook.Save(fs); } catch (Exception ex) { Logger.Error(" Error:", ex); } finally { workbook.Close(); }