Execl导出系统

前台代码:

<button class="btn btn-warning" type="button" onclick="location.href = '/DownExpress/DownExpress2'">
导出报表
</button>

后台代码:

public void DownExpress2()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("序号", typeof(string)));
dt.Columns.Add(new DataColumn("计划单号", typeof(string)));
dt.Columns.Add(new DataColumn("出库日期", typeof(string)));
dt.Columns.Add(new DataColumn("客户名称", typeof(string)));
dt.Columns.Add(new DataColumn("状态", typeof(string))); 

#region 补填表结构+数据
List<SaleOutboundOrderChild> saleoutboundorderchild = new List<SaleOutboundOrderChild>();
if (Session["SaleOutboundOrderChild"] != null)
saleoutboundorderchild = (List<SaleOutboundOrderChild>)Session["SaleOutboundOrderChild"];
if (saleoutboundorderchild.Count() == 0)
Response.Write("<script type='text/javascript'>alert('没有查询到数据!')</script>");
int num = 1;
foreach (var item in saleoutboundorderchild)
{
string TPrice = item.TPrice.ToString();
if (item.SaleOutboundOrder.BackID != null)
TPrice = "-" + TPrice;
string TypeClassRollName = "";
if (item.TypeClassRoll != null)
TypeClassRollName = item.TypeClassRoll.Name;
double? Zprice = 0;
var m = db.StockInfo.FirstOrDefault(n => n.Code == item.MaterialCoding);
if (m != null)
Zprice = m.UnitPrice * item.TQuantity;
if (item.SaleOutboundOrder.BackHouse == 1)
Zprice = -Zprice;

string TypeName = "";
if (item.MaterielTwo != null)
{
TypeName = item.MaterielTwo.TypeName.Replace("	", "");

}
dt.Rows.Add(num++, item.MarketingPlanChild.MarketingPlan.OrderNo, Common.Date(item.SaleOutboundOrder.SaleDate), item.MarketingPlanChild.MarketingPlan.Client.AbbreviationName, item.MarketingPlanChild.PStatus );//补填数据 
}
string shtnl = "";
for (int a = 0; a < dt.Columns.Count; a++)
{
shtnl = shtnl + dt.Columns[a] + '	';
}
shtnl = shtnl + "
";
for (int i = 0; i < dt.Rows.Count; i++)
{

for (int j = 0; j < dt.Columns.Count; j++)
{
shtnl = shtnl + dt.Rows[i][j] + '	';
}
shtnl = shtnl + "
";

}
DateTime dtime = DateTime.Now;
#endregion
ComNo.ExportToExcel("application/x-excel", "销售出库单数据.xls", shtnl);
}

 ExportToExcel 

 

        /// <summary>
        /// 导出表格信息
        /// </summary>
        /// <param name="FieldType"></param>
        /// <param name="FileName"></param>
        /// <param name="dt"></param>
        public static void ExportToExcel(string FieldType, string FileName, string dt)
        {
            System.Web.HttpContext.Current.Response.Charset = "utf-8";
            System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
            System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            System.Web.HttpContext.Current.Response.ContentType = FieldType;
            StringWriter tw = new StringWriter();
            System.Web.HttpContext.Current.Response.Output.Write(dt);
            System.Web.HttpContext.Current.Response.Flush();
            System.Web.HttpContext.Current.Response.End();
        }

  

认真工作、认真生活,努力做最好的自己!!!
原文地址:https://www.cnblogs.com/songhuihui/p/12377296.html