Excel workSheet Column names using C# 4.0 - c#

Im using ExcelQueryFactory to retrieve the column names of a worksheet using C# 4.0.I'm able to see the list of column names but why do i get an additional set of column names like F12,F100,F20 etc . It happens with any reader i used to read my excel file.

It's not an C# issue, that is an Excel issue.
if your columns do not have column names, Excel will provide names in the format F###, where the number is the number of the column, and F is short for Field (I guess).
Now, if you have any columns in the file that at anytime contained data, or are referenced in formulas, or Excel just thinks they are important, you'll get them in the column list.
Just filter out any columns in that format, of course with the caveat that real columns with real names like F7 will get the short end of the stick there.

Related

I Help and Advice on using Oledb for large excel Files

So I am new to Oledb and I a have project that requires me to grab data from an excel file using a Console Application. The excel file has around 500 columns and 55 rows. How could I possibly get data from columns past 255?
In order to read columns 256 -> you just need to modify the Select statement. By default both the Microsoft.ACE.OLEDB.12.0 and the Microsoft.Jet.OLEDB.4.0 drivers will read from Column 1-255 (A->IU) but you can ask it to read the remaining columns by specifying them on the Select statement.
To read the next 255 columns and taking "Sheet1" as your sheet name you would specify...
Select * From [Sheet1$IV:SP]
This will work even if there aren't another 255 columns. It will simply return the next chunk of columns if there are 1...255 additional columns.
Incidentally, the Microsoft.ACE.OLEDB.12.0 driver will read both .xls and any variant of .xlsx, .xlsm etc without changing the extended properties from "Excel 12.0". There is no need to if...then...else the connection string depending on the file type.
The OLEDB driver is pretty good for the most part but it really does rely on well formed sheets. Mixed data types aren't handled terribly well and it does weird things if the first columns/rows are empty but aside from that it's fine. I use it a lot.

EPPlus: How to update Pivot Table SourceRange

I'm getting stuck trying to update the SourceRange of a Pivot-Table with EPPLus inside a C# class.
I've found that CacheDefinition.SourceRange contains the DataSource of my existing Pivot-Table but I don't know how to change it.
Existing Pivot-Table datasource is a range on a data worksheet in the same Excel file.
Any advice?
Thanks in advance,
Alessandro
This might work:
You can create a self-dimensioning defined name that encompasses your data range. I use this all the time.
Open Name Manager.
Click New.
Enter a Name for your range.
Put the following in the Refers to: line
OFFSET(DataSource!$A$1,0,0,COUNTA(DataSource!$A:$A),COUNTA(DataSource!$1:$1))
syntax: OFFSET(reference, rows, cols, [height], [width])
Substitute your sheet/tab name for DataSource. This assumes that the table starts in A1 (first section) and you that you want a defined name as long as the number of values in column A and as wide as the values in row 1. It is a very flexible and useful method for making sure your defined names encompass all the data on the sheet.

Create excel file via c# that support filtering merged cells

I am trying to create an excel file via c# in which the cells that contains the same data are merged. This is the easy part, the hard part is to make the excel display all the rows of the merged cells with the same value when filtering that value.
For example, for this data:
I expect this result after filtering the value "bla":
The result I would normally get after filtering merged cells is only the first row with this value (in this case, the row that contains the value "1").
I have found this solution which is extremely not elegant and not efficent:
Create two copies of the the data, in the first copy merge cells with the same values and in the second copy don't merge cells with the same value.
Use the format painter to copy the format of the merged version of the data onto the unmerged version of the data (apparently this way keeps the data of the cells but display them as merged).
delete the merged copy of the data.
I guess I can program this solution, but there must be a better way to solve this problem.

Insert columns form one excel book to another in C#

I need to take values form one sheet in one Excel workbook and insert them into another existing workbook.
The values I need to take are the first 6 columns of the first file:
And I want to insert them at the beginning of another book like so
I've been using Spire.Xls to read values from the first sheet and I thought I could just do the same; parse the worksheet, read the values and just paste them into the other sheet, but that wouldn't work because three of the columns I want to copy have the same header "Descripcion" so my parser would only take the values form the first descripcion column and skip the other ones.
Is there any way, using Excel.Interop or maybe Spire itself to copy and paste entire columns between workbooks? Or alternately, is there any way to get all of the 3 "descripcion" values (without rewriting the title of the columns)?
VSTO might be helpful. I've done similar tasks in C#/VSTO.
Perhaps read through: Simple Example of VSTO Excel using a worksheet as a datasource

OleDbDataReader does not see data in the cell(.xlsx)

Have a really weird problem with reading xlsx file(I'm using OleDbDataReader).
I have a column there that consist of the following data:
50595855
59528522
C_213154
23141411
The problem is that when I read this column the reader shows me that the third row is empty. The column format in Excel is set to 'General'. But when I set the format to 'Text', everything works fine and reader sees the data in that row.
So just for a sake of experiment, I prefixed first two rows with letter and made it look like following :
C_50595855
C_59528522
C_213154
23141411
And the reader reads everything without problem even when the column format is set to 'General'.
So Excel apparently somehow analyses data in the column before loading it, and it gets confused when first cells of the column look like numeric and some of the rest are texts..
It is really weird to me as either there is data in the cell or there isn't.
Anyone have any ideas why this is happening?
Any help will be much appreciated.
Regards,
Igor
As you surmised it's an issue caused by mixed data types. If you search on "OleDBDataReader mixed types" you'll get some answers. Here's an MSDN page that describes the problem:
"This problem is caused by a limitation of the Excel ISAM driver in that once it determines the datatype of an Excel column, it will return a Null for any value that is not of the datatype the ISAM driver has defaulted to for that Excel column. The Excel ISAM driver determines the datatype of an Excel column by examining the actual values in the first few rows and then chooses a datatype that represents the majority of the values in its sampling."
... and the solution(s):
"Insure that the data in Excel is entered as text. Just reformatting the Excel column to Text will not accomplish this. You must re-enter the existing values after reformatting the Excel column. In Excel, you can use F5 to re-enter existing values in the selected cell.
You can add the option IMEX=1; to the Excel connect string in the OpenDatabase method. For example:
Set Db = OpenDatabase("C:\Temp\Book1.xls", False, True, "Excel 8.0; HDR=NO; IMEX=1;")
"

Categories

Resources