91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ASP.NET中Execl導出的方法有哪些

發布時間:2021-10-08 15:05:41 來源:億速云 閱讀:130 作者:iii 欄目:開發技術

本篇內容主要講解“ASP.NET中Execl導出的方法有哪些”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“ASP.NET中Execl導出的方法有哪些”吧!

復制代碼 代碼如下:

        /// <summary>
        /// 導出Excel
        /// </summary>
        /// <param name="page"></param>
        /// <param name="dt"></param>
        //方法一:
        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<ds.Tables.Count;k++)

                {

                    DataTable dt = ds.Tables[k];

                    sSql = "CREATE TABLE sheet" + (k + 1).ToString() + "(";

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

                    {

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

                        {

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

                            {

                                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 sheet" + (k + 1).ToString() + " 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().Replace("'", "''") + "',";

                                    }

                                }

                            }

                            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().Replace("'","''") + "')";

                                    }

                                }

                        }

                        oleDbCmd.CommandText = sSql;

                        oleDbCmd.ExecuteNonQuery();

                    }

                }

            }

            catch (System.Exception ex)

            {

                throw ex;

            }

            finally

            {

                //斷開連接

                oleDbCmd.Dispose();

                oleDbConn.Close();

                oleDbConn.Dispose();

            }

        }


        方法六:

復制代碼 代碼如下:

      
        private void CreateExcelFile(string filePath, DataTable dt1,DataTable dt2,string conditions)

        {

            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(";

                DataTable dt = dt1.Copy();

                dt.Columns.Remove("MGUID");

                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();

                DataView dv = new DataView();

                dv.Table = dt;

                DataView dv1 = new DataView();

                dv1.Table = dt1;

                if (conditions != "")

                {

                    dv.RowFilter = conditions;

                    dv1.RowFilter = conditions;

                }

                dt = dv.ToTable();

                dt1 = dv1.ToTable();

                string MGUIDs = "";

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

                {

                    MGUIDs += ",'" + dt1.Rows[j]["MGUID"].ToString() + "'";

                    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();

                }

                if (dt2 != null)

                {

                    sSql = "CREATE TABLE sheet21(";

                    dt = dt2.Copy();

                    dt.Columns.Remove("MGUID");

                    dt.Columns.Remove("DGUID");

                    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();

                    dv = new DataView();

                    dv.Table = dt2;                   

                    if (MGUIDs != "")

                    {

                        dv.RowFilter = "MGUID in(" + MGUIDs.Substring(1) + ")";

                    }

                    dt = dv.ToTable();

                    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();

            }

        }

到此,相信大家對“ASP.NET中Execl導出的方法有哪些”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

武功县| 凤凰县| 夏津县| 巴塘县| 腾冲县| 郎溪县| 乌审旗| 山阳县| 通山县| 河间市| 大姚县| 土默特左旗| 临泽县| 平顺县| 克山县| 延津县| 曲靖市| 西吉县| 股票| 司法| 襄垣县| 突泉县| 台东县| 绥江县| 乡城县| 蒙山县| 鱼台县| 洱源县| 平舆县| 大埔区| 大邑县| 卫辉市| 门源| 府谷县| 奉节县| 抚州市| 灵寿县| 庐江县| 石林| 凤城市| 克东县|