简体   繁体   English

使用EPPlus使用图表重命名工作表

[英]Renaming sheet with a chart, using EPPlus

I've got an EPPlus application that is using an existing Excel worksheet as a template for an indeterminate number of new sheets (the size of the data set will vary). 我有一个EPPlus应用程序,它使用现有的Excel工作表作为不确定数量的新工作表的模板(数据集的大小会有所不同)。

I'm trying to accomplish this by either creating the new sheets, like this: 我正在尝试通过创建新的工作表来实现此目的,如下所示:

    ExcelWorksheet templateWs = templateFile.Workbook.Worksheets["Sheet1"];
    ws = ExcelFile.Workbook.Worksheets.Add(string.Format("{0} {1}", "Sample ", sampleID), templateWs);

or else 要不然

    ExcelWorksheet templateWs = templateFile.Workbook.Worksheets["Sheet1"];
    ws = ExcelFile.Workbook.Worksheets.Add("Sheet1", templateWs);
    ws.Name = string.Format("{0} {1}", "Sample ", sampleID);

Unfortunately, both of these have the same result: the worksheet name changes, but the data series reference in the charts does not. 不幸的是,这两种方法都具有相同的结果:工作表名称更改,但图表中的数据系列引用没有更改。 It continues to reference "Sheet1". 它继续引用“ Sheet1”。

Is there a way around this? 有没有解决的办法? Possibly some way to define the data series reference in the chart to refer to "current sheet" instead of "Sheet1"? 可能以某种方式将图表中的数据系列参考定义为引用“当前工作表”而不是“ Sheet1”? Or some EPPlus trick to change the sheet reference in a chart? 还是一些EPPlus技巧来更改图表中的图纸参考?

Thanks 谢谢

The template-based Add overload does not handle charts very well. 基于模板的“ Add重载不能很好地处理图表。 I havent ever seen a good solution other than manually fixing them after. 除了以后手动修复它们之外,我从未见过好的解决方案。 Not pretty since there are some many edge cases with charts which is probably why it is not as well developed as it needs to be. 不太漂亮,因为图表有很多边缘情况,这可能就是为什么它没有得到应有的发展的原因。

As an example, if we use the chart generated here: 例如,如果我们使用此处生成的图表:

How to put two series with different types of charts inside a control chart using Epplus? 如何使用Epplus将两个具有不同类型图表的系列放置在控制图中?

we have a case of an ExcelChart WITHIN an ExcelChart . 我们在ExcelChart一个ExcelChart Really sneaky! 真是偷偷摸摸! So to do a proper copy you have to loop through both the charts PlotArea and ExcelSeries . 因此,要进行适当的复制,您必须遍历图表PlotAreaExcelSeries So, using the excel file generated by that test method in my answer we get something like this: 因此,在我的答案中使用该测试方法生成的excel文件,我们得到的是这样的:

[TestMethod]
public void Chart_Template_Rename_Test()
{
    //https://stackoverflow.com/questions/57829441/renaming-sheet-with-a-chart-using-epplus

    var templateFileInfo = new FileInfo(@"C:\temp\Chart_Two_Series.xlsx");
    Assert.IsTrue(templateFileInfo.Exists);

    var fileInfo = new FileInfo(@"C:\temp\Chart_Template_Rename_Test.xlsx");
    if (fileInfo.Exists)
        fileInfo.Delete();

    using (var templateFile = new ExcelPackage(templateFileInfo))
    using (var ExcelFile = new ExcelPackage(fileInfo))
    {
        ExcelWorksheet ws;
        var sampleID = "Sample1";
        var origWsName = "Content";

        ExcelWorksheet templateWs = templateFile.Workbook.Worksheets[origWsName];
        ws = ExcelFile.Workbook.Worksheets.Add(origWsName, templateWs);
        ws.Name = string.Format("{0} {1}", "Sample ", sampleID);

        //Look for "top-level" charts
        foreach (var excelDrawing in ws.Drawings)
        {
            if (!(excelDrawing is ExcelChart chart))
                continue;

            //Charts can contain other charts so use plot area to loop through them
            foreach (var chartType in chart.PlotArea.ChartTypes)
            foreach (ExcelChartSerie serie in chartType.Series)
            {
                serie.Series = serie.Series.Replace(origWsName, ws.Name);
                serie.XSeries = serie.XSeries.Replace(origWsName, ws.Name);

                if (serie.HeaderAddress != null)
                    serie.HeaderAddress = new ExcelAddressBase(serie
                        .HeaderAddress
                        .Address
                        .Replace(origWsName, ws.Name)
                    );
            }
        }

        ExcelFile.Save();
    }
}

Which gives this in the new Workbook Worksheet (note the Sheet name which was originally "Content"): 在新的工作簿工作表中提供了此信息(请注意工作表名称最初是“内容”):

在此处输入图片说明

However, keep in mind that if a the original XLXS has been opened and by Excel itself that can create other problems with coping sheets with charts. 但是,请记住,如果原始XLXS已打开,并且Excel本身已打开,这可能会在处理图表时产生其他问题。 Again, edge cases that have to be dealt with. 同样,必须处理的极端情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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