现在做一个数据导入与导出Excel的项目,在网上看了一下,有两种方法可以导入:
方法一:
T-SQL,用insert into语句一行一行的把数据写入Excel(本人觉得这个的效率不是很好)
方法二:
文件流,把数据先放也一个WEB数据控件里面,然后用文件流的形势生成Excel文件(好像这样比较快,不过打开Excel的时候有一个警告)
想请教一下这两种方法哪种好点,顺便分析一下为什么,或者有没有更好的方法..

解决方案 »

  1.   


    我经常用的一种: protected void btntoDoc_Click(object sender, EventArgs e)
            {
                ExportControl(rptCollection, "excel", "文件名");
            }
         
            public void ExportControl(System.Web.UI.WebControls.Repeater source, string DocumentType, string filename)
            {
                RepeaterItem item = new RepeaterItem(0, ListItemType.Item);
                
                //设置Http的头信息,编码格式   
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.ClearHeaders();
                if (DocumentType.ToLower() == "excel")
                {
                    //Excel               
                    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".xls", System.Text.Encoding.UTF8));
                    HttpContext.Current.Response.ContentType = "application/ms-excel";
                }
                else if (DocumentType.ToLower() == "word")
                {
                    //Word   
                    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".doc", System.Text.Encoding.UTF8));
                    HttpContext.Current.Response.ContentType = "application/ms-word";
                }
                HttpContext.Current.Response.Charset = "UTF-8";
                HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.UTF8;
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
                //关闭控件的视图状态   
                source.Page.EnableViewState = false;
                //初始化HtmlWriter   
                System.IO.StringWriter writer = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
                source.RenderControl(htmlWriter);
                //输出   
                string pageHtml = writer.ToString();
                                           HttpContext.Current.Response.Charset = "utf-8";
                HttpContext.Current.Response.ContentEncoding = Encoding.Default;
                HttpContext.Current.Response.Write(pageHtml);
      
                HttpContext.Current.Response.End();
            }
      

  2.   

    2要快些。。如果数据少。的话还可以考虑1 弹出这个没啥吧?我这有一个。。 DataTable Excel_UserInfo = new DataTable();
    string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileInfo.FullName + ";" + "Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1;\"";
                    string strExcel = "select * from [sheet1$]";                using (OleDbDataAdapter adaptor = new OleDbDataAdapter(strExcel, strConn))
                    {
                        DataSet ds = new DataSet();
                        adaptor.Fill(ds);
                        Excel_UserInfo = ds.Tables[0];
                    }
      

  3.   


    这个在Office中打开就不会,郁闷,07的性能倒退了???
      

  4.   


    这个在Office03中打开就不会,郁闷,07的性能倒退了???
      

  5.   

    Excel的数据源你能指望有什么效率呢?
    要么分宏多线程
      

  6.   

    弹出这个应该是office低版本与高版本兼容的问题。
      

  7.   

    采用xml格式的就不会提示这些了
    代码
    http://dotnet.aspx.cc/file/Export-Gridview-To-Excel-With-Multi-Sheet.aspx如果使用Excel.APplication插入数据,可以使用二维数组,一次赋值给Range对象即可
      

  8.   


    一直都用的datatable导出excel方式