MVC从div

时间:2017-06-21 11:51:37

标签: jquery html asp.net-mvc excel

我正在尝试实现Export to excel功能。我将整个表格以html格式作为字符串传递给导出函数并尝试将其转换为excel。以下是我实施的代码

        [HttpPost]
    public void ExportToExcel(ExportDataEntity data)
    {
        StringWriter sw = new StringWriter();
        sw.WriteLine(data.HtmlData);
        System.Web.HttpContext.Current.Response.ClearContent();
        System.Web.HttpContext.Current.Response.Buffer = true;
        System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=test.xls");
        System.Web.HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        System.Web.HttpContext.Current.Response.Charset = "";
                byte[] byteArray = Encoding.ASCII.GetBytes(sw.ToString());
        MemoryStream s = new MemoryStream(byteArray);
        StreamReader sr = new StreamReader(s, Encoding.Unicode);
        System.Web.HttpContext.Current.Response.Write(sr.ReadToEnd());
        System.Web.HttpContext.Current.Response.End();

    }

以下是我的实体类

    public class ExportDataEntity
{
    public string HtmlData { get; set; }
    public string FileName { get; set; }
    public string FileFormat { get; set; }
}

以下是表格结构

<table border="1">
            <tbody>
                <tr class="text-center">
                    <th class="text-center" rowspan="2">Col1</th>
                    <th class="text-center" colspan="3">Col2</th>
                    <th class="text-center" colspan="3">Col3</th>
                    <th class="text-center" rowspan="2">Col4</th>
                    <th class="text-center" colspan="4">Col5</th>
                    <th class="text-center" colspan="6">Col6</th>
                </tr>
                <tr>
                    <td>SubCol21</td>
                    <td>SubCol22</td>
                    <td>SubCol23</td>
                    <td>SubCol31</td>
                    <td>SubCol32</td>
                    <td>SubCol33</td>
                    <td>SubCol51</td>
                    <td>SubCol52</td>
                    <td>SubCol53</td>
                    <td>SubCol54</td>
                    <td>SubCol61</td>
                    <td>SubCol62</td>
                    <td>SubCol63</td>
                    <td>SubCol64</td>
                    <td>SubCol65</td>
                    <td>SubCol66</td>
                </tr>
                    <tr class="trData">
                        <td>1</td>
                        <td>2</td>
                        <td>3</td>
                        <td>4</td>
                        <td>5</td>
                        <td>6</td>
                        <td>7</td>
                        <td>8</td>
                        <td>9</td>
                        <td>10</td>
                        <td>11</td>
                        <td>12</td>
                        <td>13</td>
                        <td>14</td>
                        <td>15</td>
                        <td>16</td>
                        <td>17</td>
                        <td>18</td>
                    </tr>
                <tr>
                    <td>Count</td>
                    <td>1</td>
                    <td>2</td>
                    <td>3</td>
                    <td>4</td>
                    <td>5</td>
                    <td>6</td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td>7</td>
                    <td>8</td>
                    <td>9</td>
                    <td>10</td>
                    <td>11</td>
                    <td>12</td>
                </tr>
                <tr>
                    <td>Met %</td>
                    <td  colspan="3">1</td>
                    <td colspan="3">2</td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td colspan="3">3</td>
                    <td colspan="3">4</td>
                </tr>
            </tbody>
        </table>

我通过ajax调用调用ExportToExcel函数并在div中传递整个html(仅包含)。我的代码或浏览器窗口中没有出现任何错误。

我用Google搜索了很多,并试图实施各种解决方案,但没有任何帮助。

请告诉我我正在做的错误,并提供在MVC中实现它的最佳解决方案。

由于

1 个答案:

答案 0 :(得分:0)

嘿,如果你已经在Frontend中有数据,那么你可以使用Jquery Plugin table2Excel直接将你的表数据导出到excel文件。你不需要调用任何服务器端代码: 以下是相同的例子:

<html>
<head>
<style>
table, th, td {
    border: 1px solid black;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="jquery.table2excel.js"></script>
<script type="text/javascript">  
        function exportToExcel() {  
            $("#tableData").table2excel({  
                name: "Table2Excel",  
                filename: "myFileName",  
                fileext: ".xls"  
            });  
        }  
</script>

</head>
<body>


<table id="tableData" border="1">
            <tbody>
                <tr class="text-center">
                    <th class="text-center" rowspan="2">Col1</th>
                    <th class="text-center" colspan="3">Col2</th>
                    <th class="text-center" colspan="3">Col3</th>
                    <th class="text-center" rowspan="2">Col4</th>
                    <th class="text-center" colspan="4">Col5</th>
                    <th class="text-center" colspan="6">Col6</th>
                </tr>
                <tr>
                    <td>SubCol21</td>
                    <td>SubCol22</td>
                    <td>SubCol23</td>
                    <td>SubCol31</td>
                    <td>SubCol32</td>
                    <td>SubCol33</td>
                    <td>SubCol51</td>
                    <td>SubCol52</td>
                    <td>SubCol53</td>
                    <td>SubCol54</td>
                    <td>SubCol61</td>
                    <td>SubCol62</td>
                    <td>SubCol63</td>
                    <td>SubCol64</td>
                    <td>SubCol65</td>
                    <td>SubCol66</td>
                </tr>
                    <tr class="trData">
                        <td>1</td>
                        <td>2</td>
                        <td>3</td>
                        <td>4</td>
                        <td>5</td>
                        <td>6</td>
                        <td>7</td>
                        <td>8</td>
                        <td>9</td>
                        <td>10</td>
                        <td>11</td>
                        <td>12</td>
                        <td>13</td>
                        <td>14</td>
                        <td>15</td>
                        <td>16</td>
                        <td>17</td>
                        <td>18</td>
                    </tr>
                <tr>
                    <td>Count</td>
                    <td>1</td>
                    <td>2</td>
                    <td>3</td>
                    <td>4</td>
                    <td>5</td>
                    <td>6</td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td>7</td>
                    <td>8</td>
                    <td>9</td>
                    <td>10</td>
                    <td>11</td>
                    <td>12</td>
                </tr>
                <tr>
                    <td>Met %</td>
                    <td  colspan="3">1</td>
                    <td colspan="3">2</td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td colspan="3">3</td>
                    <td colspan="3">4</td>
                </tr>
            </tbody>
        </table>

<button onclick="exportToExcel()">  
            Export to Excel</button>  


</body>
</html>

以下是excel输出: enter image description here