繁体   English   中英

如何从SQL数据库检索Excel文件

[英]How to retieve a excel file from sql database

我在应用程序中使用引导程序。我在sql db中存储了一个excel文件。

这是我的代码;

Default.aspx的

 <div class="control-group">
  <label class="control-label">
   Upload</label>
  <div class="controls">
   <input id="fileupload" type="file" runat="server"/>
  </div>
 </div>
<asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click"  />

Default.aspx.cs

  protected void btnSave_Click(object sender, EventArgs e)
    {               
            string filePath = Server.MapPath("~/Upload/");
            HttpFileCollection hfc = Request.Files;

            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }          
            if (hfc.Count != 0)
            {
                string contenttype = String.Empty;

                    HttpPostedFile hpf = hfc[i];

                    string fileExtn = Path.GetExtension(hfc[i].FileName).ToLower();

                    if (fileExtn == ".xls" || fileExtn == ".xlsx" || fileExtn == ".xlmx")
                    {                      
                        switch (fileExtn)
                        {

                            case ".xls":

                                contenttype = "application/excel";

                                break;

                            case ".xlsx":

                                contenttype = "application/excel";

                                break;

                            case ".xlmx":

                                contenttype = "application/excel";

                                break;
                        }

                        if (hpf.ContentLength > 0)
                        {
                            string SPfilePath = Server.MapPath("~/Upload/");
                            string filepath = SPfilePath + System.IO.Path.GetFileName(hpf.FileName);
                            hpf.SaveAs(filepath);                                                       
                            string ContentType = hpf.ContentType;
                            int fileLen = hpf.ContentLength;
                            byte[] fileBytes = new byte[fileLen - 1];
                            hpf.InputStream.Read(fileBytes, 0, fileBytes.Length);
                             byte[] Data = fileBytes;                     
                        }
                       new DefaultManager().SaveFile(Data , fileLen ,filepath ,ContentType );
                }
            }
            UploadMsg = auditmgr.AddClientAuditReport(, audit);         
        }
    }

在这里,我将ContentType保存为varchar(256) ,将文件filepathvarchar(256) ,将fileLenint ,将Data为db中的varchar(max) 它被添加到数据库中。 但我不知道如何从db检索该excel文件。

我在SQL数据库中以字节为单位存储该Excel文件

首先将“ Data as varchar(max)”的数据类型更改为varbinary(max),然后将文件内容保存在数据库中

//对于从数据库中检索文件,您可以使用此代码

DataTable dt = GetFileDetailFromDatabase(fileID: 20); // here you select data from database, for Ex. fileID is 20

string strpath = @"C:\Download\" + dt.Rows[0]["FileName"].ToString(); // if you are storing file name(Only file name with extension not full file path) in database other wise give any temp file name with same extension which file had at the time of save in database

FileStream flStrm = new FileStream(strpath, FileMode.Create);
Byte[] Buffer = (Byte[])dt.Rows[0]["Data"];
flStrm.Write(Buffer, 0, Buffer.Length);
flStrm.Close();
Buffer = null;
 DataTable dtReport = GetDataFromDatabase(fileID: 20);     
 Response.Clear();
 Response.Buffer = true;
 Response.ContentType = Path.GetExtension(dtReport["FileName"]).ToString();
 Response.AddHeader("content-disposition", "attachment;filename=" + Path.GetFileName(dtReport["FileName"]).ToString());
 Response.ContentType = Path.GetExtension(dtReport["FileName"]).ToString();
 Response.Charset = "";
 Response.Cache.SetCacheability(HttpCacheability.NoCache);
 Response.BinaryWrite((byte[])dtReport["Data"]);
 Response.End();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM