Get specified row count in excel using c# - c#

I have a excel template where I have 1-100 row filled just with ID, my other column name and email is empty.
ID Name Email
1
2
3
.
.
100
here I have no data in the excel when I try to get row count I get 100, but I want to get the row count of either name or email which is filled with data, How can I do that.
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Open(fileName, 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);
range = xlWorkSheet.UsedRange;
int iRowCount = xlWorkSheet.UsedRange.Rows.Count;

You'll have to iterate over rows, because Excel will always show you the maximum used row and column number for worksheet.
Also, I would suggest you to not use Excel to read the data. And instead use some library, that can read files directly (and not depend on Excel installed). I used http://exceldatareader.codeplex.com/ quite succefully (at least for xlsx files). You may also want to download latest sources and build them your-self, because release is not very new, and there were a lot of fixes.
PS By not using Excel you will also solve the problem of performance, because using Excel as you showed in your code is very slow, and this will really matter when you start iterating over rows.
ExcelDataReader, on the other hand, will give a DataTable of data from excel worksheet, and will be able to parse it as you want in memory, which should be 100-1000 times quicker then working with excel.

You will have to access each Cell in your Column and check if it contains data using:
xlWorkSheet.Cells(x, y).Value

One easy way is to check which rows contain some value for aither Name or Email.
Use
System.Array values = (System.Array)range.Cells.Value;
after which loop through the array and check that either Name or Email are different than string.IsNullOrWhiteSpace.

Related

Reading from Excel dynamically in C#

I'm not sure if using the word "dynamic" is correct. Anyway, I do have some basic understanding of using the Microsoft.Office.Interop.Excel. The problem is, I'm having about 100 excel files in a folder, each of the excel files has different sheet name, number of rows and number of columns.
As far as I understand, you need to specify the range and sheet name, i.e.:
xcel.Worksheet sheet = someExcelFiles.Sheets["SomeSheetName"] as Excel.Worksheet;
Excel.Range range = sheet.get_Range("A1:A5");
Is there anyway so that my application can read all data in all of the excel files without having to specify the sheet name and range (row and columns)?
Short answer yes. Long answer From DotNetPerls which also contains grabbing number of sheets programatically.
Range excelRange = sheet.UsedRange;
object[,] valueArray = (object[,])excelRange.get_Value(
XlRangeValueDataType.xlRangeValueDefault);

How can i avoid COMException while adding multiple worksheets into a workbook using c# excel introp ?

I am trying to create a workbook having multiple worksheets but i am getting COMException as soon i try to add 6th worksheet in the workbook.
So is there any way to extend worksheets in a workbook ??
If you are using a loop to get your worksheet in a loop and not sure how many sheets are there in the workbook then I would recommend finding the total Sheets count in that workbook and then looping to get the worksheet object
For example
int SheetCount = xlWorkBook.Sheets.Count;
will give you the number of sheets in that workbook which you can use in a For Loop.
If you need more worksheets then you can use xlexcel.Worksheets.Add to get more worksheets.
Followup From Comments:
I tried this approach, and Yes it's not throwing exception when i am trying to get worksheets beyond 5 from workbook. But It's overwriting data (which i am writing for 6th sheet) into 5th sheet only even i try to get 6th worksheet after adding one in workbook. It seems below code snippet returns last available sheet in a workbook which is 5th one. m_ExcelSheet = (Excel._Worksheet)(m_ExcelSheets.get_Item(6)); How to stop overwriting data in the 5th worksheet after adding new worksheets ? – saurabh.mridul 1 min ago
The syntax of adding a worksheet is
expression.Add(Before, After, Count, Type)
If you do not specify the paramenters in the .Add then the worksheet will be added as the first worksheet.
When you loop though the worksheets after adding a worksheet without specifying the parameters then your worksheets(1) becomes the latest worksheet that you added. And hence your worksheet 5 (now worksheet 6) gets overwritten.
You need to specify that the new worksheet that you are adding has to be added at the end of the existing worksheets.
Description of parameters
Before: (Optional) (Data Type: Variant) An object that specifies the sheet before which the new sheet is added.
After: (Optional) (Data Type: Variant) An object that specifies the sheet after which the new sheet is added.
Count: (Optional) (Data Type: Variant) The number of sheets to be added. The default value is one.
Type: (Optional) (Data Type: Variant) Specifies the sheet type. Can be one of the following XlSheetType constants: xlWorksheet, xlChart, xlExcel4MacroSheet, or xlExcel4IntlMacroSheet. If you are inserting a sheet based on an existing template, specify the path to the template. The default value is xlWorksheet.
Here is an example on how to add a worksheet at the end.
//~~> Add a new worksheet at the end of the worksheets
xlWorkSheet = xlWorkBook.Sheets.Add(Type.Missing, xlWorkBook.Sheets[xlWorkBook.Sheets.Count], Type.Missing, Type.Missing);

Copy excel rows programatically with column widths

hope someone can help. i have an excel workbook and in that i have several sheets.
what i want to do is get row 1 from sheet named "Sheet1" and paste it into several other sheets (not all). Also, i want to maintain the column widths of row 1 from sheet 1 while copying, this is actually very important.
i mention several other sheets because i would like exclude some sheets, which i am planning to store in a list.
Microsoft.Office.Interop.Excel.Application xlApp = null;
Excel.Workbook xlWorkbook = null;
Excel.Sheets xlSheets = null;
Excel.Worksheet xlNewSheet = null;
any ideas how this can be done in c#?
thanks in advance.
Maintaining the width of a column or a row is very difficult to do. In my opinion: first copy the data, then read the width values of the old columns and apply them to the new columns.
All other solutions which I have seen rely on pastespecial and sometimes do not work.

Read Excel data from C#

I'm trying to read data from an Excel sheet using Office.Interoperability.Excel namespace. I'd like to get the first row of the sheet as the first row contains the headers, without specifying the start and end cells. Because I wouldn't know if a new column is added to the sheet.
Microsoft.Office.Interop.Excel.Application excelObj = new Application();
Microsoft.Office.Interop.Excel.Workbook myBook = excelObj.Workbooks.Open(#"D:\myFile.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 0, 0);
Microsoft.Office.Interop.Excel.Worksheet mySheet = (Worksheet)myBook.Sheets.get_Item(1);
Range range = mySheet.Cells.EntireRow;
Here, the range becomes the entire range and it doesn't get limited to the number of header columns. Also I've a huge data of about 10,000 rows to process.
If you requirement doesnt involve writing back to the excel file I would suggest that you use Excel Data Reader (http://exceldatareader.codeplex.com/) its a lot easier to use, doesnt require excel on the server and its faster
I think you're looking for this:
Range headers = mySheet.UsedRange.Rows(1);
I just answered another Excel reading question here: C# converting .xls to .csv without Excel
The FileHelpers library is perfect for your task. I use it myself for those numbers of rows and above.
I don't know what you are doing with the rows once they are read from Excel, but if you a looking at some processing that could be broken down into step, have a look at Rhino.Etl for that. It's a really powerful way to process large amounts of data.

Adding a new column at the start of an excel table in an excel

I am wondering how one can insert a new column at the beginning of an excel spreadsheet using C sharp.net. I am using Microsoft.Office.Interop.Excel; I want to use OLEO to do this, but I don't see that happening. I have been searching google on how to do this for the last two days now. I can't understand why there are not more tutorials on this?
Checkout out the Range.Insert method which you can call on a range of cells to insert entire rows/columns in front of them:
Worksheet sheet = (Worksheet) workBookIn.Sheets[1]; // Worksheet indexes are one based
Range rng = sheet.get_Range("A1", Missing.Value);
rng.EntireColumn.Insert(XlInsertShiftDirection.xlShiftToRight,
XlInsertFormatOrigin.xlFormatFromRightOrBelow);

Categories

Resources