I'm writing an application that opens an existing .xlsx file and writes to certain cells.
Some cells write correctly, where others just stay blank?
Any ideas?
This is a snippet of code
The same code for the cells that are and arent working, except that the index's changed
oSheet.Cells[3, 15] = "1"; // this doesnt write to the cell
oSheet.Cells[7, 7] = "1"; // this writes to the cell
All that i could think is that there is a formatting issue in the Excel file?
I've been working in Excel for years and find quirks like this all the time. If you are in .NET 4.0 try this:
using Excel = Microsoft.Office.Interop.Excel
//Other Class code
var range = oSheet.Cells[3, 15];
range.Value2 = "1";
Otherwise, try this:
using Excel = Microsoft.Office.Interop.Excel
//Other Class code
Excel.Range range = (Excel.Rang)oSheet.Cells[3, 15];
range.Value2 = "1";
Value2 seems to work more consistently, so I generally recommend using it.
Cheers!
Anthony was right, I had my columns and rows switched.
Related
There was a need to make conditional formatting of a cell with a histogram. Used ClosedXML but it didn't give the desired result.
It is necessary to solve the problem with both the gradient and negative numbers. Has anyone encountered something similar? I am attaching the code.
form_sheet.Cell("D37")
.AddConditionalFormat()
.DataBar(XLColor.FromArgb(68, 114, 196), false)
.Minimum(XLCFContentType.Number, -3)
.Maximum(XLCFContentType.Number, 3);
Ready to consider alternative solutions not through ClosedXML. The program will generate several dozen reports. All histograms will be in the same cells, so I also considered vbs, but I don’t have enough experience to write such a script that would change styles immediately for a bunch of documents.
Bit late to answer, might be helpful for others..
I also gone through the same gradient issue. Currently using ClosedXML it is not possible to generate conditional DataBar with solid color.
What am doing to resolve my issue is to generate the Excel as of now with ClosedXML and re-open the Excel again in Interop and add the DataBar in the respective cells using Interop.Excel.
I haven't fully rewrote the code using interop because performance wise we can't fully rely on interop as compared to ClosedXML, atleast for me.
Sample code for adding Databar using Interop
var excel = new Microsoft.Office.Interop.Excel.Application();
var workBooks = excel.Workbooks;
var workBook = workBooks.Add();
var workSheet = (Microsoft.Office.Interop.Excel.Worksheet)excel.ActiveSheet;
workSheet.Cells[1, "A"] = 10;
Microsoft.Office.Interop.Excel.Range range1 = workSheet.Cells[1, 1];
Microsoft.Office.Interop.Excel.Databar bar = (Microsoft.Office.Interop.Excel.Databar)range1.FormatConditions.AddDatabar();
bar.BarFillType = Microsoft.Office.Interop.Excel.XlDataBarFillType.xlDataBarFillSolid;
Thanks.
I've read that Microsoft.Office.Interop.Excel would be the easiest way to replace formulas by their values in Excel but it requires to have Office installed. Since I will need to deploy on a Windows Server (2008 or 2012), I am looking for the best and/or simplest way to accomplish that using EPPlus.
Adding formulas is well documented, e.g.
currentWorksheet.Cells["C4"].Formula = "SUM(C2:C3)";
But I cannot find any example of replacing entire worksheets of formulas by their equivalent values. Basically the Copy followed by the Paste Special option in Excel.
I dont think there is any kind of function built into Epplus that will do that for you en masse. But you can take advantage of the fact that the Cells collection of the Worksheet only contains entries for cells with content. So something like this should not be too painful performance-wise:
currentWorksheet.Cells["C2"].Value = 5;
currentWorksheet.Cells["C3"].Value = 15;
currentWorksheet.Cells["C4"].Formula = "SUM(C2:C3)";
currentWorksheet.Cells["D2"].Value = 15;
currentWorksheet.Cells["D3"].Value = 25;
currentWorksheet.Cells["D4"].Formula = "SUM(D2:D3)";
//Calculate the formulas and the overwrite them with their values
currentWorksheet.Cells.Calculate();
foreach (var cell in currentWorksheet.Cells.Where(cell => cell.Formula != null))
cell.Value = cell.Value;
I know this is 3 years old, but if you're reading this now, EPPlus now has the function .ClearFormulas() that does just that, simply do
yourSheetHere.Calculate();
yourSheetHere.ClearFormulas();
and you're good to go.
I'm using Epplus to put a formula into a cell. If I put this formula manually into the Excel cell it works:
=SUM(E4;G4)
But when I put in code with Epplus it doesn't work:
xls.ActiveSheet.Cells(8, 4).Formula = "SUM(E4;G4)"
Is there something special needed when I SUM two cells?
If I do the same with a range of cells it works, but with specific cells not.
This works (Range):
xls.ActiveSheet.Cells(8, 4).Formula = "SUM(E4:G4)"
Try changing this
xls.ActiveSheet.Cells(8, 4).Formula = "SUM(E4;G4)";
into
xls.ActiveSheet.Cells(8, 4).Formula = "SUM(E4,G4)";
Somehow using ; in a formula messes things up. Could be a bug...
I'm trying to color a particular range of cells in exsisting excel using C# code.
Below is the code I tried but it throws an exception.Let me know how to proceed.
excelWorkSheet4.get_Range("AC", "AE").Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
I got the answer for this one below code works for any particular range after "Z"
excelWorkSheet4.get_Range("AC:AC", "AE:AE").Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
What is the C# equilivant to this VB6 to setting of the active cell?
ActiveSheet.Range("L1").Select
Here's a sample piece of code:
Excel.Worksheet sht = (Excel.Worksheet)ActiveSheet;
sht.Cells[3, 3] = "HELLO";
You can also capture ranges:
Excel.Range rng = (Excel.Range)sht.Cells[3, 3];
I believe to you just the Select method as before to select a range, although I haven't tested this.
rng.Select();
You can obviously streamline this and chain these statements together, with the right casting. I don't want to hazard a guess here as I've not got a VSTO project open in from of me.
EDIT
You should also be able to get a range from the sheet using get_Range:
rng = sht.get_Range("A1", Type.Missing);
VSTO tends to return Objects most of the time, necessitating casts, but get_Range is an exception. Someone might be able to correct me as I am not a big user of VSTO (still VBA die-hard when it comes to Excel).