Hi,
I am generating excel sheet from c# code using DocumentFormat.OpenXml.dll. I have generated excel sheet. now i want to find any specific element of Excel sheet and make it bold.
How to find any element of Excel sheet from C# code ?
Thanks.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Eugene MontesPosted Mar 31, 2015, 3:29 AM
1. get the WorksheetPart of the required worksheet, e.g.:
Workbook workBook = spreadSheet.WorkbookPart.Workbook;
WorksheetPart worksheetPart = workBook.WorkbookPart.WorksheetParts[0];
2. locate its SheetData element, e.g.:
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild
3. locate the required Row from SheetData, e.g.:
Row row = sheetData.Elements
4. locate the required Cell from Row, e.g.:
Cell cell = row.Elements
Now in order to bold it you would need to manipulate with WorkbookStylesPart, for example see here. Also the above approach retrieves a specific cell based on its row and column index, but in case you want to find a specific cell that contains some text value then you would have to get SharedStringTablePart and search through its SharedStringItem in order to locate the required text and its cell reference.
All in all you must have realized by now that OpenXML SDK has some learning curve and it requires from you a knowledge of Office Open XML format.
So as a workaround I would advise you to check out this C# component for Excel workbooks. I find it to be much more intuitive, it can straightforwardly read excel files in C#, here is how you would retrieve the required cell:
Also in case you are looking for some specific text you would just use a "worksheet.Cells.FindText(...)" method. Also to bold this cell you just need:
Sharon WhitePosted Apr 23, 2013, 4:52 AM
Following is my solution to find a number from Excel and set it as bold and I use a .NET Excel component.
Workbook workbook = new Workbook();
workbook.LoadFromFile("test.xlsx", ExcelVersion.Version2010);
//find and highlight excel data
Worksheet sheet = workbook.Worksheets[0];
foreach (CellRange range in sheet.FindAllNumber(3501, true))
{
range.Style.Font.IsBold = true;
}
Hope that my solution will be helpful for you!