简体   繁体   中英

Copy Formula To Multiple Cells Using EPPlus

I am using Epplus and am using it to input a formula into cells K1 and K2 and this code works great for that. However, I need to "drag" the formulas from K1 and K2 to the right until I reach the last column with data.

My current code is

ws.Cells["K1"].Formula = "=LEFT(K7,(FIND(\" \", K7,1))";
ws.Cells["K2"].Formula = "=RIGHT(K7,LEN(K7)-FIND(\" \", K&,1))";
ws.Cells["K2"].Style.WrapText = true;
ws.Cells["K2"].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
ws.Cells["K2"].Style.HorizontalAlighment = ExcelHorizontalAlignment.Center;

Here is how you do it. Create a Range and add a Formula to that with the first cell(s) that will be used in the formula. Below a simple working example with random data.

Random rnd = new Random();

//create a new ExcelPackage
using (ExcelPackage excelPackage = new ExcelPackage())
{
    //create the WorkSheet
    ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("Sheet 1");

    //add 10 rows and 5 cols of dummy data
    for (int i = 1; i <= 10; i++)
    {
        for (int j = 1; j <= 5; j++)
        {
            worksheet.Cells[i, j].Value = rnd.Next(1, 1000);
        }
    }

    //get the range for the cells that will contain the formula
    //in this case column G, row 1 to the last row
    var range = worksheet.Cells[1, 7, worksheet.Dimension.End.Row, 7];

    //add the formula to the range
    //you can use the first row for the values, epplus will make the range working for the correct cells
    range.Formula = string.Format("SUM({0}:{1})", worksheet.Cells[1, 1].Address, worksheet.Cells[1, 5].Address);

    //calculate the formulas
    worksheet.Calculate();
}

Another method of achieving your desired result is this

using (var pck = new ExcelPackage(new FileInfo("C:\\Test\\Test.xlsx")))
{
    ExcelWorksheet ws = pck.Workbook.Worksheets[0];
    var startColumnIndex = 11;
    const int dataSourceRowIndex = 3; 

    while (ws.Cells[dataSourceRowIndex, startColumnIndex].Value != null)
    {
        ws.Cells[1, startColumnIndex].Formula = $"=LEFT({ws.Cells[dataSourceRowIndex, startColumnIndex].Address},(FIND(\" \",{ws.Cells[dataSourceRowIndex, startColumnIndex].Address},1)-1))";
        ws.Cells[2, startColumnIndex].Formula = $"=RIGHT({ws.Cells[dataSourceRowIndex, startColumnIndex].Address},LEN({ws.Cells[dataSourceRowIndex, startColumnIndex].Address})-FIND(\" \",{ws.Cells[dataSourceRowIndex, startColumnIndex].Address},1))";
        ws.Cells[2, startColumnIndex].Style.WrapText = true;
        ws.Cells[2, startColumnIndex].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
        ws.Cells[2, startColumnIndex].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

        startColumnIndex++;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM