標籤:style io ar os 使用 sp java for on
ASP.NET匯出資料到Excel
該方法只是把asp.net頁面儲存成html頁面只是把尾碼改為xlc不過excel可以讀取,接下連我看看還有別的方式能匯出資料,並利用模版產生。
下面是代碼
建立一個asp.ne的tweb應用程式把代碼粘貼進去就好了
html頁面代碼
<%@ Page language="c#" Codebehind="OutExcel.aspx.cs" AutoEventWireup="false" Inherits="eMeng.Exam.OutPutExcel" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>OutPutExcel</title>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" runat="server">
<Columns>
<asp:BoundColumn></asp:BoundColumn>
</Columns>
</asp:datagrid>
<P>
<asp:Label id="Label1" runat="server">檔案名稱:</asp:Label>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:button id="Button1" runat="server" Text="輸出到Excel"></asp:button></P>
</form>
</body>
</HTML>
接下來是cs頁面裡的代碼
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.Data.SqlClient;
namespace eMeng.Exam
{
/// <summary>
/// OutPutExcel 的摘要說明。
/// </summary>
public class OutPutExcel : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label1;
private DataSet myDS =new DataSet();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置使用者代碼以初始化頁面
if(!Page.IsPostBack)
{
Data_Load();//調用方法填充表格
}
}
/// <summary>
/// 建立資料來源
/// </summary>
/// <returns>DataView</returns>
private void Data_Load()
{
//資料庫連接字串Catalog為指定的資料庫名稱,DataSource為要串連的SQL伺服器名稱
string myConn ="User Id=sa;Password=sa;Initial Catalog=test;Data Source=zxb;Connect Timeout=20";
//查詢字串
string mySQLstr="SELECT * FROM fy";
//串連資料庫操作
SqlConnection myConnection = new SqlConnection(myConn);
//執行SQL語句操作
SqlDataAdapter myDataAdapter = new SqlDataAdapter(mySQLstr,myConnection);
//開啟資料庫
myConnection.Open();
//向DataSet填充資料,填充資料庫伺服器中test庫中的fy表
myDataAdapter.Fill(myDS,"fy");
//向DastaGrid填充資料
DataGrid1.DataSource=myDS;
DataGrid1.DataBind();
}
/// <summary>
/// 輸出到Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button1_Click(object sender, System.EventArgs e)
{
if(TextBox1.Text=="")
{
Response.Write("<SCRIPT language=javascript>");
Response.Write("window.alert(‘‘請輸入檔案名稱‘‘);");
Response.Write("</SCRIPT>");
}
else
{
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312"; //設定了類型為中文防止亂碼的出現
Response.AppendHeader("Content-Disposition","attachment;filename="+TextBox1.Text+".xls"); //定義輸出檔案和檔案名稱
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//設定輸出資料流為簡體中文
Response.ContentType = "application/ms-excel";//設定輸出檔案類型為excel檔案。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
}
}
#region Web Form設計器產生的程式碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設計器支援所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Cells[0].Attributes.Add("style","vnd.ms-excel.numberformat:@");
e.Item.Cells[3].Attributes.Add("style","vnd.ms-excel.numberformat:¥#,###.00");
}
}
}
}
ASP.NET匯出資料到Excel 執行個體介紹