Import and Export Excel Files into Varbinary (Max) in SQL Server

Introduction

In this article you will get the detail about How to Import and Export Excel files into varbinary(max)in Sql server Using Asp.net C#

What are advantages of storing as varbinary file?

  • We need not to depend on the file system,
  • It will avoid the legal issues and data risk

Aspx code

Add just two buttons,

  1. <asp:Button ID="BtnImportExcelToDB" runat="server" Text="ImportExcelToDb" OnClick="BtnImportExcelToDB_Click" />  
  2. <asp:Button ID="BtnExportExcelFromDB" runat="server" Text="ExportExcelFromDb" OnClick="BtnExportExcelFromDB_Click" />  
Codebehind code,

Code for Import Excel to Db,
  1. //Button Click event for the ImportExcelToDB   
  2. protected void BtnImportExcelToDB_Click(object sender, EventArgs e)  
  3. {  
  4.   
  5.     //Specify the filepath where the file exist  
  6.     string filename = @ "D:\TPMS\Uploaded_Boq\Raveena_Boq1.xlsx";  
  7.   
  8.     //pass the filename as a parameter  
  9.     this.StoreExcelFileToDatabase(filename);  
  10.   
  11. }  
  12.   
  13.   
  14. // store Excel sheet (or any file for that matter) into a SQL Server table  
  15. public void StoreExcelFileToDatabase(string excelFileName)   
  16. {  
  17.     // if file doesn't exist --> terminate (you might want to show a message box or something)  
  18.     if (!File.Exists(excelFileName))   
  19.     {  
  20.         return;  
  21.     }  
  22.   
  23.     // get all the bytes of the file into memory  
  24.     byte[] excelContents = File.ReadAllBytes(excelFileName);  
  25.   
  26.     // define SQL statement to use  
  27.     string insertStmt = "INSERT INTO dbo.Tender_Excel_Source(FileName, FileContent) VALUES(@FileName, @BinaryContent)";  
  28.   
  29.     // set up connection and command to do INSERT  
  30.     using(SqlConnection connection = new SqlConnection(OSMC.constring_Property))  
  31.     using(SqlCommand cmdInsert = new SqlCommand(insertStmt, connection))   
  32.     {  
  33.         cmdInsert.Parameters.Add("@FileName", SqlDbType.VarChar, 500).Value = excelFileName;  
  34.         cmdInsert.Parameters.Add("@BinaryContent", SqlDbType.VarBinary, int.MaxValue).Value = excelContents;  
  35.   
  36.         // open connection, execute SQL statement, close connection again  
  37.         connection.Open();  
  38.         cmdInsert.ExecuteNonQuery();  
  39.         connection.Close();  
  40.     }  
  41. }  
Code for Export Excel from DB,
  1. protected void BtnExportExcelFromDB_Click(object sender, EventArgs e)  
  2. {  
  3.     string filepathtostore = @ "D:\TPMS\Uploaded_Boq\Raveena_boq_From_Db.xlsx";  
  4.     RetrieveExcelFileFromDatabase(4, filepathtostore);  
  5. }  
  6.   
  7. public void RetrieveExcelFileFromDatabase(int ID, string excelFileName)   
  8. {  
  9.     byte[] excelContents;  
  10.   
  11.     string selectStmt = "SELECT FileContent FROM dbo.Tender_Excel_Source WHERE file_sequence_no = @ID";  
  12.   
  13.     using(SqlConnection connection = new SqlConnection(OSMC.constring_Property))  
  14.     using(SqlCommand cmdSelect = new SqlCommand(selectStmt, connection))   
  15.     {  
  16.         cmdSelect.Parameters.Add("@ID", SqlDbType.Int).Value = ID;  
  17.   
  18.         connection.Open();  
  19.         excelContents = (byte[]) cmdSelect.ExecuteScalar();  
  20.         connection.Close();  
  21.     }  
  22.   
  23.     File.WriteAllBytes(excelFileName, excelContents);  
  24. }  
  25. }  
Database Create Table Script
  1. USE [tpms_release1]  
  2. GO  
  3.   
  4. /****** Object: Table [dbo].[Tender_Excel_Source] Script Date: 09-06-16 Morning 10:19:05 ******/  
  5. SET ANSI_NULLS ON  
  6. GO  
  7.   
  8. SET QUOTED_IDENTIFIER ON  
  9. GO  
  10.   
  11. SET ANSI_PADDING ON  
  12. GO  
  13.   
  14. CREATE TABLE [dbo].[Tender_Excel_Source](  
  15. [fk_tender_id] [intNULL,  
  16. [file_sequence_no] [int] IDENTITY(1,1) NOT NULL,  
  17. [FileName] [nvarchar](1024) NULL,  
  18. [FileContent] [varbinary](maxNULL  
  19. ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]  
  20.   
  21. GO  
  22.   
  23. SET ANSI_PADDING OFF  
  24. GO  
Output

Output
Note

Excel File had converted to the Byte file and will be saved like this.

Excel File
Excel File
Note

Now vice versa Byte file converted into original Excel file.

Excel File

Hope the above information was useful, kindly let me know your feedback or suggestion.