Cybersecurity
DevOps Cloud
IT Operations Cloud
OpenText product name changes coming to the community soon! Learn more.
Example of how to access Excel from C# scripted rule on SV 4.0 and newer:
SV uses open source NPOI library to work with Excel files, so you can utilize it in your script too.
using System.IO; using HP.SV.CSharp; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; namespace HP.SV{ public class CSharpRule { private const string ExcelFile = @"d:\sample\excel.xlsx"; private const string SheetName = "Sheet1"; public static void Execute(HpsvRootObject sv) { WriteRequestToExcel(sv); ReadResponseFromExcel(sv); } /// <summary> /// Reads cell A2 from excel and writes its content to a response. /// </summary> private static void ReadResponseFromExcel(HpsvRootObject sv) { // read IWorkbook workbook = ReadWorkbook(ExcelFile); // get value ISheet sheet = workbook.GetSheet(SheetName); ICell cell = sheet.GetRow(0).GetCell(1); string content = cell.ToString(); // write Excel value to response sv.Response.response = content; } /// <summary> /// Writes request content to A1 /// </summary> private static void WriteRequestToExcel(HpsvRootObject sv) { // read IWorkbook workbook = ReadWorkbook(ExcelFile); // modify value in an Excek sheet ISheet sheet = workbook.GetSheet(SheetName); ICell cell = sheet.GetRow(0).GetCell(0); cell.SetCellValue(sv.Request.request); // write back to file WriteWorkbook(workbook, ExcelFile); } private static IWorkbook ReadWorkbook(string file) { IWorkbook workBook; using (var fs = File.OpenRead(file)) { workBook = new XSSFWorkbook(fs); } return workBook; } private static void WriteWorkbook(IWorkbook workbook, string file) { using (var fs = File.Open(file, FileMode.Create)) { workbook.Write(fs); } } } }