[Source Code] Solution to implement web printing in ASP.NET

xiaoxiao2021-03-06  37

Using system;

Using system.data;

Using system.Web;

Using system.text;

Using system.collections;

Using system.Web.ui;

Using system.data.sqlclient;

Using system.Web.ui.webcontrols;

Using system.Web.ui.htmlcontrols;

Using system.io;

Namespace WebPrint

{

///

/// This is a scheme that fully utilizes the IE own object to implement web printing, the main data is derived from the data bonded by the DataGrid control.

/// At the same time, users can customize the basic elements of general reports such as headers, headers, footer, and print lines per page.

/// Author: KG

/// Date: 2005.1.1

/// Source: www.domage.com.

///

Public class clsprint: System.Web.ui.page

{

Private string [] sshoulderleft;

PRIVATE STRING [] SSHOULDERCENTER;

PRIVATE STRING [] SSHOULDERRIGHT;

Private string [] sfooterleft;

Private string [] sfooterCenter;

Private string [] sfooterright;

Private string spageTitle = "";

Private string sspancolumnlist = "";

Private int ipagenumber = 30;

Private DataGrid mydatagrid;

Private static string pageurlbase; // page Basic URL

Public clsprint ()

{

Try

{

String urlsuffix = context.request.url.host;

Urlsuffix = urlsuffix (Context.Request.URL.Port.toString () == "" "": ":" context.request.URL.Port);

Urlsuffix = urlsuffix (Context.Request.ApplicationPath.tostring () == "/" "": context.request.ApplicationPath);

PageURLBase = @ "http: //" urlsuffix;

}

Catch

{

// for design time

}

}

#Region number of rows displayed per page

///

/// The number of rows displayed per page

///

Public Int Pagenumber

{

get

{

Return ipagenumber;

}

set

{

Ipagenumber = value;

}

}

#ndregion

#REGON page title

///

/ / / Page title

///

Public String PageTitle

{

get

{

Return spageTitle;

}

set

{

SpageTitle = Value;}

}

#ndregion

#REGION Grid Control button To merge columns collection

///

/// Grid control key to merge column set, format, 2, 3, 4 ...

///

Public String Spancolumnlist

{

get

{

Return sspancolumnlist;

}

set

{

Sspancolumnlist = value;

}

}

#ndregion

#Region left page eyebrow title, array type [0] [1] ... display order from above

///

/ / / Page title

///

Public String [] Shoulderleft

{

get

{

Return SSHOULDERLEFT;

}

set

{

SSHOULDERLEFT = VALUE;

}

}

#ndregion

# rr 标 标 标 标 标 组 至 至 至 至 至 至 上 至 至

///

/ / / Page title

///

Public String [] ShoulderCenter

{

get

{

Return SSHOULDERCENTER;

}

set

{

SSHOULDERCENTER = VALUE;

}

}

#ndregion

#Region Right pagebrow title, array type [0] [1] ... display order from above

///

/ / / Page title

///

Public String [] ShoulderRight

{

get

{

Return sSholderright;

}

set

{

SSHOULDERRIGHT = Value;

}

}

#ndregion

#Region left foot title, array type [0] [1] ... display order from above

///

/ / / Page title

///

Public String [] Footerleft

{

get

{

Return Sfooterleft;

}

set

{

SfooterLeft = VALUE;

}

}

#ndregion

#Region Mid page headline, array type [0] [1] ... display order from top to bottom

///

/ / / Page title

///

Public string [] FooterCenter

{

get

{

Return SfooterCenter;

}

set

{

SfooterCenter = VALUE;

}

}

#ndregion

#REGION right head title, array type [0] [1] ... display order from above

///

/ / / Page title

///

Public string [] Footerright

{

get

{

Return Sfooterright;

}

set

{

Sfooterright = value;

}

}

#ndregion

#Region Print Grid Control Name

///

/// Printing Grid Control Name

///

Public DataGrid DGDataGrid

{

get

{

Return mydatagrid;

}

set

{

MyDataGrid = value;}

}

#ndregion

#Region Modify the contents of the template file, construct print data, overwritten the print template

///

// / Modify the contents of the template file, construct the print data, and rewrite the print template

///

Public void printview ()

{

Clearview ();

Stringbuilder HTMLText = New StringBuilder ();

Try

{

STREAMREADER SR = New StreamReader (Server.Mappath (Context.Request.ApplicationPath) "//printwindow.htm"))

{

String line;

While (line = sr.readline ())! = null)

{

HtmlText.Append (line);

}

sr.close ();

}

}

Catch

{

Response.write ("