要领一 通过GridView(简评:要领较量简朴,可是只适合生成名目简朴的Excel,且无法保存VBA代码),页面无刷新 复制代码 代码如下: 复制代码 代码如下: 复制代码 代码如下:
aspx.cs部门
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
public partial class DataPage_NationDataShow : System.Web.UI.Page
{
private Data_Link link = new Data_Link();
private string sql;
protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(DataPage_NationDataShow));
}
protected void btnExcel_Click(object sender, EventArgs e)
{
string strExcelName = "MyExcel";
strExcelName = strExcelName.Replace(@"https://www.jb51.net/", "");
Data_Link link = new Data_Link();
string strSQL = this.hidParam.Value;
DataSet ds = new DataSet();
ds = link.D_DataSet_Return(strSQL);//得到想要放入Excel的数据
gvExcel.Visible = true;
gvExcel.DataSource = null;
gvExcel.DataMember = ds.Tables[0].TableName;
gvExcel.DataSource = ds.Tables[0];
gvExcel.DataBind();
ExportToExcel(this.Page, gvExcel, strExcelName);
}
protected void gvExcel_RowDataBound(object sender, GridViewRowEventArgs e)
{ }
public override void VerifyRenderingInServerForm(Control control)
{ }
/// <summary>
/// 东西要领,Excel着力(办理乱码问题)
/// </summary>
/// <param>挪用页面</param>
/// <param>Excel数据</param>
/// <param>文件名</param>
public void ExportToExcel(System.Web.UI.Page page, GridView excel, string fileName)
{
try
{
foreach (GridViewRow row in excel.Rows)
{
for (int i = 0; i < row.Cells.Count; i++)
{
excel.HeaderRow.Cells[i].BackColor = System.Drawing.Color.Yellow;
}
}
excel.Font.Size = 10;
excel.AlternatingRowStyle.BackColor = System.Drawing.Color.LightCyan;
excel.RowStyle.Height = 25;
page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
page.Response.Charset = "utf-8";
page.Response.ContentType = "application/vnd.ms-excel";
page.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>");
excel.Page.EnableViewState = false;
excel.Visible = true;
excel.HeaderStyle.Reset();
excel.AlternatingRowStyle.Reset();
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
excel.RenderControl(oHtmlTextWriter);
page.Response.Write(oStringWriter.ToString());
page.Response.End();
excel.DataSource = null;
excel.Visible = false;
}
catch (Exception e)
{
}
}
}
aspx部门
<head runat="server">
<script type="text/javascript">
//Excel DownLoad
function excelExport(){
var hidText = document.getElementById("hidParam");
hidText.value = "some params";
document.getElementById("ExcelOutput").click();
}
</script>
</head>
<body>
<form runat="server">
<input type="button" value="EXCEL下载" />
<input type="text" runat="server"/>
<asp:Button runat="server" Text= "EXCEL着力" UseSubmitBehavior="false"/>
<asp:GridView runat="server" OnRowDataBound="gvExcel_RowDataBound" Visible="False"></asp:GridView>
</form>
</body>
在适才的aspx.cs代码中
foreach (GridViewRow row in excel.Rows)
{
for (int i = 0; i < row.Cells.Count; i++)
{
excel.HeaderRow.Cells[i].BackColor = System.Drawing.Color.Yellow;
}
}
这部门是给表头添加样式。
有时候为了便于欣赏,需要给交错行添加样式,简朴点的可以用下面这种:
复制代码 代码如下:
excel.AlternatingRowStyle.BackColor = System.Drawing.Color.LightCyan;
可是细看一下会发明它把一整行的样式都改变了,包罗后头那些没有用到的列。
办理步伐是有,不外较量繁琐,就是修改每个单位格的样式。
复制代码 代码如下:
int rowCount = excel.Rows.Count;
int colCount = excel.HeaderRow.Cells.Count;
for (int i = 0; i < rowCount; i++)
{
for(int j=0;j<colCount; j++)
{
excel.Rows[i].Cells[j].BackColor = System.Drawing.Color.LightCyan;
}
}
要领二 通过DataGrid(与要领一基内情同),页面无刷新
aspx.cs部门
复制代码 代码如下: