C# add formula in a cell programatically - c#

I'm working with Visual Studio 9 and I'm using Microsoft.Office.Interop.Excel to handle Excel files from a program
To add a formula in a cell I use
xlWorkSheet.Cells[cwrite, j + 2].Formula = " =SUM(B" + cwrite.ToString() + "; 3)";
Note the space between " and =
In This case, the formula displayed in the right cell, but is not executed, due to space
However, if I suppress the space
xlWorkSheet.Cells[cwrite, j + 2].Formula = "=SUM(B" + cwrite.ToString() + "; 3)";
I got the error Error: Excepción de HRESULT: 0x800A03EC Line: System.Dynamic
Any Clue??
Thanks
David

Related

Why is an # symbol after = symbol in excel formula with EPPLUS?

This is how the formula appears in excel:
I want to use Forecast formula in Excel with EPPLUS from c#. The formula in the code is correct but in Excel appears =#FORECAST(params).
ExcelRange targetDate = sheet.Cells[listItems + 2, 2];
ExcelRange values = sheet.Cells[2, 3, listItems+1, 3];
ExcelRange timeLine = sheet.Cells[2, 2, listItems+1, 2];
sheet.Cells[8, 4].Formula = "=FORECAST.ETS(" + targetDate + "," + values + "," + timeLine + ",1,1)";
sheet.Cells[8, 4].Calculate();
I want to trim the # from the formula in the excel file, like this:
=FORECAST.ETS(B8,C2:C7,B2:B7,1,1)
In code you don't need to put the '=' character
Just use:
sheet.Cells[8, 4].Formula = "FORECAST.ETS(" + targetDate + "," + values + "," + timeLine + ",1,1)";
Similar Problem
I'm having a similar problem with a different formula:
worksheet.Cells[y, x].Formula = "SUMME(C5:C35)";
gave me =#SUMME(C5:C35) within Excel.
Instead of =SUMME(C5:C35).
Solution
It seems that EPPLUS has problems with the German formula. The problem disappears when I use:
worksheet.Cells[y, x].Formula = "SUM(C5:C35)";
My generated Excel now looks like this: =SUMME(C5:C35) (which is what I need)
I know it's not the answer to the exact problem mentioned above. But I thought it might help someone. And maybe serves as a hint to the original question.

c# Getting a row in an excel file throws TargetInvocationException

I am making a program to manage different projects for a company, the idea is that you put some info and it writes it into an excel file.
The excel files are names ("FINANZAS - ") + Current Year
The idea is that it detects the current year with DateTime and if the excel file for that year doesnt exists it creates it. This is my code:
MyApp = new Excel.Application();
MyApp.Visible = false;
if (!File.Exists("FINANZAS - " + DateTime.Now.Year.ToString() + ".xlsx")) { File.Copy(rutaPlantilla, "FINANZAS ARAINCO - " + DateTime.Now.Year.ToString() + ".xlsx"); }
MyBook = MyApp.Workbooks.Open(#"C:\Users\Sebastian\Documents\Visual Studio 2015\Projects\Finanzas ARAINCO\Finanzas ARAINCO\bin\Debug\FINANZAS ARAINCO - " + DateTime.Now.Year.ToString() + ".xlsx");
MySheet = (Excel.Worksheet)MyBook.Sheets[1];
lastRow = MySheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row + 1;
The problem comes when I try changing the date to test it with other years, I tried switching it to 2018 for testing but it throws me a TargetInvocationException on the last row (the one that, ironically, I use to detect the last row on excel)
I dont know if this is caused by something on the excel part of the code or if it has something to do with changing the date manually to 2018

Trying to update a text file

I'm trying to replace a certain line in a .txt file when I click the Update Button
This is what my program looks like
http://i.imgur.com/HKu4bGo.png
This is my code so far
string[] arrLine = File.ReadAllLines("Z:/Daniel/SortedAccounts.txt");
arrLine[accountComboBox.SelectedIndex] = "#1#" + firstNameInfoBox.Text + "#2#" + lastNameInfoBox.Text + "#3#" + emailInfoBox.Text + "#4#" + phoneNumberInfoBox.Text + "#5#EMAIL#6#";
File.WriteAllLines("Z:/Daniel/SortedAccounts.txt", arrLine);
This is what's inside SortedAccounts.txt
#1#Bob#2#Smith#3#Bob#Smith.com#4#5551234567#5#EMAIL#6#
#1#Dan#2#Lastyy#3#Daniel#Lastyy.com#4#5551234567#5#EMAIL#6#
The ComboBox is in the order as the Txt File.
So I get the same Index as the selected item in the ComboBox. And then I want to delete that line and then add a new line that same txt file with the updated information.
My code isn't doing this for some reason though and I can't figure it out
Try this out using List to easily remove an entry at a certain index. Don't forget to reload the combobox data source when the file is updated to avoid index mismatch etc..
List<string> arrLine = File.ReadAllLines("Z:/Daniel/SortedAccounts.txt").ToList();
arrLine.RemoveAt(accountComboBox.SelectedIndex);
string newLine = "#1#" + firstNameInfoBox.Text + "#2#" + lastNameInfoBox.Text + "#3#" + emailInfoBox.Text + "#4#" + phoneNumberInfoBox.Text + "#5#EMAIL#6#";
arrLine.Add(newLine);
File.WriteAllLines("Z:/Daniel/SortedAccounts.txt", arrLine);

how to calculate total Sum in Excel

I am producing last report for my venture. I need to ascertain the Sum of aggregate cells in my excel sheet. I have done the accompanying code. Be that as it may, its not computing the qualities.. Here is my code:
Worksheet.Cells[20, 16].Formula =
"Sum(" + Worksheet.Cells[6, 16].Value +
":" + Worksheet.Cells[rowIndex, 16].Value + ")";
I need to figure the Cell No 16's Sum esteem and I will show it into Cells[20,16]. Its not working. Anybody help me to settle this?
Change the two occurrences of .Value in your code to .Address.
Update
... and add a = before the Sum:
Worksheet.Cells[20, 16].Formula =
"=Sum(" + Worksheet.Cells[6, 16].Address +
":" + Worksheet.Cells[rowIndex, 16].Address + ")";

HDinsight Store Pig Result

I want to submit a pig job with the .NET SDK to HDInsight (but it also happens when I submit it with PowerShell, tested that already). Using following Statement:
string queryString = "REGISTER wasb:///PigTestFolder/myudfs.jar;" +
" A = LOAD 'wasb:///PigTestFolder/wordlist.txt' USING PigStorage()AS Line:chararray;" +
" B =ORDER A BY Line asc;" +
" D= Limit B 20;" +
" Dump D;" +
" STORE D INTO 'wasb:///PigTestFolder/results/' USING PigStorage ('\t');";
When I remove the STORE command everything works perfectly fine. But Storing the result to the BLOB is a pain in the arse. It say in the error log: "AS was unexpected at this time". But if I remove the schema and define the column with $0 it says: B was unexpected at this time.
The following Code prints out: Dump unexpected at this time.
string queryString = " A = LOAD 'wasb:///PigTestFolder/wordlist.txt' USING PigStorage();" +
" Dump A;" +
" STORE A INTO 'wasb:///PigTestFolder/results/' USING PigStorage ('\t');";
I tested the code with the Hortonworks Sandbox and it works. No problem. Another mystery is, when using only the following two lines of PigLatin code it works as well:
string queryString = " A = LOAD 'wasb:///PigTestFolder/wordlist.txt' USING PigStorage();" +
"STORE D INTO 'wasb:///PigTestFolder/results/' USING PigStorage ('\t');";
Does anyone have any idea what I am doing wrong?

Categories