epplus / excel写入具有多个字符的单元格

时间:2018-07-10 14:04:02

标签: c# excel epplus

使用EPPLUS库编写excel单元时遇到了一些困难。我可以完美地将值写入A到Z范围内的单元格,而不是AA等等。所以我的问题是:如何写单元格地址包含多个字母的单元格。例如,如果来自“ Product_row”范围的产品代码具有B7:AA7地址,该怎么办?我尝试使用R1C1表示法,但是没有获得正确的字符串格式。

这是我的代码:

using (ExcelPackage package = new ExcelPackage(newFileName))
{

    var sheet = package.Workbook.Worksheets[Worksheet_Name];

    //Make a new list with Product name and pallet count
    foreach (var item in paletInfo.PaletDefinitions)
    {
        pList.Add(new Data { pName = item.ProductName.Trim(), pCount = item.ProductCounter });
    }

    #region Fill Product and Palet count
    foreach (var col in sheet.Names.First(c => c.Name == "Product_row"))
    {
        var itemGroup = pList.GroupBy(i => i.pName).Select(g => new { pName = g.Key, TotalQty = g.Select(j => j.pCount).Sum() });
        var range = col.Address;
        var queryResult = itemGroup.FirstOrDefault(c => c.pName == sheet.Cells[range].Text);
        if (queryResult == null) continue;

        var value = queryResult.TotalQty;
        range = range.Substring(0, 1) + (Convert.ToInt32(range.Substring(1)) + 10 + actualDay - 2);

        sheet.Cells[range].Value = value;
    }
    #endregion

    sheet.Cells["AA1"].Value = actualMonth;
    sheet.Cells["AI1"].Value = actualYear;

    try
    {
        package.Save();
    }
    catch (Exception e)
    {
        Logger.Info(e.Message);
        Logger.Error(e, $"Excel Export: {e.Message}");
        mail.Send(Mail_Origin, Mail_Error, "error",$"Error when writing to Excel {nF} file, maybe file is still OPEN?");
    }
    Logger.Info("Excel_Export invoked");

}

0 个答案:

没有答案