在单独的页面上打印多个HTML表

本文关键字:HTML 打印 单独 | 更新日期: 2023-09-27 18:02:49

我试图允许用户根据他们在GridView中选择的内容打印多个表。然后它将选择更多的数据(从一个隐藏的GridView)并相应地打印所有的表。我现在正在做的是在我的页面底部的表,给它一个div id,然后使用jJavascript打开该div在一个新的窗口。但是,我希望能够一次打印多个表,甚至可能在单独的页面上打印它们。做这件事有什么特别的方法吗?某种特殊的功能?

Javascript

<script type='text/javascript'>
//<![CDATA[
    function printContent() {
        str = document.getElementById('main-content').innerHTML
        newwin = window.open('', 'printwin', 'left=20,top=30,width=600,height=400')
        newwin.document.write('<HTML>'n<HEAD>'n')
        newwin.document.write('<TITLE>Print Page</TITLE>'n')
        newwin.document.write('<script>'n')
        newwin.document.write('function chkstate(){'n')
        newwin.document.write('if(document.readyState=="complete"){'n')
        newwin.document.write('window.close()'n')
        newwin.document.write('}'n')
        newwin.document.write('else{'n')
        newwin.document.write('setTimeout("chkstate()",2000)'n')
        newwin.document.write('}'n')
        newwin.document.write('}'n')
        newwin.document.write('function print_win(){'n')
        newwin.document.write('window.print();'n')
        newwin.document.write('chkstate();'n')
        newwin.document.write('}'n')
        newwin.document.write('<'/script>'n')
        newwin.document.write('</HEAD>'n')
        newwin.document.write('<BODY onload="print_win()">'n')
        newwin.document.write(str)
        newwin.document.write('</BODY>'n')
        newwin.document.write('</HTML>'n')
        newwin.document.close()
    } //]]> 
</script>

如何填充表

            GridViewRow row = DefaultGrid.SelectedRow;
        int rowIndex = DefaultGrid.SelectedIndex;
        HiddenGrid.SelectedIndex = rowIndex;
        GridViewRow row2 = HiddenGrid.SelectedRow;
        //int id = Convert.ToInt32(row.Cells[25].Text);
        fName = row2.Cells[0].Text;
        lName = row2.Cells[1].Text;
        addr = row2.Cells[2].Text;
        addr2 = row2.Cells[3].Text;
        city = row2.Cells[4].Text;
        state = row2.Cells[5].Text;
        zip = row2.Cells[6].Text;
        country = row2.Cells[7].Text;
        email = row2.Cells[8].Text;
        phone = row2.Cells[9].Text;
        ccType = row2.Cells[10].Text;
        ccNum = row2.Cells[11].Text;
        ccExp = row2.Cells[12].Text;
        length = row2.Cells[13].Text;
        delivery = row2.Cells[14].Text;
        price = row2.Cells[15].Text;
        source = row2.Cells[16].Text;
        joined = row2.Cells[17].Text;
        url = row2.Cells[18].Text;
        orderResults = row2.Cells[19].Text;
        pubName = row2.Cells[20].Text;
        sourceCode = row2.Cells[21].Text;
        dt = row.Cells[1].Text.ToString();

表本身

<div id = 'main-content' style = "overflow: auto; height:50%; width:100%" />
    <table id="details">
    <tr>
    <td style="width: 100px;">Name: </td>
    <td><%=fName %> <%=lName %></td>
    </tr>
    <tr>
    <td>Address: </td>
    <td><%=addr %></td>
    </tr>
    <tr>
    <td>Address 2: </td>
    <td><%=addr2 %></td>
    </tr>
    <tr>
    <td>City: </td>
    <td><%=city %></td>
    </tr>
    <tr>
    <td>State: </td>
    <td><%=state %></td>
    </tr>
    <tr>
    <td>Zip: </td>
    <td><%=zip %></td>
    </tr>
    <tr>
    <td>Country: </td>
    <td><%=country %></td>
    </tr>
    <tr>
    <td>Email: </td>
    <td><a href="mailto:<%=email %>"><%=email %></a></td>
    </tr>
    <tr>
    <td>Phone: </td>
    <td><%=phone %></td>
    </tr>
    <tr>
    <td>CCType: </td>
    <td><%=ccType %></td>
    </tr>
    <tr>
    <td>CCNum: </td>
    <td><%=ccNum %></td>
    </tr>
    <tr>
    <td>CCExp: </td>
    <td><%=ccExp %></td>
    </tr>
    <tr>
    <td>Length: </td>
    <td><%=length %></td>
    </tr>
    <tr>
    <td>Delivery: </td>
    <td><%=delivery %></td>
    </tr>
    <tr>
    <td>Price: </td>
    <td><%=price %></td>
    </tr>
    <tr>
    <td>Source: </td>
    <td><%=source %></td>
    </tr>
    <tr>
    <td>Joined: </td>
    <td><%=joined %></td>
    </tr>
    <tr>
    <td>URL: </td>
    <td><%=url %></td>
    </tr>
    <tr>
    <td>orderResults</td>
    <td><%=orderResults %></td>
    </tr>
    <tr>
    <td>PubName: </td>
    <td><%=pubName %></td>
    </tr>
    <tr>
    <td>Source Code: </td>
    <td><%=sourceCode %></td>
    </tr>
     <tr>
    <td>Date/Time: </td>
    <td><%=dt %></td>
    </tr>
    </table>
    </div>

在单独的页面上打印多个HTML表

你可以使用CSS的page-break-before在单独的页面上打印内容:

<table style="page-break-before: always;">
...
</table>

这将在所有表之前插入分页符。您可能希望将此应用于第一个表之后的所有表。

要确保有一个分页符,使用page-break-before: always CSS样式的每个元素,你想做一个分页符。