Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i'm using Libre office excelsheet which was in .ods format i need to upload all the coloumns and values inside the spreadsheet into the SQL SERVER
Posted

How to Import Excel File into SQL Server using SQLBULK in ASP.net
This example explains how to upload excel file, read Excel file data, save Excel file data and import into SQL Server using SQLBULK in ASP.Net.

Step:1 Create a Excel file:


Step:2 Create a Sql table in database:


Step:3 Now, add the code in "Default.aspx"



<asp:fileupload id="fupUpload" runat="server" xmlns:asp="#unknown">

<asp:button id="btnImport" font-bold="true" forecolor="White" xmlns:asp="#unknown">

BackColor="#136671" Height="23px" runat="server" Text="Import Excel Data"
onclick="btnImport_Click" />


Step:4 Add the code in "Default.aspx.cs"

Add these NameSpace

using System.IO;
using System.Data.OleDb;
using System.Data;


Write the code in Click Event of Import Button

protected void btnImport_Click(object sender, EventArgs e)
{
string strFilepPath;
DataSet ds = new DataSet();
string strConnection = ConfigurationManager.ConnectionStrings
["connectionString"].ConnectionString;
if (fupUpload.HasFile)
{
try
{
FileInfo fi = new FileInfo(fupUpload.PostedFile.FileName);
string ext = fi.Extension;
if (ext == ".xls" || ext == ".xlsx")
{
string filename = Path.GetFullPath(fupUpload.PostedFile.FileName);
string DirectoryPath = Server.MapPath("~/UploadExcelFile//");
strFilepPath = DirectoryPath + fupUpload.FileName;
Directory.CreateDirectory(DirectoryPath);
fupUpload.SaveAs(strFilepPath);
string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
+ strFilepPath + ";Extended Properties=\"Excel 12.0
Xml;HDR=YES;IMEX=1\"";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds);
DeleteExcelFile(fupUpload.FileName); // Delete File Log
SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection,
SqlBulkCopyOptions.KeepIdentity);
sqlBulk.DestinationTableName = "Table_1";
sqlBulk.WriteToServer(ds.Tables[0]);
conn.Close();
sqlBulk.Close();
ScriptManager.RegisterStartupScript(Page, GetType(), "script1",
"alert('Excel file successfully imported into DB');", true);
return;
}
else
{
ScriptManager.RegisterStartupScript(Page, GetType(), "script1",
"alert('Please upload excel file only');", true);
return;
}
}
catch (Exception ex)
{
DeleteExcelFile(fupUpload.FileName);
ScriptManager.RegisterStartupScript(Page, GetType(), "script1",
"alert('error occured: " + ex.Message.ToString() + "');", true);
return;
}
}
else
{
ScriptManager.RegisterStartupScript(Page, GetType(), "script1",
"alert('Please upload excel file');", true);
return;
}
}

protected void DeleteExcelFile(string Name)
{
if (Directory.Exists(Request.PhysicalApplicationPath +
"UploadExcelFile\\"))
{
string[] logList = Directory.GetFiles(Request.PhysicalApplicationPath
+ "UploadExcelFile\\", "*.xls");
foreach (string log in logList)
{
FileInfo logInfo = new FileInfo(log);
string logInfoName = logInfo.Name.Substring(0,
logInfo.Name.LastIndexOf('.'));
if (logInfoName.Length >= Name.Length)
{
if (Name.Equals(logInfoName.Substring(0, Name.Length)))
{
logInfo.Delete();
}
}
}
}
}


Kishor Makwana
SoftWare Engineer
Insight Softech
www.insightsoftech.com
 
Share this answer
 
How about saving it as a CSV then importing that into SQL Server (which is a supported format)?
 
Share this answer
 
Comments
kesav prakash 4-Jul-13 1:41am    
how to create that spreadsheet into CSV file
_Damian S_ 4-Jul-13 1:49am    
File-->Save As-->CSV??

Full details here: https://help.libreoffice.org/Calc/Importing_and_Exporting_CSV_Files

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900