Click here to Skip to main content
16,023,117 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How about import excel 2013-2016 ?

What I have tried:

How about import excel 2013-2016 ?
Posted
Updated 10-Aug-16 20:44pm
Comments
Maciej Los 4-Aug-16 9:18am    
How about destination of import?

How about doing your own research.

See Working with MS Excel(xls / xlsx) Using MDAC and Oledb[^].
 
Share this answer
 
Comments
Maciej Los 4-Aug-16 9:17am    
5ed!
You have two options, use the Microsoft Office Interop

C#
private void OpenExcelFile(string filePath)
       {
           Excel.Application xlApp ;
           Excel.Workbook xlWorkBook ;
           Excel.Worksheet xlWorkSheet ;
           object misValue = System.Reflection.Missing.Value;

           xlApp = new Excel.Application();
           xlWorkBook = xlApp.Workbooks.Open(filePath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
           xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

           MessageBox.Show(xlWorkSheet.get_Range("A1","A1").Value2.ToString());

           xlWorkBook.Close(true, misValue, misValue);
           xlApp.Quit();

           releaseObject(xlWorkSheet);
           releaseObject(xlWorkBook);
           releaseObject(xlApp);
       }

       private void releaseObject(object obj)
       {
           try
           {
               System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
               obj = null;
           }
           catch (Exception ex)
           {
               obj = null;
               MessageBox.Show("Unable to release the Object " + ex.ToString());
           }
           finally
           {
               GC.Collect();
           }
       }


Or you can parse the excel file as an open xml document with use of the Open XML SDK:

Generating Excel 2010 Workbooks by using the Open XML SDK 2.0[^]
 
Share this answer
 
Below code only works when you have MS office installed on the machine or you need to install AccessDatabaseEngine_x64 drivers tool on your machine. AccessDatabaseEngine_x64 setup file is easily available & it is free.


Try out below code i think this is useful to you


aspx code

<form id="form1" runat="server">
<div>
<div style="float:left;width:100%;text-align:right">
<asp:Button ID="BtnDownload" runat="server" Text="Download" OnClick="BtnDownload_Click" /> </div>
<div style="float:left;width:100%">
<asp:FileUpload ID="FileUpload1" runat="server" /> &nbsp;&nbsp;
<asp:Button ID="BtnUpload" runat="server" Text="Upload" OnClick="BtnUpload_Click" />
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>
Download sample file <a href="SampleFile.xlsx">Click Here</a>
<p>&nbsp;</p>

</div>
<div style="float:left;width:100%">
<asp:Label ID="lblStatus" runat="server" Text="" ForeColor="Green"></asp:Label>
<asp:Label ID="lblErrorMsg" runat="server" Text="" ForeColor="Red"></asp:Label>
</div>
</div>

</form>




.cs file code

protected void BtnUpload_Click(object sender, EventArgs e)
{

if (FileUpload1.HasFile)
{
try
{
lblStatus.Text = "";
lblErrorMsg.Text = "";
string filename = Path.GetFileName(FileUpload1.FileName);
FileUpload1.SaveAs(Server.MapPath("~/MyFolder/") + filename);
lblStatus.Text = "File Updated:" + DateTime.Now;
BtnUpload.Enabled = false;
ReadExcelToTable(filename);

}
catch (Exception ex)
{
lblErrorMsg.Text = "The file could not be uploaded." + ex.Message;
lblStatus.Text = "";
BtnUpload.Enabled = true;
}
}
else
{
lblErrorMsg.Text = "Please select file.";
lblStatus.Text = "";
BtnUpload.Enabled = true;
}

}






private void ReadExcelToTable(string strFileName)
{

string connstring;

string strPath = Server.MapPath("~/MyFolder/");//System.Configuration.ConfigurationManager.AppSettings.Get("File_Path").ToString();

if (!strFileName.Contains(".xlsx"))
{
connstring = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + strPath + strFileName + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1';"; //This connection string is appropriate for Office 2007 and the older version. We can select the most suitable connection string according to Office version or our program.
}
else
{
//connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strPath + strFileName + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; // Extra blank space cannot appear in Office 2007 and the last version. And we need to pay attention on semicolon.
connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strPath + strFileName + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1'";
}
using (OleDbConnection conn = new OleDbConnection(connstring))
{
conn.Open();
DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); //Get All Sheets Name
string firstSheetName = sheetsName.Select("TABLE_NAME='Sheet1$'")[0][2].ToString(); //Get the First Sheet Name
string strQuery = string.Format("SELECT * FROM [{0}]", firstSheetName); //"Measurement Sheet" //Query String
DataSet set = new DataSet();
OleDbDataAdapter oDtAdaptor = new OleDbDataAdapter(strQuery, conn);
oDtAdaptor.Fill(set);
conn.Close();


try
{
int prodCount = 0;
StringBuilder sqlCommand = new StringBuilder();
string ErrorID = "";

foreach (DataRow Dr in set.Tables[0].Rows)
{

if (!DBNull.Value.Equals(Dr["Column1"]) && !DBNull.Value.Equals(Dr["Column2"]))
{
string Column1 = Convert.ToString(Dr["Column1"]).Trim();
string Column2 = Convert.ToString(Dr["Column2"]).Trim();
}

}

}
catch (Exception ex)
{


}
}
}


protected void BtnDownload_Click(object sender, EventArgs e)
{
Database db = DatabaseFactory.CreateDatabase("Constr");
string sqlCommand = "Select column1,column2 from {tablename}";
DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
DataSet dt = (DataSet)db.ExecuteDataSet(dbCommand);
dbCommand.Connection.Close();
string attachment = "attachment; filename=sample.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
Response.Write("column1\column2\n");
foreach (DataRow tr in dt.Tables[0].Rows)
{
Response.Write(tr["column1"] + "\t" + tr["column2"]+"\n");
}
Response.End();
}
 
Share this answer
 

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