asp.net生成Excel并导出下载五种实现方法

方法一 通过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;


但是细看一下会发现它把一整行的样式都改变了,包括后面那些没有用到的列。

asp.net生成Excel并导出下载五种实现方法

解决办法是有,不过比较繁琐,就是修改每个单元格的样式。

复制代码 代码如下:


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部分

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/ad561b62f74f90bf7c48b2be1ddf4f15.html