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(""); 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(""); excel_text.Append("");//用户id 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(""); 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(""); 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(""); excel_text.Append(""); excel_text.Append(""); excel_text.Append("");//个性化logo 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(""); 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(""); 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(""); 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("");//服装要求 excel_text.Append(""); excel_text.Append(""); excel_text.Append(""); excel_text.Append("");//收件人地址 excel_text.Append(""); excel_text.Append(""); string[] array = imgs.Split(newstring[] { "," }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < array.Length; i++) { excel_text.Append(""); excel_text.Append(""); excel_text.Append(""); } excel_text.Append(""); excel_text.Append("
"); excel_text.Append("

订单信息表

"); excel_text.Append("

" + "订单号" + "

'" + order_id + "

" + "名称" + "

" + order_name + "

" + "用户id" + "

" + userid + "

" + "身高" + "

" + height + "

" + "肩宽" + "

" + shoulder_width + "

" + "袖长" + "

" + sleeve_length + "

" + "收件人" + "

" + rec_person + "

" + "体重" + "

" + weight + "

" + "胸围" + "

" + breast + "

" + "上臀围" + "

" + upper_hip + "

" + "联系电话" + "

" + phone + "

" + "码数" + "

" + size + "

" + "腰围" + "

" + waist + "

" + "裤长" + "

" + pants_length + "

" + "个性化logo" + "

" + "年龄" + "

" + age + "

" + "臀围" + "

" + hip + "

" + "大腿围" + "

" + thigh + "

" + "跟单客服" + "

" + gencustom + "

" + "服装价格" + "

" + price + "

" + "胚衣寄售" + "

" + isredo + "

" + "件数" + "

" + count + "

" + "身体特殊说明" + "

" + bodyspacial + "

" + "面料厚度" + "

" + mianliaohoudu + "

" + "颜色" + "

" + color + "

" + "面料弹力" + "

" + mianliaotanli + "

" + "面料要求" + "

" + mianliaormk + "

" + "面料难易度" + "

" + nanyi + "

" + "服装要求" + "

" + userrmk + "

" + "收件人地址" + "

" + address + "

"); 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); }

  我们看到,在c#中导出excel,这种方法可以通过画html中table的方式做出excel,而这种方法导出的表格是一个简单表格,在排版上还需要更多的斟酌。

  在前台的时候,务必要注意,不可使用ajax,可以通过使用使用localtion.href="/控制器/excel_generation?参数=参数值"的方法导出表格。

C