c# - 如何从sql数据库中检索excel文件

标签 c# mysql sql sql-server excel

我在我的应用程序中使用 Bootstrap 。我在 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)fileLenintData 为 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;

关于c# - 如何从sql数据库中检索excel文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20834217/

相关文章:

c# - 使用 C# 在线进行现代身份验证交换

php - 如何在 MySQL 中选择最后 n 行?

python - 基于Python中的公共(public)列合并两个文本文件

c# - 当我的方法在后台 WPF 中执行某些操作时,如何打开窗口或其他内容

c# - 转换类型不起作用

c# - 如何从位图图像中提取对象?

mysql - 如何从一列中得到多列?

php - 两个相同的日期时间;一个显示 12 小时,另一个显示 24 小时

mysql - 选择查询,其中日期列是 X 之前,其中 X 是另一列

sql - 可以使用 SQL 将报告分组在一起吗?