接昨日,将上传至oracle的excel文件下载下来

本文详细介绍了一段C#代码,该代码用于从数据库中读取特定ID的Excel文件,并将其作为二进制数据流输出到HTTP响应中,实现在线下载。文章涵盖了数据库连接、SQL查询、读取BLOB类型数据并进行流处理的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码如下
try
        {
            DBHelper db = DBHelper.DBHelperInstance(System.Configuration.ConfigurationSettings.AppSettings["DataBaseType"]); ;
            OracleConnection oconn = new OracleConnection(db.pcsConnectionString);
            oconn.Open();

            string strSQL = "select DOC from exceltodb where fileid='1112'";
            OracleCommand OCMD = new OracleCommand(strSQL, oconn);
            OracleDataReader Ord = OCMD.ExecuteReader(CommandBehavior.SequentialAccess);
            int bufferSize = 100;
            byte[] outbyte = new byte[bufferSize];
            long retval;                            // The bytes returned from GetBytes.
            long startIndex = 0;
            Response.AddHeader("Content-Type", "application/vnd.ms-excel");
            Response.AddHeader("Content-Disposition", "attachment;filename=test.xls");
            if(Ord.Read())
            {
                // Reset the starting byte for the new BLOB.
                startIndex = 0;

                // Read the bytes into outbyte[] and retain the number of bytes returned.
                retval = Ord.GetBytes(0, startIndex, outbyte, 0, bufferSize);

                // Continue reading and writing while there are bytes beyond the size of the buffer.
                while (retval == bufferSize)
                {
                    this.Response.BinaryWrite(outbyte);
                    this.Response.Flush();

                    // Reposition the start index to the end of the last buffer and fill the buffer.
                    startIndex += bufferSize;
                    retval = Ord.GetBytes(0, startIndex, outbyte, 0, bufferSize);
                }

                // Write the remaining buffer.
                this.Response.BinaryWrite(outbyte);
                this.Response.Flush();
             }
             Ord.Close();
             oconn.Close();
                this.Response.End();
           }
           catch(Exception ex)
           {
             this.Response.Write("<b>ex.Message</b>");
           }
           finally
           {
            
            }

参考自优快云相关文档

转载于:https://www.cnblogs.com/yeskele/archive/2006/03/14/349628.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值