Move to next cell in the next row in excel using c# - c#

How can I move to the next row in Excel using C#. I'm using Office PIA v 14. When I used Range.Next property, it takes me to the cell immediately right to the range. How can I move to the next row? ie. the cell immediately below.

Range.Next returns a Range object that represents the next cell.
I'm not really the greatest expert alive but according to the documentation you should use Offset instead. http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.range.offset
Here is some pseudo code
var excelApp = this.Application;
int skipRows = 1;
int skipCells = 0;
var nextRange = excelApp.ActiveCell.Offset[skipRows, skipCells].Select();

Try the following code:
Range oRng = ws.get_Range(Column + (row +1), Type.Missing);
where ws is the worksheet object.

Related

ExcelWorksheet.UsedRange is counting wrong if file has empty rows on top

I have c# windows application that is reading files content. I wanted to extract values from used rows only.
I am using this code:
int rows = ExcelWorksheet.UsedRange.Rows.Count;
Everything works fine. Except when I have empty rows on top, the counting will be incorrect.
-File has no special characters, formula or such. Just plain text on it.
-The application can read excel xls and xlsx with no issue if the file has no empty rows on top.
Okay, now I've realized I'm doing it all wrong. Of course it will not read all of my UsedRange.Rows because in my for loop, I am starting the reading always on the first row. So I get the ((Microsoft.Office.Interop.Excel.Range)(ExcelWorksheet.UsedRange)).Row; as a starting point of reading
This code works:
int rows = ExcelWorksheet.UsedRange.Rows.Count;
int fRowIndex = ((Microsoft.Office.Interop.Excel.Range)(ExcelWorksheet.UsedRange)).Row;
int rowCycle = 1;
for (int rowcounter = fRowIndex; rowCycle <= rows; rowcounter++)
{
//code for reading
}
Instead of read Excel row-by-row, better to get it in C# as a Range, and then handle it as
Sheet.UsedRange.get_Value()
for whole UsedRange in Sheet. Whenever you'd like to get a part of UsedRange, do it as
Excel.Range cell1 = Sheet.Cells[r0, c0];
Excel.Range cell2 = Sheet.Cells[r1, c1];
Excel.Range rng = Sheet.Range[cell1, cell2];
var v = rng.get_Value();
You well know size of v in C# memory from the values of [r1-r0, c1-c0]

delete every row after specific string in excel through C#

I want to delete every row that comes after my specific keyword and including the keyword row as well. Can anybody please help me with C# code.
Example :- I have not attached a dummy excel file as I am posting it from mobile.
I have a word in excel :- "hello user" so I have to delete all the lines of data after this including it as well. And most important this data is not on the fixed line it can be on any line number....Suppose for now I have given it on 10th line so it may come on 12th line in next file.
Line no. Data
10. "hello user"
11. A
12. B
And so on
.
.
.
.
.
I think this code should work for you:
const string magicWord = "HelloUser";
Excel.Application app = new Excel.Application();
Excel.Workbook workbook = app.Workbooks.Open(#"myWorkbook.xlsx");
Excel.Worksheet worksheet = workbook.Sheets[1]; //Excel has no zero based index!!!
int magicWordRowIndex = Int32.MaxValue;
//Here we find your magic word. But we can't delete the rows here, so just save the index
for (int row = 1; row <= worksheet.Rows.Count; row++)
{
for (int column = 1; column <= worksheet.Columns.Count; column++)
{
if (worksheet.Rows[row][column] == magicWord)
{
magicWordRowIndex = row;
break;
}
}
}
//Now we run reversed, because else our magicWordRowIndex become invalid if we delete rows
for (int row = worksheet.Rows.Count; row >= magicWordRowIndex; row--)
{
((Excel.Range) worksheet.Rows[row, Missing.Value]).Delete(Excel.XlDeleteShiftDirection.xlShiftUp);
}
Actualy I'm not able to test it but should do the job, maybe you need minor changes and fit it to your needs.
First it finds your magicword which is hardcoded on the beginning. After finding this word we go delete all rows from bottom up to your existing word.
Notice that you need to reference: Microsoft.Office.Interop.Excel

Auto expansion of formatted table in Excel using C#

I was trying to insert a new row at the end of a worksheet using OLEDB. The worksheet has a format table in a Range (a1:xx), with format and formula stored. But OLEDB insert does not come with any format.
I have read the post How to copy format of one row to another row in Excel with c# talking about get the format, but doesn't work for me. Also, I don't think it will get the formula.
In the Excel UI, at the lower right corner of a formatted table, a double arrow would appear, and we can drag it to expand the format table range.
Anything we could do through C#?
Thanks.
Excel.Range last = xlWS.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
Excel.Range RngToCopyOri = xlWS.get_Range("A1", last).EntireRow;
Excel.Range RngToCopy = RngToCopyOri.Resize[RngToCopyOri.Rows.Count + 1, RngToCopyOri.Columns.Count]; //because insert will add only 1 row, so the range would be one row larger
Excel.Range RngToInsert = xlWS.get_Range("A1", Type.Missing).EntireRow;
RngToInsert.Insert(Excel.XlInsertShiftDirection.xlShiftDown, RngToCopy.Copy(Type.Missing));
I tried to copy Range(A1,lowerleft cell) to its original location, but nothing changed.
I tried Range.resize, autofill, autoformat. All of them has sort of problems. I finally gave up using OLEDB to insert data. Instead, i used
worksheet.UsedRange.Item[rowNo,getColumnIndex(worksheet,columnTitle)]=value
private int getColumnIndex(Excel.Worksheet sheetname, string header) {
int index=0;
Excel.Range activeRange=sheetname.UsedRange;
for (int i = 1; i <= activeRange.Columns.Count; i++) {
if (header == (string)(activeRange.Item[1,i] as Excel.Range).Value) {
index = i;
}
}
if(index==0)
throw some exception you like;
return index;
}
The getColumnIndex function aims to locate the column in SELECT [column] from...
In this way, the format table will automatically expand to the range you input the value.

Excel Range in C# using interop.Excel. Range both empty and not empty?

I have a Excel sheet that looks like this.
|A1|B1| "BLANK" |D1|E1|F1|
I.e. only the first row is populated and the third column is blank. I parse this in C# using interop.excel in the following way:
Excel.Application exApp = OpenExcel();
String mySheet = #"C:\c#\rapport.xlsx";
Excel.Workbook wb = exApp.Workbooks.Open(mySheet);
Excel.Worksheet ws = wb.Sheets[1];
Excel.Range row = ws.Rows[1];
I create a new range only containing the non-empty cells in row 1 by
Excel.Range rng = row.SpecialCells(Excel.XlCellType.xlCellTypeConstants);
Now: if I use rng.Select all the non-empty cells in my sheet is selected. And if I use rng.Count it will return 5, which is the number of non-empty cells in my sheet. But still when I print cell by cell using:
Console.WriteLine(rng[1,i].Value.ToString());
It shows that rng[1, 1-2] contains "A1 and B1", this is nice. But
rng[1, 3] is empty or null. And rng[1, 4-7] contains D1-F1.
How is this possible?
My main goal is to store all non-empty values in a string array. But I can't select the right values because my range rng is both empty and non-empty is some weird way.
Try using:
ws.Columns.ClearFormats();
ws.Rows.ClearFormats();
to be sure your Range excludes formatted but empty cells.
Another try could be to get :
Excel.Range rng = row.SpecialCells(Excel.XlCellType.xlCellTypeConstants,Type.Missing);
This solved it! However I didn't use the .Item method to store the values as Rory suggested suggested. Thnx Rory!
string[] str = new String[rng.Count];
int i = 0;
foreach (Excel.Range cell in rng.Cells)
{
str[i] = cell.Value.ToString();
i++;
}
for (int j = 0; j < str.Length; j++)
Console.WriteLine(str[j]);

Read Excel Cell And Set Row Colour

I am using Com Interop and C#. I have to iterate through an Excel file looking for certain values in each of the rows (always in column 2). For some values I need to set the background colour of the row to red.
I am having trouble:
Reading the value in cell [i][2] for row i, and
Setting the background colour of this row.
Basically I am looking for something like this (which is the best I can find after much Googling):
// ws is the worksheet
for (int i = 1; i <= ws.Rows.Count; i++)
{
Range range = ws.Cells[i][2];
int count = Convert.ToInt32(range.Value2.ToString());
if (count >= 3)
{
Range chronic = ws.UsedRange.Rows[i];
chronic.EntireRow.Cells.Interior.Color = 0xFF0000;
}
}
Of course this doesn't work. I can't get past the first hurdle of just reading the cell. Any advice is appreciated.
Try this. The code assumes that the value in the column 2 cell is a number.
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
Missing noValue = Missing.Value;
Excel.Range conditionalCell;
foreach (Excel.Range usedRange in ws.UsedRange.Rows)
{
conditionalCell = usedRange.Cells[noValue, 2] as Excel.Range;
if (Convert.ToInt32(conditionalCell.Value2) >= 3)
{
usedRange.Cells.Interior.Color = Excel.XlRgbColor.rgbRed;
}
}

Categories