asp.Netmvcエクスポートテーブル

4948 ワード

使用する場合:
.netでフロントのtableからexcelファイルにエクスポートし、各種ブラウザに互換性があります.
使用ツール:
org.in2bits.MyXls.dll
フロントからテーブルのtheadとテーブルのtbodyを取得し、文字列に変換し、カンマで区切る
html:
 
<div class="printContent">

        <table cellspacing="1" class="tablesorter table-04">

        <thead>

        <tr><th>     </th><th>     </th><th>     </th><th>    </th><th>    </th><th>    </th><th>   </th><th>    </th></tr>            

        </thead>

        <tfoot>

        <tr><th>     </th><th>     </th><th>     </th><th>    </th><th>    </th><th>    </th><th>   </th><th>    </th></tr>            

        </tfoot>

        <tbody>

        @foreach (var l in Model)

        {

            <tr id="@(l.XYHBH)"><td>@(l.XYHBH)</td><td>@(l.XYHMC)</td><td>@(l.BGSDZ)</td><td>@(l.XYHZX)</td><td>@(l.LXDH)</td><td>@(DAL.IsNull.ShortDateNull(l.CJSJ.ToString()))</td><td>@(l.HYZS)</td><td>@(BLL.ZYGLMK.XX_YXSJBQK.GetYXMC(l.YXSH))</td></tr> 

        }

        </tbody>

        </table></div>

 
javascript:
 
var heads = "";

        var bodys = "";

        var i = 0;

        //  title  ,    ,  

        $(".printContent table thead tr th").each(function () {

            heads = heads + $(this).text() + ",";

            i++;

        });

        i = 0;

        //  tbody  ,    ,  

        $(".printContent table tbody tr td").each(function () {

            bodys = bodys + $(this).text() + ",";

            i++;

        });

        //    ,     

        $.post("/Shared/ExportExcel", { head: heads, body: bodys }, function (data) {

            //      ,      

            if (data == "1") {

                //         ,    

                window.location.href = "/Shared/DownLoadExcel";

               

            }

        });

 
参照
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using org.in2bits.MyXls;


 
ExportExcel:
 
//  excel  

        #region   Excel

        public int  ExportExcel(string head, string body)

        {

            //         ,          

            string[] heads = head.Split(',');

            string[] bodys = body.Split(',');

            XlsDocument xls = new XlsDocument();

            //    

            xls.FileName = "excel.xls";

            //excel  

            xls.SummaryInformation.Author = Session["UID"].ToString().Trim();

            //  

            xls.SummaryInformation.Subject = "LIIP&A";

            //  

            xls.DocumentSummaryInformation.Company = "Intelligent Information Processing and Application Lab";

            int len = heads.Length - 1;

            //excel sheet  

            string sheetName = Session["UID"].ToString().Trim();

            int colCount = len;//   

            int rowCount = bodys.Length / len;//   

            Worksheet sheet = xls.Workbook.Worksheets.AddNamed(sheetName);

            Cells cells = sheet.Cells;

            int t = 0;

            //  excel    ,  0  len-1 

            for (int r = 0; r <= rowCount; r++)

            {

                //   (title)

                if (r == 0)

                {

                    for (int c = 0; c < colCount; c++)

                    {

                        //      colCount    , 1+r  1+c    heads[c]

                        cells.Add(1 + r, 1 + c, heads[c]).

                        Font.Bold = true;//    

                    }

                }

                else //    

                {

                    for (int c = 0; c < colCount; c++)

                    {

                        cells.Add(1+r,1+c,bodys[t]);

                        t++;

                    }

                }

            }

            //      

            xls.Save(Server.MapPath("/Content"),true);

            return 1;

        }



        //    

        public FileResult DownLoadExcel()

        {

            return File(Server.MapPath("/Content/excel.xls"), "application/ms-excel","excel.xls");

        }