简体   繁体   中英

Getting path or file name of Excel workbook

How do I get the Path or the file name of workbook in the below code so that I can get the work book that is edited. I have used ExpertXLS – Excel Spreadsheet Library for .NET - C#/VB.NET here

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*/
           }
     }
}

You may use Application.ActiveWorkbook.FullName if the workbook is active workbook. Also you can try using workbook.Path . Refer the link .

Adding below code worked. I have saved the worked book in "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();
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM