如何在不使用第三方的情况下将所选数据导出到asp mvc2中的Excel工作表中

时间:2011-08-12 11:14:12

标签: asp.net asp.net-mvc-2

我想在excel表格中导出所选数据,我的视图代码在这里

  <% using (Html.BeginForm("ExcelExport", "Process", FormMethod.Post))
    { %>

    <div id="formdata" style="width:700px;overflow:scroll">
    <table  border="0">
<tr>
<th>select</th>

<th>
FirstName
</th>
<th>
Lastname
</th>
<th>
EmailID
</th>
<th>
ContactNO
</th>
<th>
Qualification
</th>
<th>
CurrentCTC
</th>
<th>
ExpectedCTC
</th>
<th>
Experience
</th>
<th>CurrentLocation</th>
<th> keyskill</th>
<th>Company</th>
</tr>
<% foreach (var candidate in Model)
   { %>
<div class="item">
<tr>

<td>
        <input type="checkbox" name="cid" value="<%= candidate.CandidateID %>" />
       <%:Html.ActionLink("W", "Getfile", new { id = candidate.CandidateID })%>
    </td>

<td>
<h3><%=candidate.FirstName%></h3>
</td>
<td>
<%=candidate.LastName%></td>
<td><%=candidate.EmailID%></td>
  <td><%=candidate.Phone1%></td>

  <td> <%=candidate.Qualification%></td>
<td><%=candidate.CurrentCTC%></td>

<td><%=candidate.ExpectedCTC%></td>
<td><%=candidate.Experience%></td>
<td><%=candidate.CurrentLocation %></td>
<td><%=candidate.KeySkill.KeySkills %></td>
<td><%=candidate.Company.CompanyName %></td>

</div>

<% }%>
</table>
  <%}%>
    </div>


<input id="checkbox" type="submit"  name= "submitButton" value="Process" />|
<input type= "submit"name= "submitButton" value="Sendmail" />|
<input type= "submit" name= "submitButton" value="Export" />|
<%} %> 

我希望选中行使用复选框并单击导出按钮然后我的数据导出到excel表plz任何一个给我示例代码紧急

1 个答案:

答案 0 :(得分:0)

朋友们,我得到了解决方案 这是我的代码,我将所选项目提取到excel

 public ActionResult ExportExcel(int[] cid )
    {

        var grid = new GridView();
        Response.ClearContent();

        Response.ContentType = "application/vnd.ms-excel";
        Response.AddHeader("content-disposition", "attachment;filename=candidateRecord.xls");
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        var candidate = IcandidateRepository.Candidate.ToList();

            grid.DataSource = from p in candidate where cid.Contains(p.CandidateID) select new { name = p.FirstName, companyName = p.Company.CompanyName };


            grid.DataBind();
            grid.RenderControl(hw);

           Response.Write(sw);
        Response.End();
        return View();
    }