导出excel 和word  
 protected void outport_Click(object sender, EventArgs e)
    {
        Export("application/ms-excel", DropDownList1.SelectedValue );
    }
    private void Export(string FileType, string FileName)
    {
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        Response.ContentType = FileType;
        this.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        GV_cg.RenderControl(hw);
        //DataList1.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
    }

解决方案 »

  1.   


    <HTML>
      <HEAD>
        <title>WEB页面导出为EXCEL文档的方法
        </title>
      </HEAD>
    <body>
    <BR>
    <table id = "PrintA" width="100%" border="1" cellspacing="0" cellpadding="0" bgcolor = "#61FF13">
    <TR style="text-align : center;">
     <TD>单元格A</TD>
     <TD>单元格A</TD>
     <TD>单元格A</TD>
     <TD>单元格A</TD>
    </TR>
    <TR>
     <TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">单元格合并行A</FONT></TD>
    </TR>
    </TABLE>
    <BR>
    <table id = "PrintB" width="100%" border="1" cellspacing="0" cellpadding="0">
    <TR style="text-align : center;">
     <TD>单元格B</TD>
     <TD>单元格B</TD>
     <TD>单元格B</TD>
     <TD>单元格B</TD>
    </TR>
    <TR>
     <TD colSpan=4 style="text-align : center;">单元格合并行B</TD>
    </TR>
    </TABLE>
    <br><br><br>
    <input type="button" onclick="javascript:AllAreaWord();" value="导出页面指定区域内容到Word">
    <input type="button" onclick="javascript:AllAreaExcel();" value="导出页面指定区域内容到Excel">
    <input type="button" onclick="javascript:CellAreaExcel();" value="导出表单单元格内容到Excel">
    <SCRIPT LANGUAGE="javascript">
     //指定页面区域内容导入Excel
     function AllAreaExcel() 
     {
      var oXL = new ActiveXObject("Excel.Application"); 
      var oWB = oXL.Workbooks.Add(); 
      var oSheet = oWB.ActiveSheet;  
      var sel=document.body.createTextRange();
      sel.moveToElementText(PrintA);
      sel.select();
      sel.execCommand("Copy");
      oSheet.Paste();
      oXL.Visible = true;
     }
     //指定页面区域“单元格”内容导入Excel
     function CellAreaExcel() 
     {
      var oXL = new ActiveXObject("Excel.Application"); 
      var oWB = oXL.Workbooks.Add(); 
      var oSheet = oWB.ActiveSheet; 
      var Lenr = PrintA.rows.length;
      for (i=0;i<Lenr;i++) 
      { 
       var Lenc = PrintA.rows(i).cells.length; 
       for (j=0;j<Lenc;j++) 
       { 
        oSheet.Cells(i+1,j+1).value = PrintA.rows(i).cells(j).innerText; 
       } 
      } 
      oXL.Visible = true; 
     } //指定页面区域内容导入Word
     function AllAreaWord()
     {
      var oWD = new ActiveXObject("Word.Application");
      var oDC = oWD.Documents.Add("",0,1);
      var oRange =oDC.Range(0,1);
      var sel = document.body.createTextRange();
      sel.moveToElementText(PrintA);
      sel.select();
      sel.execCommand("Copy");
      oRange.Paste();
      oWD.Application.Visible = true;
      //window.close();
     }
    </SCRIPT>
    </body></html>你也可以考虑一下直接用js来操作
      

  2.   

    后台生成完后让用户下载即可.
    生成的代码网上找一下.可以添加word的com对象模型来做.
      

  3.   

    出了这个错怎么解决啊?
    ------------------------------------类型“Button”的控件“DataList1__ctl0_Button2”必须放在具有 runat=server 的窗体标记内。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Web.HttpException: 类型“Button”的控件“DataList1__ctl0_Button2”必须放在具有 runat=server 的窗体标记内。源错误: 
    行 49:  HtmlTextWriter hw = new HtmlTextWriter(tw); 
    行 50:  //GV_cg.RenderControl(hw); 
    行 51:  DataList1.RenderControl(hw); 
    行 52:  Response.Write(tw.ToString()); 
    行 53:  Response.End(); 
     源文件: c:\inetpub\wwwroot\outlineinfo_fm\webform2.aspx.cs    行: 51 堆栈跟踪: 
    [HttpException (0x80004005): 类型“Button”的控件“DataList1__ctl0_Button2”必须放在具有 runat=server 的窗体标记内。]
       System.Web.UI.Page.VerifyRenderingInServerForm(Control control) +152
       System.Web.UI.WebControls.Button.AddAttributesToRender(HtmlTextWriter writer)
       System.Web.UI.WebControls.WebControl.RenderBeginTag(HtmlTextWriter writer) +17
       System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) +17
       System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
       System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +72
       System.Web.UI.Control.Render(HtmlTextWriter writer) +7
       System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer)
       System.Web.UI.WebControls.DataListItem.RenderItemInternal(HtmlTextWriter writer, Boolean extractRows, Boolean tableLayout)
       System.Web.UI.WebControls.DataListItem.RenderItem(HtmlTextWriter writer, Boolean extractRows, Boolean tableLayout)
       System.Web.UI.WebControls.DataList.System.Web.UI.WebControls.IRepeatInfoUser.RenderItem(ListItemType itemType, Int32 repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
       System.Web.UI.WebControls.RepeatInfo.RenderVerticalRepeater(HtmlTextWriter writer, IRepeatInfoUser user, Style controlStyle, WebControl baseControl)
       System.Web.UI.WebControls.RepeatInfo.RenderRepeater(HtmlTextWriter writer, IRepeatInfoUser user, Style controlStyle, WebControl baseControl)
       System.Web.UI.WebControls.DataList.RenderContents(HtmlTextWriter writer) +398
       System.Web.UI.WebControls.BaseDataList.Render(HtmlTextWriter writer) +27
       System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
       OutLineInfo_FM.WebForm2.file(String FileType, String FileName) in c:\inetpub\wwwroot\outlineinfo_fm\webform2.aspx.cs:51
       OutLineInfo_FM.WebForm2.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\outlineinfo_fm\webform2.aspx.cs:60
       System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       System.Web.UI.Page.ProcessRequestMain() +1292 
    --------------------------------------------------------------------------------
    版本信息: Microsoft .NET Framework 版本:1.1.4322.2407; ASP.NET 版本:1.1.4322.2407 
      

  4.   

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    public class AppWord
    {
        private Word.ApplicationClass oWordApplic;
        private Word.Document oDoc; public AppWord()
    {
            oWordApplic = new Word.ApplicationClass();
    }    public void Add(string strFileName)
        {
            object missing = System.Reflection.Missing.Value;        object tempName = strFileName;        oDoc = oWordApplic.Documents.Add(ref tempName, ref missing, ref missing, ref missing);        oWordApplic.Visible = true;        oDoc.Activate();
        }    public void SaveAs(string strFileName)
        {
            object missing = System.Reflection.Missing.Value;
            object fileName = strFileName;        oDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);        oDoc.Close(ref missing, ref missing, ref missing);
            
            oWordApplic.Quit(ref missing, ref missing, ref missing);        oDoc = null;        oWordApplic = null;
        }    public void GotoBookMark(string strBookMarkName)
        {
            object missing = System.Reflection.Missing.Value;
            object Book = (int)Word.WdGoToItem.wdGoToBook;
            object NameBookMark = strBookMarkName;
            oWordApplic.Selection.GoTo(ref Book, ref missing, ref missing, ref NameBookMark);
        }    public void InsertText(string strText)
        {
            oWordApplic.Selection.TypeText(strText);
        }    public void SetAlignment(string strType)
        {
            switch (strType)
            {
                case "Center":
                    oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                    break;
                case "Left":
                    oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
                    break;
                case "Right":
                    oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                    break;
                case "Justify":
                    oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify;
                    break;
            }    }    public void SetFont(string strType)
        {
            switch (strType)
            {
                case "Bold":
                    oWordApplic.Selection.Font.Bold = 1;
                    break;
                case "Italic":
                    oWordApplic.Selection.Font.Italic = 1;
                    break;
                case "Underlined":
                    oWordApplic.Selection.Font.Subscript = 0;
                    break;
            }    }    public void SetFontSize(int nSize)
        {
            oWordApplic.Selection.Font.Size = nSize;    }
        
    }
    试下   不过要修改word文档权限