I can't read this excel file of mine after the 8th row. I am using a OLEDB connection to access it from a c# script task inside a SSIS package :
strCoExcel = "Provider = Microsoft.ACE.OLEDB.12.0;Mode=Read;Data Source =" + Path.Combine((string)Dts.Variables["PathINPUT"].Value, Dts.Variables["FileNameForEach"].Value.ToString()) + ";Extended Properties=\"Excel 12.0 Xml;HDR=NO;ImportMixedTypes=Text;TypeGuessRows=0;IMEX=1;\"";
//Gathering data from the renamed sheet
OleDbDataAdapter adapter = new OleDbDataAdapter("Select * from [DataTQT$]", coExcel);
DataTable data = new DataTable();
adapter.Fill(data);
What is wrong:
Some excel files are opened and everything is fine but others do not produce any rows or only 8.
I tried the following:
-HDR no/yes
-IMEX =1 doesnt change anything
-nor do ImportMixedTypes=Text;TypeGuessRows=0
-setting all the cell from the excel file to standard or text field
any help ?
OK so the final answer for me was not in this piece of code as suspected by others.
This data goes into a SQL database and the columns were not big enough for the data i wanted to insert. I modified the table and now everything works fine.
Related
Update:
Using the open source ExcelDataReader (https://www.nuget.org/packages/ExcelDataReader/) solved the problem.
I'm reading in Data from an .xlsx File like this
DataSet ds = new DataSet();
OleDbConnection cn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties= Excel 12.0;");
cn.Open();
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM ["Sheet1$"]", cn);
da.Fill(ds, "Sheet1$");
cn.Close();
And Everything seems to work fine, BUT in two specific rows the adapter just cuts off the last line of one Cell(both affected rows have the same content in that specific Cell).
i.e.:
"1 : 0x01 : Text_Example1(sometext)
2 : 0x02 : Text_Example2(sometext)
3 : 0x04 : TexT_Example3(sometext)"
becomes
"1 : 0x01 : Text_Example1(sometext)
2 : 0x02 : Text_Example2(sometext)
3 :"
What is the Problem here and how can i fix it?
To me it seems totally random, because in all the other rows this problem doesn't occur.
edit:
I have already tried it with extended properties HDR = NO/YES and IMEX = 0/1 like suggested on many related Questions, but that didn't do the trick either.
If I may suggest an alternative approach, an .xlsx file is an OpenXML file -- a ZIP file containing XML and other metadata.
Microsoft has an OpenXML API that allows you to manipulate such files. That's the "native" way to access the data, and should behave correctly.
There's an open source project called ClosedXML that aims to make the OpenXML API more developer-friendly. I haven't used it myself, but have seen it successfully used in existing code.
I have a project where I need to query an Excel spreadsheet. Basically I need to read a two column Excel spreadsheet into a dictionary table. I have a database of error codes and descriptions for each code. The project requires Excel (Excel 2007 compatible .xlsx) because the user wants to add new Key/Description pairs by editing an Excel spread sheet.
The idea is that I am going to get a list of error codes and I simply want to be able to display a table in a worksheet with error code and the associated description from the Excel spreadsheet, or no description found.
I figure if I can get the table (which is not very long) into a dictionary table the rest would straight forward but I cannot even find a starting point using the current version C# and 4.5.2 .NET API. I just need to open the worksheet and read the two column table into a dictionary table and I am off to the races.
Can someone please get me started in the right direction please?
You can probably use OleDbConnection to get the data from Excel.
OleDbConnection con = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source="
+ xlsx file with path
+ ";Extended Properties=Excel 8.0;");
StringBuilder stbQuery = new StringBuilder();
stbQuery.Append("SELECT * FROM [" + SHEETNAME_HERE + "$A1:B65]");
OleDbDataAdapter adp = new OleDbDataAdapter(stbQuery.ToString(), con);
DataSet dsXLS = new DataSet();
adp.Fill(dsXLS);
Once you have the data filled up, you can fetch the data in dictionary or any other object of List Enumerable.
I have a website where one of the functions is importing excel sheets to a SQL database. Right now I am loading the data into a data table and then assigning a variable to a column index. That is working just fine, but if the excel sheet is in a different format then it wont import to the database.
Does anyone know of another way to import a specific column(s) into a SQL database other than using the column index? I have also used the column name, but what if the column name changes?
You can use like this.
var fileName = string.Format("{0}\\fileNameHere", Directory.GetCurrentDirectory());
var connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", fileName);
var adapter = new OleDbDataAdapter("SELECT * FROM [workSheetNameHere$]", connectionString);
var ds = new DataSet();
adapter.Fill(ds, "anyNameHere");
DataTable data = ds.Tables["anyNameHere"];
The references for this is
1) How to read data from excel file using c#
2) Reading Excel files from C#
I hope it helps.
I'm using OleDbConnection with the following connection string:
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "'{0}'" + ";Extended Properties='Excel 12.0 xml;'";
I'm importing the Excel file into an OleDbDataReader and then using reader.Read() to read in the rows, as follows:
while (reader.Read())
{
// import row
}
For some reason, the last row of the Excel file is always missing in the import. I've tried it with several different Excel files, but it has never worked. Out of desperation, I've also tried iterating one more time after the loop ends, but it tells me there is no more data. What could possibly be the problem?
Here is my code to read uploaded Excel file. Which is working absolutely fine for past 3 months.
var connectionString = GetOleDbConnectionString(file);
using (var dataAdapter = new OleDbDataAdapter("select * from [Sheet1$]", connectionString))
{
dataAdapter.Fill(ds, tableCount.ToString());
}
private static string GetOleDbConnectionString(string file)
{
var fileExtension = Path.GetExtension(file);
if (fileExtension.EqualsCCIC(".xlsx"))
{
return #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;".F(file);
}
}
Problem: Uploaded excel file got "StartDate" as first column. But this column also has employee name along with dates (I need to read this employee name to process this sheet).
I came across one new excel file (Excel2007 .xlsx). When I upload new file (Which has both employee name and dates) it is only reading dates from the column and ignoring employee names. my dataset showing (While debugging) those cells in data table as empty strings. According to business logic I need to know which employee these dates belongs to. I removed locks for entire sheet(cell formatting>>Protection>> lock) but still no use. How can I solve this problem? I have no clue...
It is successfully reading old files (2007 .xlsx) I didn't understand what is it that makes OLEDB to hide strings in Date column?
So you're saying that the new excel file is the issue? If so...
check the data in the file (particularly the first 8 rows in the employee name column)
copy the data from the new file to a file that is known to work
Accessing Excel Spreadsheet with C# occasionally returns blank value for some cells
Check out ABHI's answer in the above link (in particular points 1. and 2.)