Code used in the project to open an Excel file and add it to the data

Source: Internet
Author: User
Protected   Void Buttonexport_click ( Object Sender, eventargs E)
{
String Strfilename =   " ../EXCEL/template.xls " ; // "D: \ template \ template.xls ";


String Strsavefilename =   " ../EXCEL/ "   + Datetime. Now. year. tostring () + Datetime. Now. Month. tostring () + Datetime. Now. Day. tostring () + Datetime. Now. Hour. tostring () + Datetime. Now. Minute. tostring () + Datetime. Now. Second. tostring () + Datetime. Now. millisecond. tostring () +   " . Xls " ;
Excel. Application thisapplication =   New Excel. applicationclass ();
Excel. Workbook thisworkbook;
Object Missing = System. reflection. Missing. value;




// Load an Excel template file
Thisworkbook = Thisapplication. workbooks. Open (OK (strfilename), missing );
// Response. Write ("<SCRIPT> alert ('! '); </SCRIPT> ");
Excel. worksheet thissheet = (Excel. worksheet) thisworkbook. Sheets [ 1 ];



String Account = Labelaccount. text;
Datatable dt = Oh. getlaborone (account). Tables [ 0 ];
Datatable dtamout =   New Datatable ();
Dtamout. Columns. Add ( " Personnel ID " );
Dtamout. Columns. Add ( " Name " );
Dtamout. Columns. Add ( " Department " );
Dtamout. Columns. Add ( " Amount to be issued " );
Dtamout. Columns. Add ( " Tax Deduction " );
Dtamout. Columns. Add ( " This real release " );
Dtamout. Columns. Add ( " Project " );
Dtamout. Columns. Add ( " Remarks " );
For ( Int I =   0 ; I < DT. Rows. Count; I ++ )
{
String Username = DT. Rows [I] [ 1 ]. Tostring ();
String Realname = DT. Rows [I] [ 2 ]. Tostring ();
String Yingfae = DT. Rows [I] [ 4 ]. Tostring ();
String Remarks = DT. Rows [I] [ 5 ]. Tostring ();
String Part = Uh. getpart (username );

Datatable dtrate = Oh. getratetable (username, account );
String Amount = Dtrate. Rows [ 0 ] [ 3 ]. Tostring ();

Double Daikoushui = Convert. todouble (yingfae) - Convert. todouble (amount );

Thissheet. cells [ 1 , 1 ] =   " Personnel ID " ;
Thissheet. cells [ 1 , 2 ] =   " Name " ;
Thissheet. cells [ 1 , 3 ] =   " Department " ;
Thissheet. cells [ 1 , 4 ] =   " Amount to be issued " ;
Thissheet. cells [ 1 , 5 ] =   " Tax Deduction " ;
Thissheet. cells [ 1 , 6 ] =   " This real release " ;
Thissheet. cells [ 1 , 7 ] =   " Project " ;
Thissheet. cells [ 1 , 8 ] =   " Remarks " ;

Thissheet. cells [I +   2 , 1 ] = Username;
Thissheet. cells [I +   2 , 2 ] = Realname;
Thissheet. cells [I +   2 , 3 ] = Part;
Thissheet. cells [I +   2 , 4 ] = Yingfae;
Thissheet. cells [I +   2 , 5 ] = Daikoushui. tostring ();
Thissheet. cells [I +   2 , 6 ] = Amount;
Thissheet. cells [I +   2 , 7 ] = Remarks;
Thissheet. cells [I +   2 , 8 ] =   "" ;
}

Thisapplication. Visible =   False ;

Thissheet. saveas (OK (strsavefilename), missing );


// Open the generated Excel File
// Excel. Workbook newworkbook;
// Newworkbook = thisapplication. workbooks. Open (strsavefilename, missing, and missing );
// Excel. worksheet newsheet = (Excel. worksheet) newworkbook. Sheets [1];
// Thisapplication. Visible = true;

// System. Diagnostics. process. Start (strsavefilename );

// You can also use system. Diagnostics. process. Start (strsavefilename); to open a new file.
Response. Redirect (strsavefilename );

}

I haven't figured out how to enable it locally. I finally found Lao Zhu.
I was quite depressed when I saw that Zhu had done well. It was a simple method that I did not expect.

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.