. NET to print Excel reports

Source: Internet
Author: User
Tags visual studio 2010

Description: This is a sample of the experiment, when used in the actual project, the General object[,] the object data from the database.

1. Experimental environment

Development platform: Visual Studio 2010

Test Template: Jbtest

excel:office2007 Excel

2. Components used in the project

Why do you choose ASP. Because Office and Visual Studio are Microsoft's development software, with good compatibility, with the above-mentioned. dll components, you can easily implement the import of Excel templates and the export of Excel data.

The ExcelQuicker.dll dynamic link library needs to be downloaded on the Internet and is available in the project. I use this version number earlier. The version number is 2.2.0.0. Today's update version number may change the name of some functions and classes, please use after the test.

3. Webconfig Configuration

4. Project Structure

The above is the overall structure of the project, in which the test page is Default.aspx. The Excelquicker.xml under the Lib folder is the configuration document for the Excelquicker component. Message.xml is the wrong information to import the error times.

5. Report Printing

First in the implementation of the printed page background reference namespace: using Excelquicker.framework;

Use the interfaces provided by the corresponding classes of the Excelquicker.framework space to implement the new report

Eqapplication objapp = new Eqapplication (strFileName, Eqreportformat.excel, strtemplate, false);

Eqworksheet objWS = new Eqworksheet ("Report data");

Data written to Excel document similar to VBA programming, detailed function please refer to code: Default.aspx.cs

Eqrange range = new Eqrange (Objdata, 11, 5, 15, 5); From "11 Rows 5 Columns" to "22 Rows 5 Columns"

Objws.add (range);

Default.aspx.cs Backstage Main code:

protected void Dy_click1 (object sender, EventArgs e)
{
String WJM;
String strtemplate = "Jbtest.xls";//Template file name
Initialize ();//Call the Excel quicker initialization function
String strFileName = session["Yhid"]. ToString ();
String strFileName = "Wangling";
WJM = strFileName;
WJM = Wjm + "_" + strtemplate;
Set_excel (WJM, strtemplate);//Generate and set this Excel table
Response.Redirect (".. \\ExcelQuicker\\TempReports\\ "+ WJM);
}
public void Initialize ()
{

Eqconfiguration.xmlfilename = Httpruntime.appdomainapppath + "Excelquicker\\lib\\message.xml";
Eqconfiguration.language = "EN";
Eqconfiguration.temporaryfilepath = Httpruntime.appdomainapppath + "excelquicker\\tempreports\\";
Eqconfiguration.outputfilepath = Httpruntime.appdomainapppath + "excelquicker\\tempreports\\";
Eqconfiguration.templatefilepath = Httpruntime.appdomainapppath + "jb\\xls\\";
}

void Set_excel (String strFileName, String strtemplate)
{


Eqapplication objapp = new Eqapplication (strFileName, Eqreportformat.excel, strtemplate, false);
Eqworksheet objWS = new Eqworksheet ("Report data");

object[,] objdata, ObjData3, ObjData4, ObjData5, OBJDATA6, ObjData7;
Objdata = new object[5, 1];//for storing data to be populated in Excel

OBJDATA3 = new object[8, 1];

OBJDATA4 = new object[1, 1];
ObjData5 = new object[3, 1];

OBJDATA6 = new object[8, 1];
ObjData7 = new object[8, 1];

String Ren, ren1, Ren2, Ren3;
Objdata[0, 0] = Congye. Text;//Employee End-of-term
objdata[1, 0] = Guanli. Text;//Unit Head
objdata[2, 0] = Zaigang. Text;//Staff
Objdata[3, 0] = Laowu. Text;//Labor dispatch personnel
Objdata[4, 0] =qita0. Text;//Other practitioners



ren = "188" + "person";
Ren1 = "22" + "person";
Ren2 = "33" + "person";
Ren3 = "44" + "person";
Objdata4[0, 0] = average number of ren;//directly engaged in production and business activities
Objdata5[0, 0] = ren1;//: Project Technician
objdata5[1, 0] = ren2;//: First-class construction engineer
objdata5[2, 0] = ren3;//: On-site construction personnel



Eqrange range = new Eqrange (Objdata, 11, 5, 15, 5); From "11 Rows 5 Columns" to "22 Rows 5 Columns"
Objws.add (range);

Eqrange range4 = new Eqrange (OBJDATA4, 30, 3, 30, 3); From "11 Rows 5 Columns" to "22 Rows 5 Columns"
Objws.add (RANGE4);
Eqrange range5 = new Eqrange (OBJDATA5, 32, 6, 34, 6); From "11 Rows 10 Columns" to "22 Rows 10 Columns"
Objws.add (RANGE5);




Objdata7[0, 0] = Pingjun. Text;
objdata7[1, 0] =zaigangpj0. Text;
objdata7[2, 0] = laowupj0. Text;
Objdata7[3, 0] = qitapj. Text;
Range7 1-average number of people in the season//
Eqrange Range7 = new Eqrange (OBJDATA7, 16, 5, 19, 5);
Objws.add (Range7);



Objdata3[0, 0] =congyeg. Text;//Payroll of employees
objdata3[1, 0] = Zaizhig. Text;//Payroll for Employees
objdata3[2, 0] = Laog. Text;//Payroll of labor dispatch personnel
Objdata3[3, 0] = Qitag. Text;//Payroll of other employees

Eqrange range3 = new Eqrange (OBJDATA3, 21, 5, 24, 5); From "11 Rows 10 Columns" to "22 Rows 10 Columns"
Objws.add (Range3);



Objdata6[0, 0] = congyepjg. Text;

objdata6[1, 0] = zaigangpjg. Text;

objdata6[2, 0] =laowupjg. Text;

Objdata6[3, 0] = qitapjg. Text;


Eqrange Range6 = new Eqrange (OBJDATA6, 25,5,28, 5);
Objws.add (Range6);

Fill in the information of the person who submitted
string ryxx = "unit owner:" + DF. Text + "Statistics in charge:" + XF. Text + "Fill in person:" + TF. Text;


Objdata = new object[1, 1];
Objdata[0, 0] = ryxx;
Range = new Eqrange (Objdata, 35, 1, 35, 1);
Objws.add (range);

Fill in the table header unit name below

Objdata = new object[1, 1];
Objdata[0, 0] = "Excel Tabular data export test";
Range = new Eqrange (Objdata, 7, 1, 7, 1);
Objws.add (range);
Objapp.add (OBJWS);

Try
{
Objapp.output ();
}
catch (Exception)
{
Objapp.dispose ();
}
}

6. Effects

Fill in the data, import the data into the corresponding Excel document

After filling in the data, click Export Excelbutton, the interface appears

Results after printing to Excel

Download the above Sample sample: Demo.zip

. NET to print Excel reports

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.