首页 > 代码库 > 导出excel-文件流方式

导出excel-文件流方式

前台页面

技术分享

js

 技术分享

controller

技术分享

后台代码

 

/// <summary>
/// 返回导出excel的数据datatable
/// </summary>
/// <returns></returns>

 

public void export(int pageSize,int pageIndex,string marker)
{

  //获取数据源dt

StringBuilder sql = new StringBuilder();
sql.Append(" ");
if (marker.Equals("1"))
{
    sql.AppendFormat(" ", pageSize, pageSize * pageIndex);
}
else {
    sql.Append(" ");
}
DBHelper db = new DBHelper();
DataTable dt_0 = db.QueryData(sql.ToString()).Tables[0];

 //设置往excel中遍历填充数据的dt

  DataTable dt_1 = new DataTable();

  --//设置虚拟表的栏位
for (var j = 0; j < 26; j++) {
DataColumn field = null;
switch (j)
  {

     case 0: field = new DataColumn("装置名称"); dt_1.Columns.Add(field); continue;
     case 1: field = new DataColumn("设备位号"); dt_1.Columns.Add(field); continue;

  }

}

//遍历dt_0
foreach (DataRow row in dt_0.Rows)
{
   DataRow dataRow_1 = dt_1.NewRow();

   dataRow_1[0] = list_0[0].UnitName;//单元格数据
   dataRow_1[1] = list_1[0].OrganiseName;

   dt_1.Rows.Add(dataRow_1);

}

//excel的栏位数据

 List<string> tabletitle_0 = new List<string>();

   tabletitle_0.Add("装置名称");//0
   tabletitle_0.Add("设备位号");//1
   tabletitle_0.Add("设备名称");//2

tabletitle_0.Add("车间名称");//3
tabletitle_0.Add("单位名称");//4
tabletitle_0.Add("电机参数");//5
tabletitle_0.Add("工作及参数");//6
tabletitle_0.Add("状态");//7
List<string> tabletitle_1 = new List<string>();
tabletitle_1.Add("设备型号");
tabletitle_1.Add("设备类型");
tabletitle_1.Add("前轴承型号");
tabletitle_1.Add("后轴承型号");
tabletitle_1.Add("电压(V)");
tabletitle_1.Add("设备评级");
tabletitle_1.Add("出厂日期");
tabletitle_1.Add("使用年限");
tabletitle_1.Add("生产厂家");
tabletitle_1.Add("安装时间");
tabletitle_1.Add("设备型号");
tabletitle_1.Add("设备类型");
tabletitle_1.Add("前轴承型号");
tabletitle_1.Add("后轴承型号");
tabletitle_1.Add("电压(V)");
tabletitle_1.Add("设备评级");
tabletitle_1.Add("出厂日期");
tabletitle_1.Add("使用年限");
tabletitle_1.Add("生产厂家");
tabletitle_1.Add("安装时间");

//画excel

    --//画栏位    

StringBuilder sHtml = new StringBuilder();
sHtml.Append("<meta http-equiv=‘content-type‘ content=‘application/ms-excel; charset=UTF-8‘/>");
sHtml.Append("<table border=1>");
sHtml.Append("<tr style=‘background-color:#D8DFF1;‘>");
var i = 0;
foreach (String s in tabletitle_0)
{
if (i == 5 || i == 6)
{
sHtml.Append("<td colspan=‘10‘align=‘center‘>");
sHtml.Append(s);
sHtml.Append("</td>");
i++;
}
else if (i == 7) {
sHtml.Append("<td rowspan=‘2‘>");
sHtml.Append(s);
sHtml.Append("</td>");
}
else
{
sHtml.Append("<td rowspan=‘2‘>");
sHtml.Append(s);
sHtml.Append("</td>");
i++;
}
}
sHtml.Append("</tr>");
sHtml.Append("<tr style=‘background-color:#D8DFF1;‘>");
foreach (String ss in tabletitle_1)
{
sHtml.Append("<td>");
sHtml.Append(ss);
sHtml.Append("</td>");
}
sHtml.Append("</tr>");

//画数据单元格

foreach (DataRow row in dt_1.Rows)
{
sHtml.Append("<tr>");
foreach (String cell in row.ItemArray)
{
sHtml.Append("<td>");
sHtml.Append(cell);
sHtml.Append("</td>");
}
sHtml.Append("</tr>");
}
sHtml.Append("</table>");

//定义流

System.Web.HttpContext.Current.Response.Charset = "GB2312";
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DateTime.Now.ToString("yyyyMMddhhmmsss") + ".xls", System.Text.Encoding.UTF8).ToString());
System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";
System.Web.HttpContext.Current.Response.Output.Write(sHtml);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
}

导出excel-文件流方式