Insert cell comments in excel programmatically - c#

What's the better way to insert cell comments in excel 2007 files programmatically using c# and .net 3.5?

I just did exactly that but with MS Word (using Microsoft.Office.Interop.Word
range.Comments.Add ( range, ref _categoryMessage );
So, I would suggest using Microsoft.Office.Interop.Excel and the similar method.
Consider this from MSDN:
https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.range.addcomment
Also see this too

The accepted answer points in the right direction, but the correct syntax is:
Excel.Range cell;
cell.AddComment("My comment");

Excel._Worksheet oSheet =
(Microsoft.Office.Interop.Excel._Worksheet) excelWorkbook.ActiveSheet;
oSheet.Cells[2, 3].Cells.AddComment("Selam");

Have you tried using VSTO ? You can easily load an Excel document and manipulate it. To add a comment to a cell, load the file, activate the worksheet, then select the cell as a range and set the comment.

Related

Save specific pages of an Excel workbook

I have one workbook with three sheets. Each sheet got 3 pages. What I want to reach is: I want to save only the first page of each sheet.
I can only count those pages with
int numberOfPages = 0;
foreach(Excel.Worksheet sheet in excelWorkbook.Sheets)
{
numberOfPages += sheet.PageSetup.Pages.Count;
}
But I cant find a way how to save these pages. Is there a way?
Here is how to copy a worksheet:
Excel.Worksheet worksheet1 = ((Excel.Worksheet)Application.ActiveWorkbook.Worksheets[1]);
Excel.Worksheet worksheet3 = ((Excel.Worksheet)Application.ActiveWorkbook.Worksheets[3]);
worksheet1.Copy(worksheet3);
Hope that helps.
I'd suggest using the Macro Recorder in such cases (which is available in Excel). The required VBA code can generated automatically in the background for you. Most probably you will need to correct it because an auto-generated code is not well-optimized, but at least you will have an idea what properties and methods should be used to get the job done. See Create or delete a macro for more information.

Trying to select named Excel 2007 Table in existing Workbook and insert row

I have an Excel template which I am opening, which has a pie chart bound to a table (ListObject), and I want to insert rows into the table. There are plenty of examples of how to ADD a table into a worksheet, but I can't find anywhere that lets me select the table.
I have tried:
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
oWB = (Excel._Workbook)(oXL.Workbooks.Open(LastFile));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
Excel.ListObject ValList = oSheet.ListObjects("ValueData");
but that gives the compile error:
Non-invocable member 'Microsoft.Office.Interop.Excel._Worksheet.ListObjects' cannot be used like a method. (CS1955)
I have also tried doing it with a macro, then trying to convert the VB to C#, but still with no joy.
For information, I am relatively new to C# Excel automation, although I have done quite a lot in C# and have done Excel COM object work using other languages.
good morning,
mr.Reband is right, i think... maybee this is worth a look? as the error says, taken the MSDN link into account, i think, oSheet.ListObjects is a field or property, rather than a method. so, your invocation is not allowed. cheers!

how to format excel sheet according to property value of an object?

i need to create and download a excel sheet in c# asp.net. I used writing Range. because it is fast. but i need to format the excel sheet. According to a property(usercolor) of the user object, i need to color the row. but when writing to range how can i do that?
i am using this code to write
var startCell = (Range)sheet.Cells[2, 1];
var endCell = new object();
endCell = (Range)sheet.Cells[(usersList.Count + 2), noofcolums];
var writeRange = sheet.get_Range(startCell, endCell);
writeRange.Value2 = data;
data is a TwoDimensionalObject. it is created by user object.
As an additional comment: Do NOT use Excel in a server inevironment. It is slow and Excel might spawn error-windows at any time, causing a hang. This cannot be circumvented in a clean way - even Microsoft agrees and does not support office in a server model.
You might try epplus, a free reading / writing library for excel. It is fast , supports formatting and is way nicer to program than excel interop.
To color any Row in Excel
oRange.get_Range("A1","X1").Interior.Color = System.Drawing.ColorTranslator.ToWin32(Color.Orange);
Hope it helps
I could not find any way to use Object property to map with row color in range writing way. I need to write cell by cell. but it is very slow. So i used creating html file(html table) and convert it to excel document. it is not slow also. Thank you very much for all replies
please refer this Export HTML Table to Excel using ASP.NET

How to get the 'first' sheet in OOXML with C# and the SDK?

SO! :) Simple question -- it's probably been asked, but I could not find it.
I am retrieving data from an XLSX using the Open XML SDK and C#.
I want to get the "first" sheet (as in the first one you would see in Excel), but when I use...
WorkbookPart wbPart = workBook.WorkbookPart;
//Now let's find the dimension of the first worksheet
string sheetArea = wbPart.WorksheetParts.First().Worksheet.SheetDimension.Reference.Value;
Unfortunately, in a brand-new XLSX this pulled "Sheet3" instead of "Sheet1". I do not know the sheet name ahead of time nor can I force the user to submit a workbook with only one sheet or specify sheet name. My present requirements are to grab the first sheet.
Can someone please help? :)
EDIT: I figured it out! But I can't answer my own question for 7 hours, so...
I found this by digging through answers on this other SO question:
Open XML SDK 2.0 - how to update a cell in a spreadsheet?
In essence, a working example might be this :
(wbPart.GetPartById(wbPart.Workbook.Sheets.Elements<Sheet>().First().Id.Value) as WorksheetPart).Worksheet.SheetDimension.Reference.Value
As far as I know, something like:
Sheet firstSheet = wbPart.Workbook.Descendants<Sheet>().First();
Worksheet firstWorksheet = ((WorksheetPart)wbPart.GetPartById(firstSheet.Id)).Worksheet;
Should return the first worksheet. The workbook Sheet descendants should always be sorted based on the order they appear in the workbook, at least in my experience.
If you wish to get the first visible, use:
Sheet firstSheet = wbPart.Workbook.Descendants<Sheet>()
.First(s => s.State == SheetStateValues.Visible);

Copy cells in excel using C#

How to copy to specific row in target sheet?
I need to copy A1 to J10 from a sheet in one excel to location starting from A15 in second excel sheet. How can I achieve this in c#? In the below Copy method there seems to be no option to specify the location in target excel sheet.
ObjWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)ObjWorkBookTemp.Sheets[1];
ObjWorkSheet.Copy(Type.Missing, ObjWorkBookGeneral.Sheets[1]);
I think you are using the wrong method here... you want to use a Paste method not a copy method.
Try the Range.PasteSpecial method... should do the trick for you.
Something like this...
Excel.Range sourceRange = firstWorksheet.get_Range("A1", "J10");
Excel.Range destinationRange = secondWorksheet.get_Range("A15", "J25");
sourceRange.Copy(Type.Missing);
destinationRange.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteFormulas, Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);

Categories