当前位置:  编程技术>.net/c#/asp.net

ASP.NET中 Execl导出的六种方法实例

    来源: 互联网  发布时间:2014-08-25

    本文导语:  代码如下:        ///         /// 导出Excel        ///         ///         ///         //方法一:        public void ImportExcel(Page page, DataTable dt)        {            try            {          ...

代码如下:

        ///
        /// 导出Excel
        ///
        ///
        ///
        //方法一:
        public void ImportExcel(Page page, DataTable dt)
        {
            try
            {

                string filename = Guid.NewGuid().ToString() + ".xls";
                string webFilePath = page.Server.MapPath("/" + filename);
                CreateExcelFile(webFilePath, dt);
                using (FileStream fs = new FileStream(webFilePath, FileMode.OpenOrCreate))
                {
                    //让用户输入下载的本地地址
                    page.Response.Clear();
                    page.Response.Buffer = true;
                    page.Response.Charset = "GB2312";

                    //page.Response.AppendHeader("Content-Disposition", "attachment;filename=MonitorResult.xls");
                    page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
                    page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                    page.Response.ContentType = "application/ms-excel";

                    // 读取excel数据到内存
                    byte[] buffer = new byte[fs.Length - 1];
                    fs.Read(buffer, 0, (int)fs.Length - 1);

                    // 写到aspx页面
                    page.Response.BinaryWrite(buffer);
                    page.Response.Flush();
                    //this.ApplicationInstance.CompleteRequest(); //停止页的执行

 
                    fs.Close();
                    fs.Dispose();

                    //删除临时文件
                    File.Delete(webFilePath);
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


     方法二:
代码如下:

        public void ImportExcel(Page page, DataSet ds)
        {

            try

            {

                string filename = Guid.NewGuid().ToString() + ".xls";

                string webFilePath = page.Server.MapPath("/" + filename);

                CreateExcelFile(webFilePath, ds);

                using (FileStream fs = new FileStream(webFilePath, FileMode.OpenOrCreate))

                {

                    //让用户输入下载的本地地址

                    page.Response.Clear();

                    page.Response.Buffer = true;

                    page.Response.Charset = "GB2312";

 

                    //page.Response.AppendHeader("Content-Disposition", "attachment;filename=MonitorResult.xls");

                    page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);

                    page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

                    page.Response.ContentType = "application/ms-excel";

 

                    // 读取excel数据到内存

                    byte[] buffer = new byte[fs.Length - 1];

                    fs.Read(buffer, 0, (int)fs.Length - 1);

 

                    // 写到aspx页面

                    page.Response.BinaryWrite(buffer);

                    page.Response.Flush();

                    //this.ApplicationInstance.CompleteRequest(); //停止页的执行

 

 

                    fs.Close();

                    fs.Dispose();

 

                    //删除临时文件

                    File.Delete(webFilePath);

                }

 

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

        方法三:
代码如下:
     
        public void ImportExcel(Page page, DataTable dt1, DataTable dt2, string conditions)

        {

            try

            {

 

                string filename = Guid.NewGuid().ToString() + ".xls";

                string webFilePath = page.Server.MapPath("/" + filename);

                CreateExcelFile(webFilePath, dt1, dt2, conditions);

                using (FileStream fs = new FileStream(webFilePath, FileMode.OpenOrCreate))

                {

                    //让用户输入下载的本地地址

                    page.Response.Clear();

                    page.Response.Buffer = true;

                    page.Response.Charset = "GB2312";

 

                    //page.Response.AppendHeader("Content-Disposition", "attachment;filename=MonitorResult.xls");

                    page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);

                    page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

                    page.Response.ContentType = "application/ms-excel";

 

                    // 读取excel数据到内存

                    byte[] buffer = new byte[fs.Length - 1];

                    fs.Read(buffer, 0, (int)fs.Length - 1);

 

                    // 写到aspx页面

                    page.Response.BinaryWrite(buffer);

                    page.Response.Flush();

                    //this.ApplicationInstance.CompleteRequest(); //停止页的执行

 

 

                    fs.Close();

                    fs.Dispose();

 

                    //删除临时文件

                    File.Delete(webFilePath);

                }

 

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

        方法四:
代码如下:

        private void CreateExcelFile(string filePath, DataTable dt)

        {

            if (File.Exists(filePath))

            {

                File.Delete(filePath);

            }

            OleDbConnection oleDbConn = new OleDbConnection();

            OleDbCommand oleDbCmd = new OleDbCommand();

 

            try

            {

                string sSql = "";

                oleDbConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + @";Extended ProPerties=""Excel 8.0;HDR=Yes;""";

                oleDbConn.Open();

                oleDbCmd.CommandType = CommandType.Text;

                oleDbCmd.Connection = oleDbConn;

                //写列名

                sSql = "CREATE TABLE sheet1(";

                for (int i = 0; i < dt.Columns.Count; i++)

                {

                    if (i < dt.Columns.Count - 1)

                    {

                        if (dt.Columns[i].DataType.Name == "String")

                        {

                            sSql += "[" + dt.Columns[i].ColumnName + "] Text,";

                        }

                        else if (dt.Columns[i].DataType.Name == "DateTime")

                        {

                            sSql += "[" + dt.Columns[i].ColumnName + "] Datetime,";

                        }

                        else

                        {

                            sSql += "[" + dt.Columns[i].ColumnName + "] Decimal,";

                        }

                    }

                    else

                    {

                        if (dt.Columns[i].DataType.Name == "String")

                        {

                            sSql += "[" + dt.Columns[i].ColumnName + "] Text)";

                        }

                        else if (dt.Columns[i].DataType.Name == "DateTime")

                        {

                            sSql += "[" + dt.Columns[i].ColumnName + "] DateTime)";

                        }

                        else

                        {

                            sSql += "[" + dt.Columns[i].ColumnName + "] Decimal)";

                        }

                    }

                }

                oleDbCmd.CommandText = sSql;

                oleDbCmd.ExecuteNonQuery();

 

                for (int j = 0; j < dt.Rows.Count; j++)

                {

                    sSql = "INSERT INTO sheet1 VALUES(";

                    for (int i = 0; i < dt.Columns.Count; i++)

                    {

                        if (i < dt.Columns.Count - 1)

                        {

                            if (DBNull.Value.Equals(dt.Rows[j][i]))

                            {

                                sSql += "NULL,";

                            }

                            else

                            {

                                if (dt.Columns[i].DataType.Name == "Decimal")

                                {

                                    sSql += dt.Rows[j][i].ToString() + ",";

                                }

                                else

                                {

                                    sSql += "'" + dt.Rows[j][i].ToString() + "',";

                                }

                            }

                        }

                        else

                            if (DBNull.Value.Equals(dt.Rows[j][i]))

                            {

                                sSql += "NULL)";

                            }

                            else

                            {

                                if (dt.Columns[i].DataType.Name == "Decimal")

                                {

                                    sSql += dt.Rows[j][i].ToString() + ")";

                                }

                                else

                                {

                                    sSql += "'" + dt.Rows[j][i].ToString() + "')";

                                }

                            }

                    }

                    oleDbCmd.CommandText = sSql;

                    oleDbCmd.ExecuteNonQuery();

                }

            }

            catch (System.Exception ex)

            {

                throw ex;

            }

            finally

            {

                //断开连接

                oleDbCmd.Dispose();

                oleDbConn.Close();

                oleDbConn.Dispose();

            }

        }

        方法五:
代码如下:
      
        private void CreateExcelFile(string filePath, DataSet ds)

        {

            if (File.Exists(filePath))

            {

                File.Delete(filePath);

            }

            OleDbConnection oleDbConn = new OleDbConnection();

            OleDbCommand oleDbCmd = new OleDbCommand();

 

            try

            {

                string sSql = "";

                oleDbConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + @";Extended ProPerties=""Excel 8.0;HDR=Yes;""";

                oleDbConn.Open();

                oleDbCmd.CommandType = CommandType.Text;

                oleDbCmd.Connection = oleDbConn;

                //写列名

                for(int k=0;k

    
 
 

您可能感兴趣的文章:

  • Asp.net中DataTable导出到Excel的实现代码
  • asp.net导出Excel乱码的原因及解决方法
  • asp.net使用npoi读取excel模板并导出下载详解
  • asp.net导出excel的简单方法实例
  • asp.net导出Excel类库代码分享
  • asp.net实例 定义和使用asp:AccessDataSource
  • asp.net输出重写压缩页面文件的实例
  • asp match正则函数使用Matchs实例
  • c#(asp.net)连接excel的实例代码
  • asp.net实例代码 在DataGrid控件中显示数据
  • asp.net 伪静态简单实例
  • asp.net取得所有颜色值实例
  • asp.net实例代码之DataGrid数据编辑
  • asp.net 动态添加多个用户控件(实例代码)
  • asp.net 邮件发送类的简单实例
  • asp.net 动态创建控件的演示实例
  • asp.net操作cookie实例代码
  • asp.net实例代码之添加DataColumn到DataTable控件中
  • asp.net批量删除实例代码教程
  • asp.net 操作cookie实例详解
  • asp.net实例代码之显示数据在不同的控件
  • ASP.net WebAPI 上传图片实例
  • asp.net读取与删除磁盘文件的实例代码
  • asp.net实例代码之datagrid页面索引
  • asp.net实例代码之更新访问数据
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • asp.net UrlEncode对应asp urlencode的处理方法
  • asp.net post方法中参数取不出来的解决方法
  • Linux平台下哪种方法实现ASP好?
  • asp.net 禁用viewstate(web.config中配置)的方法
  • c#(asp.net)接收存储过程返回值的方法
  • asp.net 超时设置的方法介绍
  • ASP.NET使用Subtract方法获取两个日期之间的天数
  • asp.net获取url地址的方法
  • iis8.5显示ASP的详细错误信息500 内部服务器错误解决方法
  • asp.net Control控件常用的属性与方法
  • ASP.NET 回发密码框清空问题处理方法
  • asp.net 参数不同共用一个页面的实现方法
  • Asp.net防止重复提交的实现方法
  • 重新注册asp.net 2.0的方法
  • asp.net防止后退与重复提交表单的简单方法
  • asp.net Session丢失的解决方法
  • asp.net ajax时用alert弹出对话框与验证控件冲突的解决方法
  • IIS7配置ASP详细错误信息发送到浏览器显示的方法
  • iis支持asp.net4.0的注册命令使用方法
  • asp.net动态添加非标准html控件的方法
  • ASP.NET之 Ajax相关知识介绍及组件图
  • 我想了解一些关于Java怎样与Asp或Asp.net结合方面在未来发展方向的问题?
  • c#/ASP.NET操作cookie(读写)代码示例
  • win2008 r2 服务器环境配置(FTP/ASP/ASP.Net/PHP)
  • asp与asp.net的session共享
  • 如何在unix下发布asp?
  • 怎么让Apache支持Asp?
  • ??谁能把ASP代码改为JSP的
  • ASP和ASP.Net共享Session解决办法
  • 通过socket和asp打交道
  • 犹豫中……,到底是选择ASP,还是JSP?




  • 特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3