将XLSM文件保存到数据库列并检索?

时间:2009-06-09 10:04:21

标签: c# .net sql-server c#-2.0 excel-2007

以前实现的代码接受xls文件使用流使用相同的方法将其保存到表中的列上但唯一的更改是保存的文件是保存到的xlsm或xlsx类型文件数据库中的列

当我尝试从数据库中获取内容并抛出保存的xlsm文件或xlsx文件时,我收到错误“Excel文件发现不可读的内容你想恢复这本工作簿的内容吗?”

这是保存xlsm或xlsx文件的代码

System.IO.Stream filestream = System.IO.File.Open(file, System.IO.FileMode.Open);
int fileLength = (int)filestream.Length;
byte[] input = new byte[fileLength];
filestream.Read(input, 0, fileLength);
string Sql = "insert into upload values(@contents)";
con.Open();
System.Data.SqlClient.SqlCommand c = new System.Data.SqlClient.SqlCommand(Sql, con);
c.Parameters.Add("@contents", System.Data.SqlDbType.Binary);
c.Parameters["@contents"].Value = input;
c.ExecuteNonQuery();

检索并发送给用户

SqlCommand comm = new SqlCommand("select contents from upload order by id desc", con);
SqlDataReader reader = comm.ExecuteReader();
int bufferSize = 32768;                   
        byte[] outbyte = new byte[bufferSize];  
        long retval;                           
        long startIndex = 0;                    
        startIndex = 0;
        retval = reader.GetBytes(0, startIndex, outbyte, 0, bufferSize);
        while (retval > 0)
        {
            System.Web.HttpContext.Current.Response.BinaryWrite(outbyte);
            startIndex += bufferSize;
            if (retval == bufferSize)
            {
                retval = reader.GetBytes(2, startIndex, outbyte, 0, bufferSize);
            }
            else
            {
                retval = 0;
            }
        }

2 个答案:

答案 0 :(得分:0)

有几件事让我觉得可能。

首先,您没有致电reader.Read()

其次,不需要检查retval == bufferSize - 只需再次调用GetBytes,如果没有从字段中读取字节,它将返回0。

第三,当您写入HttpResponse时,您需要确保在将字节写入输出之前调用Response.Clear(),并在将文件写入响应后调用Response.End()。

要尝试的另一件事是将文件保存到硬盘驱动器并将其与原始文件进行比较。它的大小是一样的吗?如果它更大,那么你正在向文件写入太多信息(参见前面关于HttpResponse的评论)。如果它更小,那么你写得不够,并且很可能过早地退出循环(参见关于retval的评论)。

答案 1 :(得分:0)

我忍不住注意到您的代码无法在using块中包装IDisposable的地方数量,如下所示:

using (SqlConnection con = new SqlConnection(connectionString))
{
    byte[] input;
    using (System.IO.Stream filestream = System.IO.File.Open(file, System.IO.FileMode.Open))
    {
        int fileLength = (int)filestream.Length;
        input = new byte[fileLength];
        filestream.Read(input, 0, fileLength);
    }
    const string Sql = "insert into upload values(@contents)";
    con.Open();
    using (System.Data.SqlClient.SqlCommand c = new System.Data.SqlClient.SqlCommand(Sql, con))
    {
        c.Parameters.Add("@contents", System.Data.SqlDbType.Binary);
        c.Parameters["@contents"].Value = input;
        c.ExecuteNonQuery();
    }

    using (SqlCommand comm = new SqlCommand("select contents from upload order by id desc", con))
    {
        using (SqlDataReader reader = comm.ExecuteReader())
        {
            int bufferSize = 32768;
            byte[] outbyte = new byte[bufferSize];
            long retval;
            long startIndex = 0;
            startIndex = 0;
            retval = reader.GetBytes(0, startIndex, outbyte, 0, bufferSize);
            while (retval > 0)
            {
                System.Web.HttpContext.Current.Response.BinaryWrite(outbyte);
                startIndex += bufferSize;
                if (retval == bufferSize)
                {
                    retval = reader.GetBytes(2, startIndex, outbyte, 0, bufferSize);
                }
                else
                {
                    retval = 0;
                }
            }
        }
    }
}