Excel auto filter - c#

I have a problem while filtering the Excel column by year. I tried the below code but didn't get any success, could anybody help me or offer some suggestions on how I can find the year in excel filters.
For example, if I enter '12/30/2016', it will search successfully but I would like to search by year (e.g. '2016').
Excel.Range range = WorksheetSource.UsedRange;
range.AutoFilter(29, tByear.Text, XlAutoFilterOperator.xlFilterValues,test4[0,0], true);
range.AutoFilter(29, tByear.Text, Excel.XlAutoFilterOperator.xlOr, tByear.Text+ "*", true);

try this
Excel.Range currentFind = null;
xlworkSheet.Cells.Find(date,Type.Missing,Microsoft.Office.Interop.Excel.XlFindLookIn.xlValues,Microsoft.Office.Interop.Excel.XlLookAt.xlWhole,Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows,Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, false,Type.Missing, Type.Missing)

Related

How do I get specific text from excel worksheet using C#?

I'm wanting to find multiple cells where a string is present. So usually a cell will have a int and the next cell will have a string. Is there anyway to find the string and print the value(int) to the console. So column A would have int and column B would be string. If the int doesn't have a string then it doesn't get printed. I've tried a couple methods on here but can't find what I'm looking for.
string fileExcel = #"C:\test.xlsx";
Application x1Application = new Application();
Workbook x1Workbook = x1Application.Workbooks.Open(fileExcel);
Worksheet x1Worksheet = x1Workbook.ActiveSheet;
var x1Range = (Excel.Range)x1Worksheet.Columns["A:B"];
Office interop isn't the most intuitive API, so you will need to be able to experiment quickly in a test environment to see what works and what doesn't work. Also, there are a lot of examples on MSDN for Office interop, and you will likely need to browse many examples to piece things together. The code below is mostly taken from How to: Programmatically Search for Text in Worksheet Ranges
In the test.xlsx file I have the following data
A
B
1
alpha
2
bravo
3
charlie
string fileExcel = #"C:\test.xlsx";
var application = new Excel.Application();
var workbook = application.Workbooks.Open(fileExcel);
var worksheet = workbook.ActiveSheet;
var range = (Excel.Range)worksheet.Columns["B:B"];
var foundRange = range.Find("bravo",
Type.Missing,
Excel.XlFindLookIn.xlValues,
Excel.XlLookAt.xlPart,
Excel.XlSearchOrder.xlByRows,
Excel.XlSearchDirection.xlNext,
false,
Type.Missing,
Type.Missing);
if (foundRange != null)
{
var numberRange = application.get_Range($"A{foundRange.Row}");
Console.WriteLine(foundRange.Value);
Console.WriteLine(numberRange.Value);
}
The above code should print
bravo
2
Also note, that the above code is very bare bones just to help you get started. One of the reasons that the Office interop is difficult to work with is because things are so dynamic. Likely you will need a lot more null checks and type safety checks to make sure things are stable.

Get the last cell with a value in a selection Excel C#

I want to locate the last cell with a value from the selection made by a user in Excel.
When running the code below the lastCell variable returns the last cell in the sheet instead of the last cell in the selection.
I have no idea why this is happening as I'm telling the lastCell to use my selection.
Any information would be appreciated.
My code is as follows.
Excel.Range selectedRange = excelApp.Selection;
Excel.Worksheet ws = excelApp.ActiveWorkbook.ActiveSheet;
Excel.Range lastCell = selectedRange.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
var newSelectedRange = ws.Cells.Range[selectedRange.Item[1, 1], lastCell];

C#: Finding exact string in excel sheet doesn't work with xlWhole

In my excel sheet I'm looking with my program for a key word like "Tescase". The parameter xlWhole dosen't find any range, but xlPart finds the key word. The problem with xlPart ist that it finds all the words in my excel sheet that contains the word "Testcase". But I would like to search only for the exact key word "Testcase".
Hear is my code:
var app = new Application();
var workbook = app.Workbooks.Open(pExcelFile);
var worksheet = (Worksheet)workbook.Worksheets.get_Item(1);
Range range = worksheet.UsedRange;
Range findTestcaseColumn = range.Find(What: "Testcase", LookIn: XlFindLookIn.xlValues, LookAt: XlLookAt.xlWhole);
if (findTestcaseColumn != null ) {
// found
} else {
//not found
}
But xlPart finds the key word "Testcase":
Range findTestcaseColumn = range.Find(What: "Testcase", LookIn: XlFindLookIn.xlValues, LookAt: XlLookAt.xlPart);
Thanks for suggetions.
Same problem here from VBA inside Excel: I fill a cell with a text, then come back later with .find to add something in the cell next to it. With xlPart I find the cell, with xlWhole it fails. Very strange behaviour. Resetting the style of the search column back to standard is a solution, but that's insane of course. It should ignore the style or have an option to do so.
Please join me giving feedback to Microsoft.
https://learn.microsoft.com/nl-nl/office/vba/api/excel.range.find

C#/Excel: Recover Number Error in Excel

I have a prob lem with excel and c#. When I set in the cell A1 a number like 123456789.
In all column A when the format is Number, next code is working without problems:
Workbook workbook = new Workbook(stream);
Worksheet worksheet = workbook.Worksheets[0];
var count = worksheet.Cells.Count; // count is 1
If I have column A design in format Text, converts that cell as '123456789 and then the code is wrong and returns me count=126 and when I query the cells all cells from 2 to 126 are empty or null and A1 as '1234567890
Why is happening that behavior?
Try out something like
Excel.Range range;
range = worksheet.UsedRange;
var count = range.Count;
It's a problem othersalready got.
Source : Interop Excel UsedRange Rows Count incorrect

Get the last cell (column, row) of a Excel range object

I have a Microsoft.Office.Interop.Excel.Range object and want to estimate the end-coordinates of that range ie (last column, last row).
There are no properties like LastCol, LastRow. The only properties are Column and Row, which specify the first cell. But how can I get the last cell of the range?
there is two way to do this :
Using Worksheet.UsedRange to determine the range. This will give you a range like A1:F10, or use Worksheet.UsedRange.SpecialCells(IExcel.XlCellType.xlCellTypeLastCell) to obtain F10.
With Range.End[IExcel.XlDirection] property, it returns the last continuous no-empty cell in the specified direction. Note that if the Range is empty, it will return the first no-empty cell or last cell(when excel reach its boundaries) in the given direction. Ex: the whole column A is empty, Range["A1"].End[IExcel.XlDirection.xlDown] will be A65535(last row in excel 97-2003, A1048576 for excel 2007)
//Just In case if you are wondering what IExcel is
using IExcel = Microsoft.Office.Interop.Excel;
This should get the first first and last cell of the range:
Initiate Excel.
Open workbook.
Select your range, in this case I got the used range of the active sheet.
Call the get_address method of the range.
Split the result of get_address on the colon.
The first value of the array resulting from the split will have the beginning cell.
The second value of the array resulting from the split will have the ending cell.
Replace the dollar signs with nothing and you will have the beginning and ending cells for the range.
Microsoft.Office.Interop.Excel.Application excel = new Application();
Microsoft.Office.Interop.Excel.Workbook workBook =
excel.Workbooks.Open(fileLocation);
Microsoft.Office.Interop.Excel.Worksheet sheet = workBook.ActiveSheet;
Microsoft.Office.Interop.Excel.Range range = sheet.UsedRange;
string address = range.get_Address();
string[] cells = address.Split(new char[] {':'});
string beginCell = cells[0].Replace("$", "");
string endCell = cells[1].Replace("$", "");
workBook.Close(true);
excel.Quit();
If you want the last column and last row relative to the beginning of the range you can do the following:
int lastColumn = range.Columns.Count;
int lastRow = range.Rows.Count;
Using Excel 2013, we've had lots of cases where UsedRange and xlCellTypeLastCell simply gave horribly wrong values.
For example, we'd have a worksheet containing just 7 columns, and sometimes these two functions would tell our C# code that there were 16,000+ columns of data.
The only method I found which truly worked out the bottom-right cell containing data was to use the tips in this suggestion:
sheet.Cells.Find()

Categories

Resources