首页 > 其他 > 详细

每日一剂 14-6-4

时间:2014-06-07 21:11:40      阅读:351      评论:0      收藏:0      [点我收藏+]

注释 async true

1,当在asp:updatepanel中使用asp:button导出excel时不能下载下来的原因:
asp:updatepanel里的所有控件都是async postback,及不是同步的。
解决方案:使用updatepanel的triggers属性,使其成为普通的postback。同步的。【只有同步的时候才可以识别response.write()中的表头信息】
<Triggers>
<asp:PostBackTrigger ControlID="btn导出" />
</Triggers>


2,导出excel方法

protected void ExportExcelEx(string fileName,
IEnumerable<object> data,
bool hasSeq = false,
ExcelCellsBuilder titleCells = null,
ExcelCellsBuilder footerCells = null)
{
if (data != null && data.Any())
{
ProcessExcelExport(fileName, GetExcelString(data, hasSeq, titleCells, footerCells));
}
}

protected void ExportExcelEx(string fileName,
DataTable data,
bool hasSeq = false,
ExcelCellsBuilder titleCells = null,
ExcelCellsBuilder footerCells = null)
{
if (data != null && data.Rows.Count > 0)
{
ProcessExcelExport(fileName, GetExcelString(data, hasSeq, titleCells, footerCells));
}
}

/// <summary>
/// 将DataTable导出为Excel CJ
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="res">数据源</param>
/// <param name="hasSeq">是否导出序号</param>
/// <param name="titleCells">表头内容</param>
/// <param name="footerCells">表尾内容</param>
protected string GetExcelString(object data,
bool hasSeq = false,
ExcelCellsBuilder titleCells = null,
ExcelCellsBuilder footerCells = null)
{
string tableBody = string.Empty;
int addNum = 0;

string tableFormat = "<table style=‘border-collapse: collapse;‘>{0}{1}{2}{3}</table>";
string thFormat = "<th style=‘border:.5pt solid black;background-color: rgb(217,217,217);‘>{0}</th>";
string trFormat = "<tr>{0}</tr>";
string tdFormat = "<td style=‘border:.5pt black solid‘>{0}</td>";

StringBuilder header = new StringBuilder();
StringBuilder trs = new StringBuilder();

if (data is DataTable)
{
var dt = data as DataTable;

//拼接表头
header.Append("<tr>");

if (hasSeq)
{
addNum++;
header.AppendFormat(thFormat, "序号");
}

foreach (DataColumn item in dt.Columns)
{
header.AppendFormat(thFormat, item.ColumnName);
}

header.Append("</tr>");

for (int i = 0; i < dt.Rows.Count; i++)
{
StringBuilder tds = new StringBuilder();

if (hasSeq)
tds.AppendFormat(tdFormat, i + 1);

for (int j = 0; j < dt.Columns.Count; j++)
{
tds.AppendFormat(tdFormat, dt.Rows[i][j]);
}
trs.AppendFormat(trFormat, tds);
}
}
else if (data is IEnumerable<object>)
{
var objType = data.GetType().GetGenericArguments()[0];
//获取列
IEnumerable<PropertyInfo> columns = objType.GetProperties();
IEnumerable<object> exData = data as IEnumerable<object>;

//拼接表头
header.Append("<tr>");

if (hasSeq)
{
addNum++;
header.AppendFormat(thFormat, "序号");
}
foreach (var item in columns)
{
header.AppendFormat(thFormat, item.Name);
}
header.Append("</tr>");

int rowIndex = 1;
var res = exData.GetEnumerator();
while (res.MoveNext())
{
StringBuilder tds = new StringBuilder();
//序号
if (hasSeq)
tds.AppendFormat(tdFormat, rowIndex);

foreach (var column in columns)
{
var item = res.GetType().GetProperty("Current").GetValue(res, null);
tds.AppendFormat(tdFormat, item.GetType().GetProperty(column.Name).GetValue(item, null));
}
trs.AppendFormat(trFormat, tds);
rowIndex++;
}
}

string titleCellsStr = string.Empty, footerCellsStr = string.Empty;

if (titleCells != null)
{
titleCells.AddExcelColNum(addNum);
titleCellsStr = titleCells.ToString();
}

if (footerCells != null)
{
footerCells.AddExcelColNum(addNum);
footerCellsStr = footerCells.ToString();
}

tableBody = string.Format(tableFormat, titleCellsStr, header, trs, footerCellsStr);
return tableBody;
}

protected void ProcessExcelExport(string fileName, string excelString)
{
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
curContext.Response.Clear();
curContext.Response.Buffer = true;
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
curContext.Response.Charset = "";
curContext.Response.AppendHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");
//解决编码问题终极办法的关键代码
string seiya = excelString + "<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=UTF-8\"/>";
curContext.Response.Output.Write(seiya);
curContext.Response.End();
}

每日一剂 14-6-4,布布扣,bubuko.com

每日一剂 14-6-4

原文:http://www.cnblogs.com/it-aaron/p/3774897.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!