Today we'll share an Excel template code tutorial to see How to open an Excel template file to fill the data saved as a file.
The specific code is rendered as follows:
String strFileName = environment.currentdirectory+@ "/template/template.tpl";
String strsavefilename =
environment.currentdirectory+@ "/excel/" +system.datetime.now.tostring (). Replace (":", ""). Replace ("-", ""). Replace ("", "") +@ ". xls";
Excel.Application thisapplication = new Excel.applicationclass ();
Excel.Workbook ThisWorkBook;
Object missing = System.Reflection.Missing.Value;
Try
{
Loading Excel template files
ThisWorkBook = ThisApplication.Workbooks.Open (strfilename,missing,missing,missing,missing,missing,
missing,missing,missing,missing,missing,missing,missing);
Excel.Worksheet Thissheet = (excel.worksheet) thisworkbook.sheets[1];
thissheet.cells[7,1] = dtend.toshortdatestring ();
Progressbar.value = 30;
Thisapplication.visible = false;
DataRow dr = dt. Rows[0];
for (int i=0; i
{
THISSHEET.CELLS[7,I+2] = Dr[i]. ToString ();
Progressbar.value +=1;
}
Save as new file after data is updated
Thissheet.saveas (strsavefilename,missing,missing,missing,missing,missing,missing,missing,missing);
}
catch{}
Finally
{
Thisapplication.quit ();
ThisWorkBook = null;
ThisApplication = null;
DT = NULL;
}
Try
{//Open the Excel file you just generated
Excel.Workbook NewWorkbook;
NewWorkbook = NewApplication.Workbooks.Open (strsavefilename,missing,missing,missing,missing,
missing,missing,missing,missing,missing,missing,missing,missing);
Excel.Worksheet newsheet = (excel.worksheet) newworkbook.sheets[1];
Newapplication.visible = true;
You can also use System.Diagnostics.Process.Start (Strsavefilename) to open a new file