1. 主页面代码:
<script language="javascript" type="text/javascript">
//给用户控件中控件注册事件。
function CausePostBackToExport()
{
document.getElementById('<%=btnOffers.ClientID %>').click();
return false;
}
</script>
<div style="position: absolute; z-index: -1"><asp:Button ID="btnOffers" runat="server" onclick="btnOffers_Click"
Text="Button" /></div>
<asp:UpdatePanel ID="UpdatePanel4" runat="server">
<ContentTemplate>
<uc2:OffersUserStatusPart ID="OffersUserStatusPart1" runat="server" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnOffers" />
</Triggers>
</asp:UpdatePanel>
</div>
后台:
protected void btnOffers_Click(object sender, EventArgs e)
{
//调用用户控件的方法;
OffersUserStatusPart1.Export();
}
2:用户控件;
/// <summary>
/// Export the offers of influencer to Excel
/// </summary>
public void Export()
{
DataTable dataSource = GetDataSource();
ConvertHelper.ExportToExcel(Response,dataSource);
}
/// <summary>
/// Export the offers of influencer to Excel
/// </summary>
public void Export()
{
DataTable dataSource = GetDataSource();
ExportToExcel(Response,dataSource);
}
/// Export the data to Excel.
/// </summary>
/// <param name="Response">The response of the current page</param>
/// <param name="dataSource">The data will be export.</param>
/// <param name="displayColumnNames">The data of the column will be export.</param>
/// <param name="displayColumnCaptions">The data will be export</param>
public static void ExportToExcel(HttpResponse Response, DataTable dataSource)
{
Response.AppendHeader("Content-Disposition", "attachment;filename=result.xls");
Response.ContentType = "application/ms-excel";
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
string source = DataTableToExcelTable(dataSource, null, null);
Response.Write(source);
Response.Flush();
Response.End();
}
/// <summary>
/// Export the data to Excel.
/// </summary>
/// <param name="Response">The response of the current page</param>
/// <param name="dataSource">The data will be export.</param>
/// <param name="displayColumnNames">The data of the column will be export.</param>
/// <param name="displayColumnCaptions">The data will be export</param>
public static void ExportToExcel(HttpResponse Response,DataTable dataSource, string[] displayColumnNames, string[] displayColumnCaptions)
{
Response.AppendHeader("Content-Disposition", "attachment;filename=result.xls");
Response.ContentType = "application/ms-excel";
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
string source = DataTableToExcelTable(dataSource, displayColumnNames, displayColumnCaptions);
Response.Write(source);
Response.Flush();
Response.End();
}
/// <summary>
/// Create the xls file stream.
/// </summary>
/// <param name="dt">the data will be export</param>
/// <param name="displayColumnNames">The data of the column will be export.</param>
/// <param name="displayColumnCaptions">The data will be export</param>
/// <returns></returns>
public static string DataTableToExcelTable(DataTable dt, string[] displayColumnNames, string[] displayColumnCaptions)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("<?xml version=\"1.0\"?>");
sb.AppendLine("<?mso-application progid=\"Excel.Sheet\"?>");
sb.AppendLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">");
sb.AppendLine(" <Worksheet ss:Name=\"Sheet1\">");
sb.AppendLine(" <Table>");
sb.AppendLine(" <Row>");
if (displayColumnCaptions != null)
{
for (int i = 0; i < displayColumnCaptions.Length; i++)
sb.AppendLine(" <Cell><Data ss:Type=\"String\">" + displayColumnCaptions[i] + "</Data></Cell>");
}
else if (displayColumnNames != null)
{
for (int i = 0; i < displayColumnNames.Length; i++)
sb.AppendLine(" <Cell><Data ss:Type=\"String\">" + displayColumnNames[i] + "</Data></Cell>");
}
else
{
for (int i = 0; i < dt.Columns.Count; i++)
sb.AppendLine(" <Cell><Data ss:Type=\"String\">" + dt.Columns[i].Caption + "</Data></Cell>");
}
sb.AppendLine(" </Row>");
if (displayColumnNames != null)
{
foreach (DataRow dr in dt.Rows)
{
sb.AppendLine(" <Row>");
foreach (string colName in displayColumnNames)
{
if (dt.Columns[colName].DataType == typeof(DateTime))
{
sb.AppendLine(" <Cell><Data ss:Type=\"String\">" + string.Format("{0:d}", dr[colName]) + "</Data></Cell>");
continue;
}
sb.AppendLine(" <Cell><Data ss:Type=\"String\">" + dr[colName].ToString() + "</Data></Cell>");
}
sb.AppendLine(" </Row>");
}
}
else
{
foreach (DataRow dr in dt.Rows)
{
sb.AppendLine(" <Row>");
Object[] ary = dr.ItemArray;
for (int i = 0; i <= ary.GetUpperBound(0); i++)
{
if (dt.Columns[i].DataType == typeof(DateTime))
{
sb.AppendLine(" <Cell><Data ss:Type=\"String\">" + string.Format("{0:d}", ary[i]) + "</Data></Cell>");
continue;
}
sb.AppendLine(" <Cell><Data ss:Type=\"String\">" + ary[i].ToString() + "</Data></Cell>");
}
sb.AppendLine(" </Row>");
}
}
sb.AppendLine(" </Table>");
sb.AppendLine(" </Worksheet>");
sb.AppendLine("</Workbook>");
return sb.ToString();
}
浙公网安备 33010602011771号