主頁 > 知識庫 > ASP.NET中 Execl導(dǎo)出的六種方法實(shí)例

ASP.NET中 Execl導(dǎo)出的六種方法實(shí)例

熱門標(biāo)簽:400電話辦理信任翰諾科技 電銷機(jī)器人 數(shù)據(jù) 宿遷智能外呼系統(tǒng)排名 地圖標(biāo)注多少錢一張 廣州銷售外呼系統(tǒng)定制 福州人工智能電銷機(jī)器人加盟 怎樣給陜西地圖標(biāo)注顏色 ai電銷機(jī)器人對貸款有幫助嗎 云狐人工智能電話機(jī)器人

復(fù)制代碼 代碼如下:

        /// summary>
        /// 導(dǎo)出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數(shù)據(jù)到內(nèi)存
                    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(); //停止頁的執(zhí)行

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

                    //刪除臨時文件
                    File.Delete(webFilePath);
                }

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


     方法二:
復(fù)制代碼 代碼如下:

        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數(shù)據(jù)到內(nèi)存

                    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(); //停止頁的執(zhí)行

 

 

                    fs.Close();

                    fs.Dispose();

 

                    //刪除臨時文件

                    File.Delete(webFilePath);

                }

 

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

        方法三:
復(fù)制代碼 代碼如下:
     
        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數(shù)據(jù)到內(nèi)存

                    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(); //停止頁的執(zhí)行

 

 

                    fs.Close();

                    fs.Dispose();

 

                    //刪除臨時文件

                    File.Delete(webFilePath);

                }

 

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

        方法四:
復(fù)制代碼 代碼如下:

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

            }

        }

        方法五:
復(fù)制代碼 代碼如下:
      
        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;kds.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();

            }

        }

        方法六:
復(fù)制代碼 代碼如下:
      
        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();

            }

        }

您可能感興趣的文章:
  • C#數(shù)據(jù)導(dǎo)入/導(dǎo)出Excel文件及winForm導(dǎo)出Execl總結(jié)

標(biāo)簽:延安 焦作 大興安嶺 綿陽 曲靖 新疆 宜春 黃南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ASP.NET中 Execl導(dǎo)出的六種方法實(shí)例》,本文關(guān)鍵詞  ASP.NET,中,Execl,導(dǎo)出,的,六種,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《ASP.NET中 Execl導(dǎo)出的六種方法實(shí)例》相關(guān)的同類信息!
  • 本頁收集關(guān)于ASP.NET中 Execl導(dǎo)出的六種方法實(shí)例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章