如何使用ASP.net打印标签中的数据?

时间:2012-12-27 07:45:43

标签: c# javascript asp.net printing

在我的asp.net网站上,我需要打印标签内的信息。标签内的信息将来自数据库。 标签将包括客户姓名,地址,电话号码等数据。 我需要将它打印在一张纸上。

在我的网页上,我有一个按钮和一个HTML表格。 HTML表包含客户信息。一切都适合我。我可以得到印刷品。但我希望格式化打印页面上的数据。防爆。中心对齐,有一些字体和字体大小。我无法这样做。

以下是我的打印预览页面enter image description here

这是我的打印页面 enter image description here

很好。但我希望打印页面更加时尚,任何徽标。需要增加文本的大小,表格应该居中。如果我能在打印页面中获得表格的边框,那将是一件好事。基本上我想在打印页面上做一些样式。

我尝试实现printstyles css但我无法获得任何结果。

在“打印”按钮的按钮点击事件期间使用的代码是

protected void btnPrint_Click(object sender, EventArgs e)
{

    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    StringBuilder sb = new StringBuilder();
    sb.Append("<script type = 'text/javascript'>");
    sb.Append("window.onload = new function(){");
    sb.Append("var divToPrint=document.getElementById('print');");
    sb.Append("var printWin = window.open('', '', 'left=0");
    sb.Append(",top=0,width=800,height=600,status=0');");
    //sb.Append("printWin.document.write(<link rel='stylesheet' type='text/css' href='PrintStyle.css' media='print'/>\n);");
    sb.Append("printWin.document.write(divToPrint.outerHTML);");
    sb.Append("printWin.document.close();");
    sb.Append("printWin.focus();");
    sb.Append("printWin.print();}");
    sb.Append("</script>");
    ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", sb.ToString());

}

如果有人可以帮我这样做会很好。

3 个答案:

答案 0 :(得分:2)

试试这个,但是把你的标签放在div里面,并像'mydiv'一样给div一个id,因为我正在使用它。

<html>
<head>
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js" > </script> 
<script type="text/javascript">

function PrintElem(elem)
{
    Popup($(elem).html());
}

function Popup(data) 
{
    var mywindow = window.open('', 'my div', 'height=400,width=600');
    mywindow.document.write('<html><head><title>my div</title>');
    /*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
    mywindow.document.write('</head><body >');
    mywindow.document.write(data);
    mywindow.document.write('</body></html>');

    mywindow.print();
    mywindow.close();

    return true;
}

</script>
</head>

<div id="mydiv">
This will be printed. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a quam at nibh adipiscing interdum. Nulla vitae accumsan ante. 
</div>  
 <input type="button" value="Print Div" onclick="PrintElem('#mydiv')" />
 </body>
 </html>

答案 1 :(得分:0)

此代码仅打印div内容。您应该将css链接或样式标记添加到divToPrint变量中。

   printWin.document.write('<link rel="stylesheet" type="text/css" href="/style.css">'+ document.getElementById('print'));

答案 2 :(得分:-1)

您需要以这样一种形式创建HTML表格结构,其中您的标签放在中间位置并在顶行放置oyur徽标,如下所示

<table width="100%" cellspacing="2" cellpadding="0" style="border: 1px solid black;
    height: 100%;">
    <tr>
        <td colspan="3" align="center">
            Your Logo Comes Here
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td align="center">
            Put Your Lable Here
        </td>
        <td>
        </td>
    </tr>
    <tr>
        <td colspan="3" align="center">
            This is Footer
        </td>
    </tr>
</table>