Asp.net printing (who has a better way)

Source: Internet
Author: User

To print part of the page, but there is no good solution, please use the following method to print the datagrig data on the Web
If anyone has printed the nested datalist printing method in the DataGrid, please secretly tell me :)
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. text;
Using system. IO;

Namespace common
{
/// <Summary>
/// Summary of myprint.
/// </Summary>
Public class datagridprint
{
Public datagridprint ()
{

}

/// <Summary>
/// Spell out the table to be printed
/// </Summary>
/// <Param name = "ds"> dataset </param>
/// <Param name = "DG"> DataGrid </param>
/// <Returns> </returns>
Public static string duplint (Dataset ds, DataGrid DG)
{
//************************************** **************************************** ***********//

Datatable mydatatable = new datatable ();
Mydatatable = Ds. Tables [0];

Int myrow = mydatatable. Rows. count;
Int mycol = DG. Columns. count;

Stringbuilder sb = new stringbuilder ();

 
String colheaders = "<HTML> <body>" +
"<Object ID = 'webbrowser 'width = 0 Height = 0 classid = 'clsid: 8856f961-340a-11d0-a96b-00c04fd705a2 'viewastext> </Object>"
+ "<Table> <tr> ";

For (INT I = 0; I <mycol; I ++)
{
Colheaders + = "<TD>" + DG. Columns [I]. headertext. tostring () + "</TD> ";
}
Colheaders + = "</tr> ";

SB. append (colheaders );

// For (INT I = 0; I <myrow; I ++)
//{
// Sb. append ("<tr> ");
// For (Int J = 0; j <mycol; j ++)
//{
// Sb. append ("<TD> ");
// Sb. append (mydatatable. Rows [I] [J]. tostring (). Trim ());
// Sb. append ("</TD> ");
//}
// Sb. append ("</tr> ");
//}

SB. append ("</table> </body> Colheaders = sb. tostring ();

Colheaders + = "<scrip merge Ge = 'javascript '> webbrowser. execwb (); window. Opener = NULL; window. Close (); </SCRIPT> ";

Return (colheaders. tostring ());

//************************************** **************************************** ***********//

}

/// <Summary>
/// Spell out the table to be printed
/// </Summary>
/// <Param name = "DG"> DataGrid </param>
/// <Returns> </returns>
Public static string duplint (DataGrid DG)
{
Int myrow = DG. Items. count;
Int mycol = DG. Columns. count;

Stringbuilder sb = new stringbuilder ();
String colheaders =
"<Object ID = 'webbrowser 'width = 0 Height = 0 classid = 'clsid: 8856f961-340a-11d0-a96b-00c04fd705a2 'viewastext> </Object>"
+ "<Table> <tr align = center> <TD colspan = 3> current location <TD> </tr> <tr> ";

For (INT I = 0; I <mycol; I ++)
{
Colheaders + = "<TD>" + DG. Columns [I]. headertext. tostring () + "</TD> ";
}
Colheaders + = "</tr> ";

SB. append (colheaders );

For (INT I = 0; I <myrow; I ++)
{
SB. append ("<tr> ");
For (Int J = 0; j <mycol; j ++)
{
SB. append ("<TD> ");
SB. append (DG. items [I]. cells [J]. Text. Trim ());
SB. append ("</TD> ");
}
SB. append ("</tr> ");
}
SB. append ("<tr align = right> <TD colspan = 3> tabulation Date :");
SB. append (system. datetime. Now. tolongdatestring (). Trim ());
SB. append ("<TD> </tr> ");
SB. append ("</table> ");
Colheaders = sb. tostring ();
Colheaders + = "<SCRIPT syntax Ge = 'javascript '> webbrowser. execwb (); window. Opener = NULL; window. Close (); </SCRIPT> ";
Return (colheaders );
}
# Region rewrite the template file to restore the print template Style

# Region modify the content of the template file, construct print data, and rewrite the print Template
/// <Summary>
/// Modify the content of the template file, construct print data, and rewrite the print Template
/// </Summary>
Public static void printview (DataGrid DG)
{
Clearview ();

Stringbuilder htmltext = new stringbuilder ();
Try
{
Using (streamreader sr = new streamreader (system. Web. httpcontext. Current. server. mappath (system. Web. httpcontext. Current. Request. applicationpath) + "\ printwindow.htm "))
{
String line;
While (line = Sr. Readline ())! = NULL)
{
Htmltext. append (line );
}
Sr. Close ();
}
}
Catch
{
System. Web. httpcontext. Current. response. Write ("<SCRIPT> alert ('file read error') </SCRIPT> ");
}
// ---------- Replace the content marked in HTM with the content you want to add
Htmltext. Replace ("model", duplint (DG ));
// ---------- Generate the HTM file ------------------――
Try
{
Using (streamwriter Sw = new streamwriter (system. web. httpcontext. current. server. mappath (system. web. httpcontext. current. request. applicationpath) + "\ print00000000htm", false, system. text. encoding. getencoding ("gb2312 ")))
{
Sw. writeline (htmltext );
Sw. Flush ();
Sw. Close ();
}
}
Catch
{
System. Web. httpcontext. Current. response. Write ("the file cocould not be wirte :");
}

}

# Endregion

/// <Summary>
/// Restore the print Template
/// </Summary>
Public static void Clearview ()
{
Stringbuilder htmltext = new stringbuilder ();
Try
{
If (! File. exists (system. Web. httpcontext. Current. server. mappath (system. Web. httpcontext. Current. Request. applicationpath) + "\ printmediaworkflow htm "))
{
Filestream FS = file. Create (system. Web. httpcontext. Current. server. mappath (system. Web. httpcontext. Current. Request. applicationpath) + "\ printmediaworkflow htm ");
FS. Close ();
}

 

Using (streamwriter Sw = new streamwriter (system. Web. httpcontext. Current. server. mappath (system. Web. httpcontext. Current. Request. applicationpath) + "\ printwindow.htm "))
{
String stabletitle = "";
Stabletitle = stabletitle + "<HTML> \ n ";
Stabletitle = stabletitle + "Stabletitle = stabletitle + "<title> </title> \ n ";
Stabletitle = stabletitle + "<meta http-equiv = \" Content-Type \ "content = \" text/html; charset = gb2312 \ "> \ n ";

# Region template page style
Stabletitle = stabletitle + "<style type = \" text/CSS \ "> \ n ";
Stabletitle = stabletitle + "<! -- \ N ";
Stabletitle = stabletitle + "body, table, TR, TD, P {font-size: 9pt; font-family:; line-Height: 1.4} \ n ";
Stabletitle = stabletitle + ". Print-coltitle \ n ";
Stabletitle = stabletitle + "{\ n ";
Stabletitle = stabletitle + "border-Right: Black 1px solid; \ n ";
Stabletitle = stabletitle + "border-top: Black 0px solid; \ n ";
Stabletitle = stabletitle + "border-left: Black 0px solid; \ n ";
Stabletitle = stabletitle + "border-bottom: Black 2px solid; \ n ";
Stabletitle = stabletitle + "white-space: normal; \ n ";
Stabletitle = stabletitle + "background-color: # eeeeee; \ n ";
Stabletitle = stabletitle + "} \ n ";

 

Stabletitle = stabletitle + ". Print-row \ n ";
Stabletitle = stabletitle + "{\ n ";
Stabletitle = stabletitle + "border-Right: Black 1px solid; \ n ";
Stabletitle = stabletitle + "border-top: Black 0px solid; \ n ";
Stabletitle = stabletitle + "border-left: Black 0px solid; \ n ";
Stabletitle = stabletitle + "border-bottom: Black 1px solid; \ n ";
Stabletitle = stabletitle + "white-space: normal; \ n ";
Stabletitle = stabletitle + "background-color: White; \ n ";
Stabletitle = stabletitle + "} \ n ";

 

Stabletitle = stabletitle + ". Print-body \ n ";
Stabletitle = stabletitle + "{\ n ";
Stabletitle = stabletitle + "border-Right: Black 1px solid; \ n ";
Stabletitle = stabletitle + "border-top: Black 2px solid; \ n ";
Stabletitle = stabletitle + "border-left: Black 2px solid; \ n ";
Stabletitle = stabletitle + "border-bottom: Black 2px solid; \ n ";
Stabletitle = stabletitle + "background-color: White; \ n ";
Stabletitle = stabletitle + "white-space: normal; \ n ";
Stabletitle = stabletitle + "} \ n ";
Stabletitle = stabletitle + ". Print-title {font-size: 12pt; font-family:; line-Height: 1.4} \ n ";
Stabletitle = stabletitle + "--> \ n ";
Stabletitle = stabletitle + "</style> \ n ";
# Endregion

 

Stabletitle = stabletitle + "<script language = \" javascript \ "> function maximizewin () {If (window. screen) {var aw = screen. availwidth; var Ah = screen. availheight; window. moveTo (-4,-4); window. resizeTo (Aw + 9, Ah + 4) ;}} function tableallrowspan (tablename, columnindex) {var objtablename = tablename; var strtmp = \ "\"; var spancount = 1; vaR spanstart = 1; var spanoffset = 0; var rowindex = 0; For (rowin DEX = 1; rowindex <objtablename. rows. length; rowindex ++) {If (strtmp = objtablename. rows (rowindex ). cells (columnindex ). outertext) {spancount ++;} else {strtmp = objtablename. rows (rowindex ). cells (columnindex ). outertext; If (rowindex! = 1) {objtablename. rows (spanstart ). cells (columnindex ). rowspan = spancount; For (spanoffset = 1; spanoffset <spancount; spanoffset ++) {objtablename. rows (spanstart + spanoffset ). cells (columnindex ). style. display = \ "None \" ;}} spanstart = rowindex; spancount = 1 ;}} if (rowindex! = 1) {objtablename. rows (spanstart ). cells (columnindex ). rowspan = spancount; For (spanoffset = 1; spanoffset <spancount; spanoffset ++) {objtablename. rows (spanstart + spanoffset ). cells (columnindex ). style. display = \ "None \";}}}";
Stabletitle = stabletitle + "function tablesomerowspan (tablename, columnindex, brow, erow) {var objtablename = tablename; var strtmp = \" \ "; var spancount = 1; var spanstart = 1; vaR spanoffset = 0; var rowindex = 0; For (rowindex = brow; rowindex <erow; rowindex ++) {If (strtmp = objtablename. rows (rowindex ). cells (columnindex ). outertext) {spancount ++;} else {strtmp = objtablename. rows (rowindex). Cells (columnindex). outertext; If (rowindex! = Brow) {objtablename. rows (spanstart ). cells (columnindex ). rowspan = spancount; For (spanoffset = 1; spanoffset <spancount; spanoffset ++) {objtablename. rows (spanstart + spanoffset ). cells (columnindex ). style. display = \ "None \" ;}} spanstart = rowindex; spancount = 1 ;}} if (rowindex! = Brow) {objtablename. rows (spanstart ). cells (columnindex ). rowspan = spancount; For (spanoffset = 1; spanoffset <spancount; spanoffset ++) {objtablename. rows (spanstart + spanoffset ). cells (columnindex ). style. display = \ "None \" ;}}function tablerowspan (tablename, columnindex) {var objtablename = document. all (tablename); var spancount = 1; var spanstart = 1; var spanoffset = 0; var rowindex = 0; var bcolumnindex = 0; If (columnindex = 0) {tableallrowspan (objtablename, columnindex);} else {bcolumnindex = columnindex-1; for (rowindex = 1; rowindex <objtablename. rows. length; rowindex ++) {spanoffset = objtablename. rows (rowindex ). cells (bcolumnindex ). rowspan; If (spanoffset> 1) {spanstart = rowindex; spancount = spanstart + spanoffset; tablesomerowspan (rows, columnindex, spanstart, spancount); rowindex = rowindex + SpanOffSet-1 ;}}}} maximizewin (); </SCRIPT> \ n ";

Stabletitle = stabletitle + "Stabletitle = stabletitle + "<body> \ n ";
Stabletitle = stabletitle + "model \ n ";
Stabletitle = stabletitle + "</body> \ n ";
Stabletitle = stabletitle + "Sw. writeline (stabletitle );
Sw. Flush ();
Sw. Close ();
}
}
Catch
{
System. Web. httpcontext. Current. response. Write ("<SCRIPT> alert ('file read error') </SCRIPT> ");
}
}
# Endregion
In addition, you can import the data of dataset to the Crystal Report, and then import the data to PDF, Doc, XSL, HTML, etc for printing. below is the method
Crystalreport1 myreport = new crystalreport1 ();
Sqldataprovider = new sqldataprovider ();
Myreport. setdatasource (sqldataprovider. runprocdataset ("show_authors"). Tables [0]); // sqldataprovider. runprocdataset ("show_authors"). Tables [0] is a datatable
Crystaldecisions. Shared. diskfiledestinationoptions diskopts = new diskfiledestinationoptions ();
Myreport. exportoptions. exportdestinationtype = crystaldecisions. Shared. exportdestinationtype. diskfile;
// Myreport. exportoptions. exportformattype = crystaldecisions. Shared. exportformattype. portabledocformat; // PDF
Myreport. exportoptions. exportformattype = crystaldecisions. Shared. exportformattype. wordforwindows; // here is word
Diskopts. diskfilename = "C: \ output.doc ";
Myreport. exportoptions. destinationoptions = diskopts;
Myreport. setdatabaselogon ("sa", "hyaocuk! "," 127.0.0.1 "," pubs ");
Myreport. Export ();

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.