我们知道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(" '" + order_id + " | "); excel_text.Append(" " + "名称" + " | ");//名称 excel_text.Append(" " + order_name + " | "); excel_text.Append(" ||||
" + "用户id" + " | ");//用户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(" " + 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(" " + 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("
" + "个性化logo" + " | ");//个性化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(" " + 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(" " + 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(" " + mianliaormk + " | "); excel_text.Append(" " + "面料难易度" + " | ");//面料难易度 excel_text.Append(" " + nanyi + " | "); excel_text.Append(" ||||
" + "服装要求" + " | ");//服装要求 excel_text.Append(" " + userrmk + " | "); excel_text.Append(" ||||||
" + "收件人地址" + " | ");//收件人地址 excel_text.Append(" " + address + " | "); excel_text.Append(" ||||||
"); excel_text.Append(" |
我们看到,在c#中导出excel,这种方法可以通过画html中table的方式做出excel,而这种方法导出的表格是一个简单表格,在排版上还需要更多的斟酌。
在前台的时候,务必要注意,不可使用ajax,可以通过使用使用localtion.href="/控制器/excel_generation?参数=参数值"的方法导出表格。