Excel Code Tutorial template file fill data Save as a file

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.