如何从SQL Server DB中读取和查看Excel文件 [英] How to Read and view Excel file from SQL Server DB

查看:104
本文介绍了如何从SQL Server DB中读取和查看Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我使用C#将Excel上传到数据库中。

现在我想要检索相同的excel文件视为卓越。要从DB读取文件,我编写的代码在指定的路径中保存为字节文件。但是我无法以excel的形式打开文件。



请在下面找到我编写的代码来下载文件。

请告诉我如何以excel和view的形式打开它。



  public   void  readExcelFromDB()
{
SqlConnection conn = new SqlConnection( @ 我的连接字符串);


conn.Open();
SqlCommand Cmd = new SqlCommand( select来自Test_Excel的ExcelSheet,其中ExcelID = 1,conn);
Cmd.CommandType = CommandType.Text;
// SqlCommand Cmd = new SqlCommand(select Request from Requests,RequestsID = 27,conn);


SqlDataReader Reader = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
//
string DocumentName = null ;
FileStream FStream = null ;
BinaryWriter BWriter = null ;
//
//
//
byte []二进制= null ;
const int ChunkSize = 100 ;
int SizeToWrite = 0 ;
MemoryStream MStream = null ;
//
while (Reader.Read())
{
DocumentName = Reader [ ExcelSheet ]的ToString();
// 创建一个文件来保存输出。
FStream = new FileStream( @ D:\ Mathi_Working \ + DocumentName,FileMode.OpenOrCreate,FileAccess.Write);
BWriter = new BinaryWriter(FStream);
Binary =(Reader [ ExcelSheet]) as byte [];
SizeToWrite = ChunkSize;
MStream = new MemoryStream(二进制);
//
for int i = 0 ; i < Binary.GetUpperBound( 0 ) - 1 ; i = i + ChunkSize)
{
if (i + ChunkSize > = Binary.Length)SizeToWrite = Binary。长度 - i;
byte [] Chunk = new byte < /跨度> [SizeToWrite];
MStream.Read(Chunk, 0 ,SizeToWrite);
BWriter.Write(Chunk);
BWriter.Flush();
}
BWriter.Close();
FStream.Close();

}
FStream.Dispose();

conn.Close();

Process.Start( @ D:\Mathi_Working \ + DocumentName);
}



谢谢&问候,

Mathi。

解决方案

检查以下链接。可以帮到你。



Word,Excel,Image ,XML文件Stroe / Retrive from Database [ ^ ]



http://chiragrdarji.wordpress.com/2007/08/31/storing-and-retrieving-docpdfxls-files -in-sql-server / [ ^ ]





我希望这会对你有所帮助。

Hi,

I have uploaded an Excel into the DB using C#.
Now I want to retrieve the same excel file and view as excel. To read the file from DB I have written code which saves as byte file in the path specified. But I'm unable to open the file as an excel.

Please find the code below which I have written to download the file as byte.
Kindly let me know how could I open it as an excel and view.

public void readExcelFromDB()
        {
            SqlConnection conn = new SqlConnection(@"My Connection String");
        

            conn.Open();
            SqlCommand Cmd = new SqlCommand("select ExcelSheet from Test_Excel where ExcelID = 1", conn);
            Cmd.CommandType = CommandType.Text;
            //SqlCommand Cmd = new SqlCommand("select Attachment from Requests where RequestsID = 27", conn);
            

            SqlDataReader Reader = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
            //
            string DocumentName = null;
            FileStream FStream = null;
            BinaryWriter BWriter = null;
            //
            //
            //
            byte[] Binary = null;
            const int ChunkSize = 100;
            int SizeToWrite = 0;
            MemoryStream MStream = null;
            //
            while (Reader.Read())
            {
                DocumentName = Reader["ExcelSheet"].ToString();
                // Create a file to hold the output.
                FStream = new FileStream(@"D:\Mathi_Working\" + DocumentName, FileMode.OpenOrCreate, FileAccess.Write);
                BWriter = new BinaryWriter(FStream);
                Binary = (Reader["ExcelSheet"]) as byte[];
                SizeToWrite = ChunkSize;
                MStream = new MemoryStream(Binary);
                //
                for (int i = 0; i < Binary.GetUpperBound(0) - 1; i = i + ChunkSize)
                {
                    if (i + ChunkSize >= Binary.Length) SizeToWrite = Binary.Length - i;
                    byte[] Chunk = new byte[SizeToWrite];
                    MStream.Read(Chunk, 0, SizeToWrite);
                    BWriter.Write(Chunk);
                    BWriter.Flush();
                }
                BWriter.Close();
                FStream.Close();

            }
            FStream.Dispose();

            conn.Close();

            Process.Start(@"D:\Mathi_Working\" + DocumentName);
        }


Thanks & Regards,
Mathi.

解决方案

Check below links.May be helped to you.

Word, Excel, Image, XML Files Stroe / Retrive from Database[^]

http://chiragrdarji.wordpress.com/2007/08/31/storing-and-retrieving-docpdfxls-files-in-sql-server/[^]


I hope this will help to you.


这篇关于如何从SQL Server DB中读取和查看Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆