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
- How to create a Chart with a discontinuous range?
- How to create a sparkline from a named range?
- How to open an Excel 2013 Macro Enabled Template?
- How to open an Excel file from stream?
- How to open an existing XLSX workbook and save it as XLS?
- Does XlsIO support Excel files with macros that are digitally signed?
- How to create a simple Excel file?
- How to fill template based data Template Markers
- How to open an existing workbook?
- How to open a CSV file?