c# mvc如何生成excel
我们知道mvc可以通过js生成excel文件,同样在后台c#中一样可以生成excel文件。
首先我们要在后台中写如下方法:
[HttpGet]
public FileResult excel_generation(string userid,string count,string order_id,string order_name,string rec_person,string height,string shoulder_width,string sleeve_length,string phone,string weight,string breast,string upper_hip,string size,string waist,string pants_length,string age,string hip,string thigh,string price,string userrmk,string address, string imgs,string mianliaormk="",string nanyi ="",string logo="",string gencustom="",string bodyspacial="",string isredo="否",string color="原图色",string mianliaohoudu="",string mianliaotanli="") { var excel_text = new System.Text.StringBuilder(); //excel_text.Append(""); excel_text.Append("
"); excel_text.Append(""); excel_text.Append("
"); string filename = order_id.Trim() + "-" + rec_person.Trim() +"-" + order_name.Trim() + "的订单.xls"; byte[] fileContents = System.Text.Encoding.Default.GetBytes(excel_text.ToString()); return File(fileContents, "application/ms-excel", filename); }"); excel_text.Append(" "); excel_text.Append(""); excel_text.Append(" "); excel_text.Append("订单信息表
"); excel_text.Append(""); excel_text.Append(" "); excel_text.Append("");//订单号 excel_text.Append(" " + "订单号" + "
"); excel_text.Append(" '" + order_id + "
");//名称 excel_text.Append(" " + "名称" + "
"); excel_text.Append(" " + order_name + "
"); excel_text.Append(" "); excel_text.Append("");//用户id excel_text.Append(" " + "用户id" + "
");//----------------------------------------新增 excel_text.Append(" " + userid + "
");//身高 excel_text.Append(" " + "身高" + "
"); excel_text.Append(" " + height + "
");//肩宽 excel_text.Append(" " + "肩宽" + "
"); excel_text.Append(" " + shoulder_width + "
");//袖长 excel_text.Append(" " + "袖长" + "
"); excel_text.Append(" " + sleeve_length + "
"); excel_text.Append(" "); excel_text.Append("");//收件人 excel_text.Append(" " + "收件人" + "
"); excel_text.Append(" " + rec_person + "
");//体重 excel_text.Append(" " + "体重" + "
"); excel_text.Append(" " + weight + "
");//胸围 excel_text.Append(" " + "胸围" + "
"); excel_text.Append(" " + breast + "
");//上臀围 excel_text.Append(" " + "上臀围" + "
"); excel_text.Append(" " + upper_hip + "
"); excel_text.Append(" "); excel_text.Append("");//联系电话 excel_text.Append(" " + "联系电话" + "
"); excel_text.Append(" " + phone + "
");//码数 excel_text.Append(" " + "码数" + "
"); excel_text.Append(" " + size + "
");//腰围 excel_text.Append(" " + "腰围" + "
"); excel_text.Append(" " + waist + "
");//裤长 excel_text.Append(" " + "裤长" + "
"); excel_text.Append(" " + pants_length + "
"); excel_text.Append(" "); excel_text.Append("");//个性化logo excel_text.Append(" " + "个性化logo" + "
"); excel_text.Append(" ");//年龄 excel_text.Append(" " + "年龄" + "
"); excel_text.Append(" " + age + "
");//臀围 excel_text.Append(" " + "臀围" + "
"); excel_text.Append(" " + hip + "
");//大腿围 excel_text.Append(" " + "大腿围" + "
"); excel_text.Append(" " + thigh + "
"); excel_text.Append(" "); excel_text.Append("");//跟单客服 excel_text.Append(" " + "跟单客服" + "
"); excel_text.Append(" " + gencustom + "
");//服装价格 excel_text.Append(" " + "服装价格" + "
"); excel_text.Append(" " + price + "
");//是否重复下单 excel_text.Append(" " + "胚衣寄售" + "
"); excel_text.Append(" " + isredo + "
");//件数 excel_text.Append(" " + "件数" + "
"); excel_text.Append(" " + count + "
"); excel_text.Append(" "); excel_text.Append("");//身体特殊说明 excel_text.Append(" " + "身体特殊说明" + "
"); excel_text.Append(" " + bodyspacial + "
");//面料厚度 excel_text.Append(" " + "面料厚度" + "
"); excel_text.Append(" " + mianliaohoudu + "
");//色号 excel_text.Append(" " + "颜色" + "
"); excel_text.Append(" " + color + "
");//面料弹力 excel_text.Append(" " + "面料弹力" + "
"); excel_text.Append(" " + mianliaotanli + "
"); excel_text.Append(" "); excel_text.Append("");//面料要求 excel_text.Append(" " + "面料要求" + "
"); excel_text.Append(" " + mianliaormk + "
");//面料难易度 excel_text.Append(" " + "面料难易度" + "
"); excel_text.Append(" " + nanyi + "
"); excel_text.Append(" "); excel_text.Append("");//服装要求 excel_text.Append(" " + "服装要求" + "
"); excel_text.Append(" " + userrmk + "
"); excel_text.Append(" "); string[] array = imgs.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < array.Length; i++) { excel_text.Append("");//收件人地址 excel_text.Append(" " + "收件人地址" + "
"); excel_text.Append(" " + address + "
"); excel_text.Append(" "); } excel_text.Append(""); excel_text.Append(""); excel_text.Append(" 我们看到,在c#中导出excel,这种方法可以通过画html中table的方式做出excel,而这种方法导出的表格是一个简单表格,在排版上还需要更多的斟酌。
在前台的时候,务必要注意,不可使用ajax,可以通过使用使用localtion.href="/控制器/excel_generation?参数=参数值"的方法导出表格。
相关