asp.net打印(谁有更好的办法)
因为要做打印页面一部分,但是一直没有找到好的解决方案,请下面是一个WEB中打印datagrig数据的方法
如果谁有打印datagrid中嵌套datalist打印方法,请偷偷的告诉我一下:)
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>
/// myPrint 的摘要说明。
/// </summary>
public class DatagridPrint
{
public DatagridPrint()
{
}
/// <summary>
/// 拼出要打印的表
/// </summary>
/// <param name="ds">DATASET</param>
/// <param name="dg">DATAGRID</param>
/// <returns></returns>
public static string DGPrint(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></html>");
colHeaders=sb.ToString();
colHeaders+="<scrip languge='javascript'>WebBrowser.ExecWB(7,1); window.opener=null;window.close();</script>" ;
return(colHeaders.ToString());
//*****************************************************************************************//
}
/// <summary>
/// 拼出要打印的表
/// </summary>
/// <param name="dg">datagrid</param>
/// <returns></returns>
public static string DGPrint(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>当前位置<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>制表日期:");
sb.Append(System.DateTime.Now.ToLongDateString().Trim());
sb.Append("<td></tr>");
sb.Append("</table>");
colHeaders=sb.ToString();
colHeaders+="<script languge='javascript'>WebBrowser.ExecWB(7,1); window.opener=null;window.close();</script>" ;
return(colHeaders);
}
#region 重写模板文件,恢复打印模板样式
#region 修改模板文件的内容,构造打印数据,重写打印模板
/// <summary>
/// 修改模板文件的内容,构造打印数据,重写打印模板
/// </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('读取文件错误')</Script>");
}
//----------替换htm里的标记为你想加的内容
htmltext.Replace("Model",DGPrint(dg));
//----------生成htm文件------------------――
try
{
using(StreamWriter sw=new StreamWriter( System.Web.HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath) + "\\printWindow.htm",false,System.Text.Encoding.GetEncoding("GB2312")))
{
sw.WriteLine(htmltext);
sw.Flush();
sw.Close();
}
}
catch
{
System.Web.HttpContext.Current.Response.Write ("The file could not be wirte:");
}
}
#endregion
/// <summary>
/// 恢复打印模板
/// </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) + "\\printWindow.htm"))
{
FileStream fs = File.Create(System.Web.HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath) + "\\printWindow.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 + "<head>\n";
sTableTitle = sTableTitle + "<title></title>\n";
sTableTitle = sTableTitle + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=GB2312\">\n";
#region 模板页的样式
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 ( RowIndex = 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 (objTableName, ColumnIndex ,SpanStart, SpanCount); RowIndex = RowIndex + SpanOffSet-1;}}}} maximizeWin();</script>\n";
sTableTitle = sTableTitle + "</head>\n";
sTableTitle = sTableTitle + "<body>\n";
sTableTitle = sTableTitle + "Model\n";
sTableTitle = sTableTitle + "</body>\n";
sTableTitle = sTableTitle + "</html>\n";
sw.WriteLine(sTableTitle);
sw.Flush();
sw.Close();
}
}
catch
{
System.Web.HttpContext.Current.Response.Write("<Script>alert('读取文件错误')</Script>");
}
}
#endregion
另外,还可以把dataset的数据导入到水晶报表,然后再倒入pdf,doc,xsl,html等中进行打印,下面是方法
CrystalReport1 myreport = new CrystalReport1();
SqlDataProvider sqldataprovider =new SqlDataProvider();
myreport.SetDataSource(sqldataprovider.RunProcDataSet("Show_authors").Tables[0]);//sqldataprovider.RunProcDataSet("Show_authors").Tables[0]是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;//这里是word
DiskOpts.DiskFileName = "c:\\Output.doc";
myreport.ExportOptions.DestinationOptions = DiskOpts;
myreport.SetDatabaseLogon("sa", "hyaocuk!", "127.0.0.1", "pubs");
myreport.Export();