I have a CSV file that needs to be opened in excel. I want to read an entire row at a time into my program and store it in some kind of list or array. In past projects I have used:
DateTime[] dates = xlworksheet.get_Range("B7", "B"+xlWorksheet.Rows.Count);
This is giving me the error: "Cannot convert type 'object[,]' to 'System.DateTime[]'". This makes sense but I don't know how to store the entire column other wise. How do I read an entire column from an excel worksheet into a list/array in my program?
I am assuming you are using .NET 3.5 or higher. Add a using System.Linq;
DateTime[] dates = xlworksheet.get_Range("B7", "B"+xlWorksheet.Rows.Count).Cast<DateTime>().ToArray();
Related
I have created a program which converts an Excel worksheet into a text file. I load the whole of the worksheet into an array then loop through it and write out the columns I need. Some of the columns are dates.
My issues are some dates are e.g. 06/09/2014 but are being output as 09/06/2014. I have formatted the dates using CultureInfo("en-GB") when I read it from the array. Is this not the right way?
The program i'm doing now has me grabbing info from our database and exporting it to an Excel file. This is the first time I've done this so, sorry for the stupid questions (possibly).
I am having problems formatting the date/time.
The database has the date/time as : 9/19/2013 5:30:00 PM
However, whenever it is exported to the Excel file the cell shows : 9/19/2013 17:30
I just want the military time to go away.
You can apply whatever format you want to the excel cells manually at the end of the process, at the beginning by putting the data into a template or in code.
Also, you can link the spreadsheet directly to the data (subject to which RDMS you use), no program required!
If you aren't using custom formats directly in Excel then you can just write the data in your C# code using string formatters.
http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.85).aspx
I have an excel sheet and I am processing it by using Open XML SDK 2.0. The scenario is, There is a column which contains date in my excel sheet. I need to get the maximum and minimum date from that column.
I can do this by looping and reaching to that cell, doing comparisons and finding desired answer.
But due to optimality I want to do this by using LINQ to get Minimum and maximum dates.
Is it possible to do so? If yes, then how?
You can see how to get IEnumerable of all cells from column there:Read excel sheet data in columns using OpenXML, and use Max() on it.
Thanks to all
I have used like this
IEnumerable<Cell> cells = workSheetPart.Worksheet.Descendants<Cell>().Where(c => string.Compare(GetColumnName(c.CellReference.Value), strIndex, false) == 0).OrderBy(c => c.CellValue.Text);
And getting min and max values like this
int cellCount = cells.Count();
Cell MaxCell = cells.ToArray()[0];
Cell MinCell = cells.ToArray()[cellCount - 1];
You will want to take a look at the LINQ Min() and Max() functions. If you need to return the entire Cell object, you can use OrderByDescending().
You could read this post Open XML SDK and LINQ to XML by Eric White (Eric wrote a lot of posts about OpenXML and LINQ). And then you will be able to query your Excel file data with LINQ. You might want to see the spreadsheet objects structure in The Open XML SDK Productivity Tool, which can generate the source code for you. You could use this code to understand how to programmatically access the data you need.
I have a old excel file , i want to modify that.
i need to filter rows from my old excel file and write selected rows to new excel file.
My plan is to read a single row,store it in a list,pass this arraylist to a function.
The function does some checking on the arraylist, if the condition is satisfied, i will write this entire row to my new excel file else i will go back and read next row.
I am not getting anything to read a single row and save it in a arraylist.
I am able to read cell by cell and do conditioning , but this is very slow.
Any better option.
Read the entire input file into a custom-made object at the same time, do your work on it, then write all of that data at the same time into the final Excel file at the same time.
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.