Asp.net exports data as an Excel report

Source: Internet
Author: User

When making an Excel file, you only need to define the template file and then fill in the data. Therefore, in order to reduce the error rate of new users and simplify development, a general base class is written to form a report, as long as the following code simply inherits the base class.

The Code is as follows: Copy code

Using System;
Using System. Data;
Using Lee. Library. Reports. Office. Excel;
 
Namespace Lee. Sample. Reports. Office. Excel
{
Public class SampleReport: BaseReport <DataSet>
{
Public SampleReport (DataSet ds): base (ds ){}
 
Protected override void InitReport ()
{
This. ReportName = @ "UserInfo ";
This. ReportSavePath = AppDomain. CurrentDomain. BaseDirectory + @ "ReportsOfficeExcel ";
This. Excel = new WorkBook (AppDomain. CurrentDomain. BaseDirectory + @ "ReportsOfficeExcelUserInfo.xls", "UserInfo ");
This. Excel. Sheets. Add (new WorkSheet ("MyUserInfo "));
For (int I = 0; I <this. DataSource. Tables [0]. Rows. Count; I ++)
{
DataRow dr = this. DataSource. Tables [0]. Rows [I];
This. excel. sheets [0]. add (new WorkCell ("B" + (I + 3 ). toString (), dr. field <string> ("ID ")));
This. excel. sheets [0]. add (new WorkCell ("C" + (I + 3 ). toString (), dr. field <string> ("NAME ")));
}
}
}
}

Related Article

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.