求 asp.net  mvc2.0框架 导出EXCEL 例子?

解决方案 »

  1.   

    跟.NET导出是一样 唯一不一样就是把方法写在ACTION里了。。
      

  2.   

    http://www.codeproject.com/KB/aspnet/Streaming_Excel_ASP_NET.aspx
      

  3.   

    估计和WEBFORM一样也都能用吧.
      

  4.   

    void DataSetToSheet(string strFilePath,string strFileType)
    {
    Excel.Application m_Excel = null;
    Excel._Workbook m_Book = null;
    Excel._Worksheet wsOne = null;
    Excel.Range range = null; string strOneTitle = "单期数据";
    m_Excel = new Excel.Application();//创建一个Excel对象(同时启动EXCEL.EXE进程)
    m_Excel.SheetsInNewWorkbook = 1;//工作表的个数 

    m_Book = (Excel._Workbook)(m_Excel.Workbooks.Add(Missing.Value));//添加新工作簿  //添加“股东数据结构分析-单期数据”
    wsOne = (Excel._Worksheet)(m_Book.Worksheets["Sheet1"]);
    wsOne.Name = "StructOne";
    try
    {

    if(ViewState["OneDate"].ToString() != "")
    {
    #region 添加“单期数据”到Excel的指定Sheet中

    System.Data.DataTable dtOne = (System.Data.DataTable)ViewState["dtInfo"];
    //以下是填写EXCEL中数据
    range = wsOne.get_Range(wsOne.Cells[1,1], wsOne.Cells[1,4]);
    range.MergeCells = true;  //合并单元格
    range.Font.Bold = true;   //加粗单元格内字符
    wsOne.Cells[1,1] = strOneTitle;
    wsOne.Cells[2,1] = "类别";
    wsOne.Cells[2,2] = "人数";
    wsOne.Cells[2,3] = "数量";
    wsOne.Cells[2,4] = "比例(%)"; for(int i=0; i<dtOne.Rows.Count; i++)
    {
    wsOne.Cells[i+3,1] = dtOne.Rows[i]["Type"];
    wsOne.Cells[i+3,2] = dtOne.Rows[i]["PerNum"];
    wsOne.Cells[i+3,3] = dtOne.Rows[i]["HaveNum"];
    wsOne.Cells[i+3,4] = dtOne.Rows[i]["HaveRate"];
    }
    wsOne.Columns.AutoFit(); 
    #endregion
    }

    m_Book.SaveAs(strFilePath + strFileType,Excel.XlFileFormat.xlWebArchive, Missing.Value,Missing.Value,Missing.Value,Missing.Value,Excel.XlSaveAsAccessMode.xlNoChange,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);

    m_Book.Close(false, Missing.Value, Missing.Value);
    m_Excel.Workbooks.Close();
    m_Book = null;
    m_Excel.Quit();
    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_Excel);
    int generation=System.GC.GetGeneration(m_Excel);
    m_Excel = null;
    System.GC.Collect(generation);
    GC.Collect();
    //Kill();
    string fileSavePath = strFilePath + ".xls";
    string filename = "InfoFrame.xls";
    string strContentType = "application/ms-excel";

    FileStream fs = new FileStream(fileSavePath ,FileMode.Open);
    byte[] bytes = new byte[(int)fs.Length];
    fs.Read(bytes,0,bytes.Length);
    fs.Close();
    Response.ContentType = strContentType;
    Response.AddHeader("Content-Disposition", "attachment; filename = " + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
    Response.BinaryWrite(bytes);
    }
    catch(Exception)
    {

    }
    finally
    {
    GC.Collect();
    }
    }
      

  5.   

    跟楼主的问题一样。
    已经解决了
    可以看一下我的解决方案,希望可以帮到LZ
    http://topic.csdn.net/u/20110831/15/fcd43dd2-44a4-460d-a737-5d11fe53b587.html