【问题标题】:Export PDF error, An item with the same key has already been added导出 PDF 错误,已添加具有相同密钥的项目
【发布时间】:2015-07-23 18:07:27
【问题描述】:

我正在尝试从数据库中导出 pdf,但我遇到了这个问题

已添加具有相同键的项目

在最后一行 - zip.AddFile(str, "");

那么如何解决这个问题呢?以下是代码:

public static void generateRespondentReport(string startDate, string endDate)
{
    List<string> inpatientFileList = generateRespondentReportByQuestionSet(1, startDate, endDate);
    List<string> outpatientFileList = generateRespondentReportByQuestionSet(2, startDate, endDate);
    List<string> visitorFileList = generateRespondentReportByQuestionSet(3, startDate, endDate);

    // timestamp           
    DateTime now = DateTime.Now;
    string FileName = "RespondentReport_" + now.Day + now.ToString("MMM") + now.Year + "_" + string.Format("{0:hh-mm-sstt}", now) + ".zip";
    System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
    response.ClearContent();
    response.Clear();
    response.ContentType = "application/zip";
    response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");

    // add into zipfile
    ZipFile zip = new ZipFile();
    foreach (string str in inpatientFileList)
    {
        zip.AddFile(str, "");
    }
    foreach (string str in outpatientFileList)
    {
        zip.AddFile(str, "");
    }
    foreach (string str in visitorFileList)
    {
        zip.AddFile(str, "");
    }

【问题讨论】:

    标签: c# asp.net database dictionary


    【解决方案1】:

    在添加文件之前,首先检查该文件在 zip 中是否已经存在...

            foreach (string str in <X>)
            {
                var nameCount = zip.Count(entry => entry.FileName == str);
                if (nameCount>0)
                {
                    zip.AddFile(str + string.Format("({0})",nameCount), "");
                }
                else
                {
                    zip.AddFile(str, "");
                }
            }
    

    【讨论】:

    • 您在使用 zipDotNet 吗?如果是这样,我会使用一个新的“AddFile”方法来扩展该类,该方法可以找出文件名冲突。有的话我会写一篇。
    • 是否代表我的文件列表?那我这个方法做三遍?
    • 是的,它代表文件列表。是的,或者您可以将所有列表连接在一起,或者创建一个辅助函数。我尝试创建 FileList 的扩展,但不能扩展:\
    • 我用这个方法做了三遍,还是一样的错误。有没有其他方法可以解决这个问题?
    • 路径变量(AddFile 的第二个变量)很可能是空的,可能会引发错误。不太可能是 generateRespondentReportByQuestionSet 或 foreach 循环之后的代码。这些文件在内存中吗?
    猜你喜欢
    • 2017-01-02
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    相关资源
    最近更新 更多