I wrote this method (almost similar in other post)
public void update(string fileName, string sheetName)
{
string connString = connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(fileName) + ";Extended Properties='Excel 12.0;HDR=NO'";
try
{
OleDbConnection oledbConn = new OleDbConnection(connString);
oledbConn.Open();
OleDbCommand cmd = new OleDbCommand("UPDATE ["+sheetName+"$B5:B5] SET F1=17", oledbConn);
cmd.ExecuteNonQuery();
oledbConn.Close();
}
catch(Exception ex)
{
Debug.Write("Error: " + ex.Message);
}
}
and calling that method:
update("test.xls", "test");
So far, it works fine because when I open the test.xls file, B5 gets updated to 17. However, if there is a cell: B1 is dependent on B5: B1=B5*5, then B1 will not get updated automatically. I have to manually open the Excel file and save it with warning in order to get B1 updated. How can I do it programmatically?
I don't think that you can depend on Excel updating calculated columns when you use the ACE driver to interact with the Excel worksheet. When you are using OLEDB to operate on the workbook's worksheet, it is treating the worksheet as a database table like structure.
I think you may want to use OpenXML to read/write to the file. There are several threads on StackOverflow with more info on using OpenXML that are worth checking out.
This post shows your exactly how to force a cell re-calc using OpenXML.
Related
I get the Excel sheet with data uploaded from UI, I need to access that sheet through WCF service and insert the data contained in it into a SQL table.
Kindly guide how this can be done.
I am facing challenge in reading that Excel sheet through WCF service.
I have used below code to read a Excel sheet from a local drive. Change Source property to your FTP location of your excel sheet.
string con = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\MyData.xlsx;" +
#"Extended Properties='Excel 8.0;HDR=Yes;'";
using (OleDbConnection connection = new OleDbConnection(con))
{
connection.Open();
OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);
using (OleDbDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
// Do your things here
}
}
}
Note: .xls has data limitations whereas .xlsx supports more data rows in a sheet. You need to install Microsoft Office Access database engine 2007 if you are going to read .xlsx files
string UploadedFilePath = FullPathOfExcelOnTheServer;
string ExcelConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + UploadedFilePath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1;FMT=Delimited\"";
using (OleDbConnection oledbConnExcel = new OleDbConnection(ExcelConn))
{
oledbConnExcel.Open();
using (OleDbDataAdapter oledbAdapterExcel = new OleDbDataAdapter("SELECT * from [" + SheetName + "$]", oledbConnExcel))
{
using (DataTable dtblSheetData = new DataTable())
{
try
{
oledbAdapterExcel.Fill(dtblSheetData);
}
catch (Exception lexQuery)
{
}
}
}
oledbConnExcel.Close();
}
Fact is the is no way to read the excel data through WCF Service. But You can cosider this approaches
You may:
Read all excel data on client side and call WCF service and include all data from excel in parameter as (array of strings)?
You can create macro(VB) in Excel sheet and what call WCF Service and send data through parameter. When You have all logic in one place.
If client side is .NET side You can use atribbute [KnowsTypeAtribbute].Then You can call WCF service with param type as Excel type (Sheet or other). May by use some type form OpenXml dll may by usefull (But I don't know is sheet or other types is serializable. A serializable is require)
You can send to WFC service path to file and read excel file from that path through WCF service.
I've been doing research on this problem for about a day and a half and haven't found a solution to my problem yet. This is my scenario:
I have a AsyncFileUpload and a button which is configured to accept only xls and xlsx files (Excel) file. This file that is uploaded is used to import information into a gridview. The old format xls files works fine. The problem comes when I want to upload the xlsx file. I've tested multiple scenarios and I have found that it works perfectly when the xlsx file is currectly/actively open. When it is closed it gives me errors. I have fiddled and explored solutions to this problem and the range of errors I have received is quite large. But the most prominent errors where the following:
1. External table is not in the expected format.
2. could-not-find-installable-isam
I have found an article relating to the second error (Could not find installable ISAM) but I haven't tried all the suggested solutions yet because I would rather avoid making changes to the windows registry because I do not have exstensive knowledge regarding this matter.
I'm attaching the code that I use to make the connection between the OleDb and the excel file:
protected void AsyncUpload_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
string connString = "";
string strFileType = Path.GetExtension(e.FileName).ToLower();
string path = e.FileName;
////Connection String to Excel Workbook
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;HDR=Yes;IMEX=1";
}
string query = "SELECT * FROM [sheet1$]";
OleDbConnection conn = new OleDbConnection(connString);
if (conn.State == ConnectionState.Closed)
conn.Open();
OleDbCommand cmd = new OleDbCommand(query, conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
using (OleDbDataReader dr = cmd.ExecuteReader())
{
_InsertWrapper.clearLists();
while (dr.Read())
{
var row1Col0 = dr[0];
Console.WriteLine(row1Col0);
_InsertWrapper.GenerateList(dr);
}
}
da.Dispose();
conn.Close();
conn.Dispose();
InventoryGrid.DataBind();
ErrorsGrid.DataBind();
}
If someone has additional knowledge or an explanation that can contribute please add it so that more people that are struggling with the same type of problem can gain as much information from this question as possible.We are all still learning.
So, main idea: Need to find a solution to successfully upload the xlsx file
Try using third party tools. They work efficiently:
http://code.google.com/p/excellibrary/
I am trying to update some data in an Excel sheet of the format "xlsx" using OLEDB connection, but I am unable to make out the connection establishment.
Here is my code:
String sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source='" + "D:\abc1.xlsx" + "';Extended Properties='Excel 8.0;HDR=Yes'";
OleDbConnection con = new OleDbConnection(sConnectionString);
con.Open();
OleDbCommand com = new OleDbCommand("select * from Sheet1",con);
OleDbDataReader reader = null;
reader = com.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader[0]);
}
con.Open();
Console.ReadLine();
}
When I run the code, I'm facing the following exception:
The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local
machine.
Any idea how to recover from this exception or any other suggestions from which i can update my data in excel is advisable.
This could be the provider you have stated try changing it to the one which matches the Excel version on your machine
Try
Provider=Microsoft.ACE.OLEDB.12.0;Data Source='D:\abc1.xlsx';Extended Properties="Excel 12.0 Xml;HDR=YES";
Instead
Could also be that excel isnt installed
Also check that you have referenced the OLEDB library for your project
Change your PlatformTarget Type from AnyCPU to X86.
Steps:
Goto Project Properties.
Select Build tab.
Select X86 from PlatformTarget options.
It is possible that there are multiple reasons for this exception.
1) You could use the OleDbEnumerator class to find out what providers are available.
Accordingly you set your connection string.
2) Before that just try out below connection string.
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + "D:\abc1.xlsx" + "';Extended Properties='Excel 8.0;HDR=Yes'";
3) If you have a 64 bit OS, there is no 64-bit version of the JET provider available and there's no alternative. As long as you want to support JET, you'll need to set the build target to x86.
first save as your excel workbook as Excel 97-2003 Workbook
It will work in my project...
string filepath = Server.MapPath("~/ImportData/") + fileUpload.FileName;
OleDbConnection oconn = new OleDbConnection
(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";
Extended Properties=Excel 8.0");`
oconn.Open();
OleDbDataAdapter adp = new OleDbDataAdapter("select * from Sheet1", oconn);
DataSet ds = new DataSet();
adp.Fill(ds);
oconn.Close();`
When I try this code OleDBConnection.open() not work and didn't throw any error, just open windows form and say anything I see messageBox try1 but program didn't show try2 what is the wrong in my connection string please help I've tried also excel 12.0 but it looks in reference Excel 14.0 in References (Microsoft Excel 14.0 Object Library) and the file is exist in c:\product.xlsx
OleDbConnection conn_exel = new OleDbConnection(#"provider=Microsoft.Jet.OLEDB.12.0; Data Source=C:\product.xlsx; Extended Properties=""Excel 14.0;HDR=Yes;""");
conn_exel.Open();
MessageBox.Show("try2");
OleDbCommand command_exel = new OleDbCommand(#"SELECT * FROM [Sayfa1$] WHERE id = 1",conn_exel);
OleDbDataReader reader_exel = command_exel.ExecuteReader();
MessageBox.Show("try3");
while (reader_exel.Read())
{
MessageBox.Show(reader_exel.GetString(1));
}
conn_exel.Close();
Try like this
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;
Extended Properties="Excel 12.0 Xml;HDR=YES";
"HDR=Yes;" indicates that the first row contains columnnames, not data. "HDR=No;" indicates the opposite.
reference
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + PathNam + ";
Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"
http://www.nullskull.com/q/10173180/hello-would-you-explain-imex-option-when-import-excel.aspx
for IMEX usage
I have an xls file i would like to read using c# and populate the information in a data table. The code I am using is :
public static DataTable GetExcelData(string excelFilePath)
{
OleDbConnection objConn = null;
string oledbConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFilePath + ";Extended Properties=Excel 10.0;";
objConn = new OleDbConnection(oledbConnectionString);
if (objConn.State == ConnectionState.Closed)
{
objConn.Open();
}
var objCmdSelect = new OleDbCommand("Select * from [Sheet1$]", objConn);
var objAdapter = new OleDbDataAdapter();
objAdapter.SelectCommand = objCmdSelect;
var objDataset = new DataSet();
objAdapter.Fill(objDataset, "ExcelDataTable");
objConn.Close();
return objDataset.Tables[0];
}
Once this data table is populated, I need to remove the first 5 or so rows which contain header information, and loop through the data table populating an access database table. I have had no luck with this or any of the other 10,000 ways suggested. Does anyone have any information that can help me. I am running VS2010 .net 4.0 framework. Any and all help would be super appreciated.
Thanks,
John
I've had a great deal of trouble trying to get Excel data into a DataTable using OLEDB. I finally solved the issue by switching to a solution that uses Excel Interop. Please see this answer for further explanation and sample code:
Importing from Excel: some cells become null