How to find same values from different cell in Excel file using C# - c#-4.0

Microsoft.Office.Interop.Excel.Range Range_Number_Phone;
Range_Number_Phone = xlWorkSheet.UsedRange.Find("Phone");
This code finds first values only.it can't find another word.How is it possible?Please help me.

using Free Spire.XLS dll from NuGet Package Manager we can find same values from a different cell.
Spire.Xls.Workbook workbook = new Spire.Xls.Workbook();
workbook.LoadFromFile(#"D:\\input.xlsx", ExcelVersion.Version2010);
//find and highlight excel data
Spire.Xls.Worksheet sheet = workbook.Worksheets[0];
foreach (CellRange range in sheet.FindAllString("Customer PO Number", false, false))
{
range.Style.Color = Color.LawnGreen;
}
//save and launch the project
workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);
System.Diagnostics.Process.Start("result.xlsx");

Related

Why is my Excel file empty after writing content to cells using Microsoft.Office.Interop.Excel?

I'm using Microsoft.Office.Interop.Excel to write an Excel file. The code below is not working:
var excel = new Microsoft.Office.Interop.Excel.Application();
var workbook = excel.Workbooks.Add(Type.Missing);
var worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
worksheet.Name = "sheet1";
worksheet.Cells[1,1] = "top left";
worksheet.Cells[1,2] = "top right";
worksheet.Cells[2,1] = "bottom left";
worksheet.Cells[2,2] = "bottom right";
workbook.SaveAs("temp.xlsx");
workbook.Close();
excel.Quit();
It produces an Excel file but it is empty. I'm expecting to see the text "top left", "top right",... in the first 2 by 2 cells. But I see nothing.
Why is it not writing content to my worksheet?
You can find my code at github: https://github.com/gibran-shah/Image2Excel
Perhaps this link might help you: http://csharp.net-informations.com/excel/csharp-create-excel.htm
According to the link above, try using:
var worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.get_Item(1);
To troubleshoot further, I would choose some other name for the sheet, and check that the name is actually being set by opening the workbook and observing the name in the sheet's tab. This would ensure you are indeed adding data to the sheet that you think you are.

Is it possible to use ClosedXML to read Excel file to Ax 2012?

I have used ClosedXML to create an excel file with 3 worksheets from AX 2012 R3.
I'm wondering if it is possible to use ClosedXML to read the excel file back in to AX?
Yes, that is possible.
Here is the sample code:
ClosedXML.Excel.XLWorkbook workbook;
ClosedXML.Excel.IXLWorksheet worksheet;
ClosedXML.Excel.IXLCells cellsUsed;
ClosedXML.Excel.IXLCell cell;
System.Collections.IEnumerator enumerator;
str value;
;
try
{
workbook = new ClosedXML.Excel.XLWorkbook(#"YourTestFile.xlsx");
worksheet = workbook.Worksheet("SomeEpicSheetName");
cellsUsed = worksheet.CellsUsed();
enumerator = cellsUsed.GetEnumerator();
while (enumerator.MoveNext())
{
cell = enumerator.get_Current();
value = System.Convert::ToString(cell.get_Value());
info(value);
}
}
catch
{
error(AifUtil::getClrErrorMessage());
}
This code creates a message in the InfoLog for each cell filled with a value. You can use it as a start in any new job after changing the used path and sheet name.
This is my example sheet:
The output:

Error accessing Names in Excel using Interop

I am programming an application in C# (Visual Studio 2015) and I need to update an .xlsm file.
This file has many formulas, over 1200 names and vba code.
I am using the Interop library and I am able to update some cells and get the relative updated formulas but i have some problem with the Names defined in the Excel.
The program recognizes the names in the Names collection but doesnt let me access some of the names.
When I try to access the value of the cell using its name it produces an exception.
I dont understand why i can access some of the names and others no.
Besides, in the excel, I can see the Name in the combo but when I select it, the cursor doesn't position over the cell.
In my program I could avoid this problem accessing the cells using the reference instead of the Name, but the vba in the excel uses the names and if i open the file from my app it doesnt work.
I am using this code:
excelApplication = new Microsoft.Office.Interop.Excel.Application();
excelApplication.ScreenUpdating = true;
excelApplication.Visible = true;
excelApplication.DisplayAlerts = false;
excelWorkbook = excelApplication.Workbooks.Open(txtFicheroEntrada.Text);
wsDatos = excelWorkbook.Worksheets[1];
wsDatos.Select();
foreach(Microsoft.Office.Interop.Excel.Name v in excelWorkbook.Names)
{
string NombreVar = v.Name;
//here i found the name BobinadoAT correctly. It exists
if (NombreVar == "BobinadoAT" ){ Console.WriteLine(NombreVar); }
}
if (wsDatos.Range["BobinadoAT"] != null) //but here this produces an exception
{
string valorcelda = wsDatos.Range["BobinadoAT"].Value.ToString();
}
¿does anyone work with many excel Names?
¿Am I accessing the names incorrectly?

openxml sdk excel total sums

i am having difficulties with open xml sdk:
i am trying to generate excel file with several columns that have numbers and i want to have total sum at the end
i have tried to Generate Table Definition Part Content and inside define every column (id, name etC). If column has true for TotalColumn, it adds code (rough example)
var column = new TableColumn{
id = 1,
name = "example",
TotalsRowFunction = TotalsRowFunctionValues.Sum,
TotalsRowFormula = new TotalsRowFormula("=SUBTOTAL(109;[" + rowName + "])")
};
I can't get it to work, when i open excel it reports error, but it doesn't explicitly says what the problem is... I tried with microsoft validator but can't figure anything out...
I'd appreciate any help / example code since i can't google anything out
EDIT:
i use this at the end:
workbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;
workbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;
Why not use a cell formula?
E.g.
cell.DataType = new EnumValue<CellValues>(CellValues.Number);
cell.CellFormula = new CellFormula(string.Format("=SUBTOTAL({0};[{1}])", "109", rowName));
//This will force a full recalculation of all the cells
workbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;
workbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;
I ended using EPPlus for this as it seems to be working simple and efficient

How to get the value in an Excel dropdown using C#

I am looking for code to open and read an Excel file, any version of Excel, including 2010. One of my columns has a dropdown in it. I need to get the value of the selected item in the dropdown. I would eventually want to populate these values into a business object.
If anyone has some code to share please let me know.
I am using C# and Visual Studio 2010.
Thanks.
I know the VBA for both the ActiveX combo and the forms dropdown, and based on that, I can give you some very inexpert notes for c# for the forms dropdown, the combo eludes me as yet.
Working with notes from: http://support.microsoft.com/kb/302084
//Get a new workbook.
oWB = (Excel._Workbook)(oXL.Workbooks.Open("C:\\Docs\\Book1.xls"));
//3rd Sheet
oSheet = (Excel._Worksheet)oWB.Sheets.get_Item(3);
//This will return an index number
var i = oSheet.Shapes.Item("Drop Down 1").ControlFormat.Value;
//This will return the fill range
var r = oSheet.Shapes.Item("Drop Down 1").ControlFormat.ListFillRange;
oRng = oSheet.get_Range(r);
//This will return the value of the dropdown, based on the index
//and fillrange
var a =oRng.get_Item(i).Value;
//Just to check
textBox1.Text = a;
This may help with an ActiveX combo, but I have only half got it to work:
using MSForm = Microsoft.Vbe.Interop.Forms;
<...>
Excel.OLEObject cbOLEObj = (Excel.OLEObject)workSheet.OLEObjects("ComboBox1");
MSForm.ComboBox ComboBox1 = (MsForm.ComboBox) cbOLEObj.Object;
Console.WriteLine(ComboBox1.Text);
From: http://www.eggheadcafe.com/community/aspnet/66/10117559/excel-get-value-from-a-combobox.aspx

Resources