How to create and open Excel Template files by using XlsIO?

8 Dec 20234 minutes to read

Creating Excel Template Files

Excel template files (XLT or XLTX) can be created in XlsIO by saving a file as Template using ExcelSaveType enumeration. The following code snippet illustrates this.

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Save as XLT
  workbook.Version = ExcelVersion.Excel97to2003;
  FileStream stream = new FileStream("XLTFile.xlt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  workbook.SaveAs(stream, ExcelSaveType.SaveAsTemplate);

  //Save as XLTX
  workbook.Version = ExcelVersion.Excel2007;
  FileStream stream1 = new FileStream("XLTXFile.xltx", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  workbook.SaveAs(stream1, ExcelSaveType.SaveAsTemplate);
  workbook.Close();
  excelEngine.Dispose();
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Save as XLT
  workbook.Version = ExcelVersion.Excel97to2003;
  workbook.SaveAs("XLTFile.xlt", ExcelSaveType.SaveAsTemplate);

  //Save as XLTX
  workbook.Version = ExcelVersion.Excel2007;
  workbook.SaveAs("XLTXFile.xltx", ExcelSaveType.SaveAsTemplate);
}
Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Save as XLT
  workbook.Version = ExcelVersion.Excel97to2003
  workbook.SaveAs("XLTFile.xlt", ExcelSaveType.SaveAsTemplate)

  'Save as XLTX
  workbook.Version = ExcelVersion.Excel2007
  workbook.SaveAs("XLTXFile.xltx", ExcelSaveType.SaveAsTemplate)
End Using

Opening Excel Template Files

In XlsIO, an Excel template file is opened in the same way, as excel workbook (.xls and .xlsx) is opened. The following code snippet illustrates this.

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  FileStream inputStream = new FileStream("Sample.xltx", FileMode.Open, FileAccess.Read);
  IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);

  FileStream stream = new FileStream("Output.xlsx", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  workbook.Close();
  excelEngine.Dispose();
}
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;

//Open Excel Template.
IWorkbook workbook = application.Workbooks.Open("Sample.xltx", ExcelOpenType.Automatic);
workbook.SaveAs("Output.xlsx");
workbook.Close();
excelEngine.Dispose();
Dim excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Excel2013

'Open Excel Template.
Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xltx", ExcelOpenType.Automatic)
workbook.SaveAs("Output.xlsx")
workbook.Close()
excelEngine.Dispose()

See Also