c# mvc如何生成excel

时间:2022-02-21 07:04:17

  我们知道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("<table width='400 height='392' border='1' cellpadding='1'>");
excel_text.Append("<table border='1' cellpadding='1'>");
excel_text.Append("<tbody>");
excel_text.Append("<tr>");
excel_text.Append("<td height='44' colspan='8'><div align='center'>");
excel_text.Append("<h2><strong>订单信息表</strong></h2>");
excel_text.Append("</div></td>");
excel_text.Append("</tr>"); excel_text.Append("<tr>");
excel_text.Append("<td height='28'><strong><p align='center'>" + "订单号" + "</p></strong></td>");//订单号
excel_text.Append("<td><p align='center'>'" + order_id + "</p></td>");
excel_text.Append("<td><strong><p align='center'>" + "名称" + "</p></strong></td>");//名称
excel_text.Append("<td colspan='5'><p align='center'>" + order_name + "</p></td>");
excel_text.Append("</tr>"); excel_text.Append("<tr>");
excel_text.Append("<td height='28' width='106'><strong><p align='center'>" + "用户id" + "</p></strong></td>");//用户id
excel_text.Append("<td width='184'><p align='center'>" + userid + "</p></td>");//----------------------------------------新增
excel_text.Append("<td width='106'><strong><p align='center'>" + "身高" + "</p></strong></td>");//身高
excel_text.Append("<td width='78'><p align='center'>" + height + "</p></td>");
excel_text.Append("<td width='82'><strong><p align='center'>" + "肩宽" + "</p></strong></td>");//肩宽
excel_text.Append("<td width='78'><p align='center'>" + shoulder_width + "</p></td>");
excel_text.Append("<td width='79'><strong><p align='center'>" + "袖长" + "</p></strong></td>");//袖长
excel_text.Append("<td width='81'><p align='center'>" + sleeve_length + "</p></td>");
excel_text.Append("</tr>"); excel_text.Append("<tr>");
excel_text.Append("<td height='28'><strong><p align='center'>" + "收件人" + "</p></strong></td>");//收件人
excel_text.Append("<td><p align='center'>" + rec_person + "</p></td>"); excel_text.Append("<td><strong><strong><p align='center'>" + "体重" + "</p></strong></td>");//体重
excel_text.Append("<td><p align='center'>" + weight + "</p></td>");
excel_text.Append("<td><strong><strong><p align='center'>" + "胸围" + "</p></strong></td>");//胸围
excel_text.Append("<td><p align='center'>" + breast + "</p></td>");
excel_text.Append("<td><strong><strong><p align='center'>" + "上臀围" + "</p></strong></td>");//上臀围
excel_text.Append("<td><p align='center'>" + upper_hip + "</p></td>");
excel_text.Append("</tr>"); excel_text.Append("<tr>");
excel_text.Append("<td height='28'><strong><p align='center'>" + "联系电话" + "</p></strong></td>");//联系电话
excel_text.Append("<td><p align='center'>" + phone + "</p></td>"); excel_text.Append("<td><strong><p align='center'>" + "码数" + "</p></strong></td>");//码数
excel_text.Append("<td><p align='center'>" + size + "</p></td>");
excel_text.Append("<td><strong><p align='center'>" + "腰围" + "</p></strong></td>");//腰围
excel_text.Append("<td><p align='center'>" + waist + "</p></td>");
excel_text.Append("<td><strong><p align='center'>" + "裤长" + "</p></strong></td>");//裤长
excel_text.Append("<td><p align='center'>" + pants_length + "</p></td>");
excel_text.Append("</tr>"); excel_text.Append("<tr>");
excel_text.Append("<td height='28'><strong><p align='center'>" + "个性化logo" + "</p></strong></td>");//个性化logo
excel_text.Append("<td align='center'><img src='" + logo + "' height='28'/></td>"); excel_text.Append("<td><strong><p align='center'>" + "年龄" + "</p></strong></td>");//年龄
excel_text.Append("<td><p align='center'>" + age + "</p></td>");
excel_text.Append("<td><strong><p align='center'>" + "臀围" + "</p></strong></td>");//臀围
excel_text.Append("<td><p align='center'>" + hip + "</p></td>");
excel_text.Append("<td><strong><p align='center'>" + "大腿围" + "</p></strong></td>");//大腿围
excel_text.Append("<td><p align='center'>" + thigh + "</p></td>");
excel_text.Append("</tr>"); excel_text.Append("<tr>");
excel_text.Append("<td height='28'><strong><p align='center'>" + "跟单客服" + "</p></strong></td>");//跟单客服
excel_text.Append("<td><p align='center'>" + gencustom + "</p></td>");
excel_text.Append("<td><strong><p align='center'>" + "服装价格" + "</p></strong></td>");//服装价格
excel_text.Append("<td><p align='center'>" + price + "</p></td>"); excel_text.Append("<td><strong><p align='center'>" + "胚衣寄售" + "</p></strong></td>");//是否重复下单
excel_text.Append("<td><p align='center'>" + isredo + "</p></td>"); excel_text.Append("<td><strong><p align='center'>" + "件数" + "</p></strong></td>");//件数
excel_text.Append("<td><p align='center'>" + count + "</p></td>");
excel_text.Append("</tr>"); excel_text.Append("<tr>");
excel_text.Append("<td height='28'><strong><p align='center'>" + "身体特殊说明" + "</p></strong></td>");//身体特殊说明
excel_text.Append("<td><p align='center'>" + bodyspacial + "</p></td>");
excel_text.Append("<td ><strong><p align='center'>" + "面料厚度" + "</p></strong></td>");//面料厚度
excel_text.Append("<td><p align='center'>" + mianliaohoudu + "</p></td>");
excel_text.Append("<td><strong><p align='center'>" + "颜色" + "</p></strong></td>");//色号
excel_text.Append("<td><p align='center'>" + color + "</p></td>");
excel_text.Append("<td><strong><p align='center'>" + "面料弹力" + "</p></strong></td>");//面料弹力
excel_text.Append("<td><p align='center'>" + mianliaotanli + "</p></td>");
excel_text.Append("</tr>");
excel_text.Append("<tr>");
excel_text.Append("<td height='28'><strong><p align='center'>" + "面料要求" + "</p></strong></td>");//面料要求
excel_text.Append("<td colspan='3'><p align='center'>" + mianliaormk + "</p></td>");
excel_text.Append("<td colspan='2'><strong><p align='center'>" + "面料难易度" + "</p></strong></td>");//面料难易度
excel_text.Append("<td colspan='2'><p align='center'>" + nanyi + "</p></td>");
excel_text.Append("</tr>"); excel_text.Append("<tr>");
excel_text.Append("<td height='68'><strong><p align='center'>" + "服装要求" + "</p></strong></td>");//服装要求
excel_text.Append("<td colspan='7'><p align='center'>" + userrmk + "</p></td>");
excel_text.Append("</tr>"); excel_text.Append("<tr>");
excel_text.Append("<td height='28'><strong><p align='center'>" + "收件人地址" + "</p></strong></td>");//收件人地址
excel_text.Append("<td colspan='7'><p align='center'>" + address + "</p></td>");
excel_text.Append("</tr>"); string[] array = imgs.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
for (int i = ; i < array.Length; i++)
{
excel_text.Append("<tr>");
excel_text.Append("<td height='310' colspan='8' align='center'><img src='" + array[i] + "' height='310'/></td>");
excel_text.Append("</tr>");
} excel_text.Append("</tbody>");
excel_text.Append("</table>"); 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?参数=参数值"的方法导出表格。