zoukankan      html  css  js  c++  java
  • MVC导出Excle

     Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application

    ();
                    app.Workbooks.Add();
           

                    Microsoft.Office.Interop.Excel.Worksheet sheet = app.ActiveSheet;
                    sheet.Cells[5, "B"] = "会员卡号";
                    sheet.Cells[5, "C"] = "姓名";
                    sheet.Cells[5, "D"] = "性别";
                    sheet.Cells[5, "E"] = "电话";
                    sheet.Cells[5, "F"] = "会员等级";
                    sheet.Cells[5, "G"] = "登记时间";
                    //sheet.get_Range(sheet.Cells[5, "E"]).set_ColumnWidth(60);
                    //sheet.get_Range(sheet.Cells[5, "G"]).set_ColumnWidth(60);
                    int rowIndex = 5;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        rowIndex++;
                        sheet.Cells[rowIndex, "B"] = dt.Rows[i]["MC_CardID"].ToString();
                        sheet.Cells[rowIndex, "C"] = dt.Rows[i]["MC_Name"].ToString();
                        var Sex = "男";
                        if (int.Parse(dt.Rows[i]["MC_Sex"].ToString()) == 2)
                        {
                            Sex = "女";
                        }
                        sheet.Cells[rowIndex, "D"] = Sex;
                        sheet.Cells[rowIndex, "E"] = dt.Rows[i]["MC_Mobile"].ToString();
                        sheet.Cells[rowIndex, "F"] = dt.Rows[i]["CL_LevelName"].ToString();
                        sheet.Cells[rowIndex, "G"] = dt.Rows[i]["MC_CreateTime"].ToString();
                    }
                    Response.Clear();
                    Response.Buffer = true;
                    Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString

    ("yyyyMMddhhmmss") + ".xlsx");
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    Response.ContentType = "application/vnd.ms-excel";

                    return Json(true, JsonRequestBehavior.AllowGet);
                }
                catch (Exception e)
                {
                    return RedirectToAction("Index", "Error", e);
                }

            }

  • 相关阅读:
    C# 中的委托和事件
    POJ题目分类
    A*算法
    Poj 1077 eight(BFS+全序列Hash解八数码问题)
    Poj 2304 Combination Lock(模拟顺、逆时钟开组合锁)
    Poj 2247 Humble Numbers(求只能被2,3,5, 7 整除的数)
    Poj 2328 Guessing Game(猜数字游戏)
    Poj 2403 Hay Points(Map)
    Poj 1338 Ugly Numbers(数学推导)
    Poj 1504 Adding Reversed Numbers(用字符串反转数字)
  • 原文地址:https://www.cnblogs.com/yf1019091807/p/5096393.html
Copyright © 2011-2022 走看看