Copy Excel cell comments in a specified range - c#

What is the way to copy all cell comments (on right click - Insert Comments) in a specified range?
Range r1 = (Range)ws1.get_Range("A1", "C10");
Range r2 = (Range)ws2.get_Range("A1", "C10");
r2.Value = r1.Value; // copies cell values and ignores comments
I know that r1.Copy(r2); would copy values and comments, but it shows unnecessary Excel dialogs due to validation issues and therefore I cannot use it.

There's a AddComment method for Range. Unfortunately, it cannot be applied to a range of cells. I guess they assumed: why would you want the same comment written multiple times? So you'll have to loop:
for (int r = 1; r <= r1.Rows.Count; r++)
{
for (int c = 1; c <= r1.Columns.Count; c++)
{
r2[r, c].AddComment(r1.Comment);
}
}

Related

Is it possible to select all cells where a conditional format is true?

I want to check an excel worksheet if any of the cells have a conditional format that true. I have multiple formats that format cells different colors, but I just want to be able to see if any of the conditions are true (don't care which condition). This example isn't working, but I want to know if I'm on the right track or even possible to do what I'm asking.
var lastCell = Globals.ThisAddIn.Application.ActiveCell.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing);
var firstCell = ((Worksheet)Globals.ThisAddIn.Application.ActiveSheet).Cells[2,1];
var range = ((Worksheet)Globals.ThisAddIn.Application.ActiveSheet).Range[firstCell, lastCell]
var r = range.SpecialCells(XlCellType.xlCellTypeAllFormatConditions, true);
As the FormatConditions collection of a range applies to an entire range, I don't believe there's a mechanism to determine the value for each cell in the range. I think your best bet would be to test the Formula in the cell of interest. I'm sure this is not what you wanted to hear.
Get the used range with this answer. Of course, replacing xlWorkSheet with ((Worksheet)Globals.ThisAddIn.Application.ActiveSheet).
Then, assuming you set it to range, loop through the range...
for (int r = 1; r <= range.Rows.Count; r++)
{
for (int c = 1; c <= range.Columns.Count; c++)
{
...replace the formula/value temporarily with something like (preface: I almost guarantee this code will not work)...
string formula = range[r, c].Formula; //or Value, check HasFormula
for (int i = 1; i <= range.FormatConditions.Count; i++)
{
range[r, c].Formula = range.FormatConditions[i].Formula1; // or Formula2 (?)
}
bool condition = range[r, c].Value;
// do work based on the condition
range[r, c].Formula = formula;
... and close the loops.
}
}

How to take part in excel files

Hello sorry for my english.
I have to select a row of a excel file, put any new data and save them.
In the end I see that the excel file is always larger than before although the data are not increased but it looks to be created of the blank columns to the right.
I think this because when I execute the following statement
var wb = openWorkBook(filename);
var ws = wb.Worksheet("CNF");
IXLRow row = ws.Row(device.Ordinal - 1 + FirstRow);
for (int j = 0; j < MAXCOLS; ++j)
{
IXLCell cell = row.Cell(j + FirstCol);
...}
as range goes from A1 to XFD1048576.
Although after I take the line of my interest and cycle of 100 columns when I go
wb.Save();
the file increases.
So I ask you if you have a method to take only a part of a file then for example take already suffered from a limited number of columns, starting from education var ws = wb.Worksheet("CNF");.
Thank you

Faster way to find empty cells and Cells containing specific string in Excel 2013

While creating and customizing Excel file using Interop and Office 2013 installed, gives me somehow extremely slow results (more than 5 minutes).
In fact, the same thing works very well on Excel 2010 interop (just 50 seconds, exactly same process). (Code snippet below)
It would be nice to know if there is a faster way to do this. I know there are different libraries to do this but I would like to stick to Interop since everything is already in the same.
I am creating Excel file first then check if there are any empty cells or cells containing a specific string and change color of those cells.
To create Excel, I used Object array and parse it that is really faster. Main thing which is pulling it down is to search and change cell color.
// Check for empty cell and make interior silver color
for (int row = 0; row < rowNo; row++)
{
for (int col = 0; col < columnNo; col++)
{
if (string.IsNullOrEmpty(objData[row, col].ToString()))
{
// Access that cell in Excel now and change interior color
Range cell = (Range)activeSheet.Cells[row + 2, col + 1];
cell.Interior.Color = System.Drawing.Color.Silver;
}
}
}
// Check for cells contains "column header string#"
for (int col = 16; col < columnNo; col++)
{
// Get column header - only once and use it for all rows in the same column
string cellValue = activeSheet.Cells[1, col + 1].Value2.ToString();
for (int row = 0; row < rowNo; row++)
{
string value = objData[row, col].ToString();
if (string.IsNullOrEmpty(value) || value.Contains(cellValue+"#"))
{
Range cell = (Range)activeSheet.Cells[row + 2, col + 1];
cell.Interior.Color = System.Drawing.Color.Silver;
cell.Font.Color = System.Drawing.Color.Red;
}
}
}
I'd look into selecting the whole range and setting conditional formatting so blank cells are the color you want. Sorry don't have any code for you, it's been awhile since I've played in that realm.

Optimized way of adding multiple hyperlinks in excel file with C#

I wanted to ask if there is some practical way of adding multiple hyperlinks in excel worksheet with C# ..? I want to generate a list of websites and anchor hyperlinks to them, so the user could click such hyperlink and get to that website.
So far I have come with simple nested for statement, which loops through every cell in a given excel range and adds hyperlink to that cell:
for (int i = 0; i < _range.Rows.Count; i++)
{
Microsoft.Office.Interop.Excel.Range row = _range.Rows[i];
for (int j = 0; j < row.Cells.Count; j++)
{
Microsoft.Office.Interop.Excel.Range cell = row.Cells[j];
cell.Hyperlinks.Add(cell, adresses[i, j], _optionalValue, _optionalValue, _optionalValue);
}
}
The code is working as intended, but it is Extremely slow due to thousands of calls of the Hyperlinks.Add method.
One thing that intrigues me is that the method set_Value from Office.Interop.Excel can add thousands of strings with one simple call, but there is no similar method for adding hyperlinks (Hyperlinks.Add can add just one hyperlink).
So my question is, is there some way to optimize adding hyperlinks to excel file in C# when you need to add a large number of hyperlinks...?
Any help would be apreciated.
I am using VS2010 and MS Excel 2010.
I have the very same problems (adding 300 hyperlinks via Range.Hyperlinks.Add takes approx. 2 min).
The runtime issue is because of the many Range-Instances.
Solution:
Use a single range instance and add Hyperlinks with the "=HYPERLINK(target, [friendlyName])" Excel-Formula.
Example:
List<string> urlsList = new List<string>();
urlsList.Add("http://www.gin.de");
// ^^ n times ...
// create shaped array with content
object[,] content = new object [urlsList.Count, 1];
foreach(string url in urlsList)
{
content[i, 1] = string.Format("=HYPERLINK(\"{0}\")", url);
}
// get Range
string rangeDescription = string.Format("A1:A{0}", urlsList.Count+1) // excel indexes start by 1
Xl.Range xlRange = worksheet.Range[rangeDescription, XlTools.missing];
// set value finally
xlRange.Value2 = content;
... takes just 1 sec ...

ExtremeML Negative Exponent Export Issue

In C# I have a DataTable which has valid values, some of which are in exponential format. I export this DataTable to an xlsx file using ExtremeML (which I believe is based on OpenXML) and the following code:
if (!Directory.Exists(savePath.Text))
Directory.CreateDirectory(savePath.Text);
using (var package = SpreadsheetDocumentWrapper.Create(savePath.Text + fileName + ".xlsx"))
{
for (int i = 0; i < dataset.Tables.Count; i++)
{
// declares worksheet
var part = package.WorkbookPart.WorksheetParts.Add(dataset.Tables[i].TableName.ToString()); // second worksheet filled by second collection
bool first = true;
int position = 0;
for (int row = 0; row < dataset.Tables[i].Rows.Count; row++)
{
for (int col = 0; col < dataset.Tables[i].Columns.Count; col++)
{
// adds to file
if (first)
part.Worksheet.SetCellValue(new GridReference(row, col), dataset.Tables[i].Columns[col].ColumnName.ToString());
else
part.Worksheet.SetCellValue(new GridReference(row, col), dataset.Tables[i].Rows[position][col]);
}
if (first)
{
first = false;
}
else
position++;
}
}
}
The resulting excel file is almost acceptable, except that negative exponential negative numbers
(for example -7.45E-05) treat the negative sign as a digit (previous example becomes 0.000-745). The columns are all set to be doubles in the DataTable, so the excel file should be formatting them as such.
Any ideas as to the cause of this problem?
(Also, I know the code is a touch painful to grok- the code is not mine, and I haven't gotten around to cleaning up the program.)
Your sample code seems fine.
This looks like a bug in the way way ExtremeML converts doubles. As you say, it only appears to affect negative exponential negative numbers.
I am currently debugging it and creating additional unit tests to cover these scenarios, after which I will commit the updated code to CodePlex and post an update here.
Edit: This bug has now been fixed. You'll need to download and build the ExtremeML source code from CodePlex, as the runtime release is not currently up-to-date.

Categories

Resources