Good afternoon community question C # language? I'm making some modifications to an excel hosted on my server I need to go through it, check if the rows are empty add a new row with header and fill the new row with the sum of two fields, and reviewed many link pages on how to do it and could not To give a solution, I was commissioned to use the office interop library. This is my code:
protected void btnACS_Click(object sender, EventArgs e)
{
//variables para poder manipular el excel
Excel.Application xlA;
Excel._Workbook xlWB;
Excel._Worksheet xlWS;
object misValue = System.Reflection.Missing.Value;
Excel.Range Rng;
//Inicializar el excel y obtener el objeto de la aplicacion
xlA = new Excel.Application();
xlA.Visible = true;
//abrir excel para manipular sus datos
xlWB = xlA.Workbooks.Open(Server.MapPath("." +
"/Carpetadondeestaelarchivo/nombreexcel.xlsx"), 1, true, 5, "", "", true,
Excel.XlPlatform.xlWindows, "/t", false, false, 1, true, 1, 1);
xlWS = (Excel.Worksheet)xlWB.Worksheets[1];
// seleccionar rango activo
Rng = xlWS.UsedRange;
// leer las celdas
int rows = Rng.Rows.Count;
int cols = Rng.Columns.Count;
//recorrer las celdas e introducir una fila nueva con una formula
if (rows != null && cols != null)
{
for(int i=1; i<rows; i++)
{
for (int j = 1; j < cols; j++)
{
xlWS.Cells[1, 6]="Titulo";//Inserta titulo
xlWS.Cells[2, 6] = "=([@filaseleccionada]-[@filaseleccionada]+[@filaseleccionada])";//Inserta formula
}
}
}
//Para guardar los cambios y cerrar todo en el archivo
xlWB.Save();
xlWB.Close(true,misValue,misValue);
xlA.Quit();
releaseObject(xlWS);
releaseObject(xlWB);
releaseObject(xlA);
}
I need to combine where the for with the injection of the formula is new in the world of the c #