ASP.NET 列印、匯出
我是菜鳥,我是新手。前幾天寫程式時,應客戶要求加上列印和資料匯出功能,在過去從未做過類似的,就在網上搜了一搜,結果云云,方法雲雲。試過好多都要出錯。於是乎,拿過來好幾個,相比對照之下,終於弄出來了,記錄下來,以後會用得著,也給予同我一樣的新手借鑑。
匯出功能,還是並非我最想要的(怎麼能把瀏覽器下載那個提示框取消了,就如同儲存圖片一樣),有高手路過,請指點一二......
************匯出GridView資料************
1、頁面中新增:
<asp:LinkButton ID="lbtn_Excel" runat="server" OnClick="lbtn_Excel_Click"><img src="../icon/excel.jpg" height="20" align="absmiddle" border="0" />匯出</asp:LinkButton>
2、後置程式碼,lbtn_Excel_Click方法:
protected void lbtn_Excel_Click(object sender, EventArgs e)
{
string FileName = "MateOutList(" + DateTime.Now.ToShortDateString()+"_" +DateTime.Now.ToShortTimeString() + ").xls";
//匯出Excel
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=" + FileName);
Response.Charset="GB2312";
Response.ContentType="application/vnd.xls";
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
System.IO.StringWriter strW=new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlTW=new HtmlTextWriter(strW);
//GridView去樣式
System.Drawing.Color oldbackcolor = gv_Excel.HeaderStyle.BackColor;
System.Drawing.Color oldforecolor = gv_Excel.HeaderStyle.ForeColor;
GridView1.RenderControl(htmlTW);
Response.Write(strW.ToString());
Response.End();
}
3、切記重寫 VerifyRenderingInServerForm方法
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
4、如果LinkButton是在UpdatePanel<ContentTemplate></ContentTemplate>之間,切記在觸發器中寫上去<asp:PostBackTrigger ControlID="lbtn_Excel" />,如:
<Triggers>
<asp:PostBackTrigger ControlID="lbtn_Excel" />
</Triggers>
*************OK,原來匯出就是如此簡單。
**********列印************
1、頁面新增:
<asp:LinkButton ID="lbtn_Print" runat="server" OnClientClick="return PintPage();"><img src="../icon/print.jpg" height="20" align="absmiddle" border="0" />列印</asp:LinkButton>
<script language="javascript" type="text/javascript">
function PintPage()
{
//指向列印頁面
window.showModalDialog('PrintMateOut.aspx?MainID='+ document.getElementById('hid_billid').value+'&SubID='+document.getElementById('hid_sub_id').value,'','dialogHeight:375px;dialogWidth:740px;help:0;');
}
</script>
2、列印頁面,後置程式碼並沒什麼東西,前臺用JavaScript處理了:
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"></meta>
<script language="javascript" type="text/javascript">
function doPage()
{
layLoading.style.display = "none";
}
//設定網頁列印的頁首頁尾為空
function PageSetup_Null()
{
try
{
var Wsh=new ActiveXObject("WScript.Shell");
HKEY_Key="header";
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");
HKEY_Key="footer";
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");
}
catch(e){}
}
//設定網頁列印的頁首頁尾為預設值
function PageSetup_Default()
{
try
{
var Wsh=new ActiveXObject("WScript.Shell");
HKEY_Key="header";
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"&w&b頁碼,&p/&P");
HKEY_Key="footer";
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"&u&b&d");
}
catch(e){}
}
/*列印函式
*doType 頁面頁尾型別 0 設定為空,1 設定為預設
*/
function PrintTable(doType) //列印函式
{
if(doType == '0')
{
PageSetup_Null();
}
else
{
PageSetup_Default();
}
document.getElementById("btn_Print").style.display = "none";
document.getElementById("btn_NoPrint").style.display = "none";
window.print();
document.getElementById("btn_Print").style.display = "";
document.getElementById("btn_NoPrint").style.display = "";
window.location.reload();
}
<body>
<!-------------------------要列印部分------------------------
<table>
要列印的表格
</table>
-------------------------要列印部分------------------------->
<table cellpadding="0" cellspacing="0" border="0" style="width: 666px;">
<tr>
<td align="right" style="height: 27px;">
<asp:Button ID="btn_Print" runat="server" Text="確認列印" OnClientClick="PrintTable('0')"
Height="25px" Width="80px" />
<asp:Button ID="btn_NoPrint" runat="server" Text="取消列印" OnClientClick="JavasCript:window.close();"
Height="25px" Width="80px" />
</td>
</tr>
</table>
</body>
*************OK,列印也就此搞定。
如果有疑問或著有改進,可給我留言,呵呵,共同探討,共同進步。