This project has moved. For the latest updates, please go here.

Index was out of range. Must be non-negative and less than the size of the collection

Jun 5, 2014 at 9:05 PM
Edited Jun 5, 2014 at 9:06 PM
In my project , user will download the excel from UI he has the leverage to select the work book tabs which he want to see in the excel sheet. One he get that excel he can use it to get data to the corresponding excel sheet. By uploading the excel. The problem I will encountered is if I download the excel and use that excel sheet to get data from my application I am getting following error
{"Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"}
After downloading the excel and open that excel and hit manually save then I upload that excel then it works fine.
Below are the code I used:
  public byte[] DownloadExcel(string path, string tempPath, List<string> SelectedTabs, int colN)
    {
        FileInfo fileInfo = new FileInfo(path);
        File.Copy(path, tempPath, true);
        FileInfo tempFileInfo = new FileInfo(tempPath);
        List<string> workBookName = new List<string>();
        IEnumerator<ExcelWorksheet> workSheets = null;
        ExcelPackage newPackage = null;
        using (newPackage = new ExcelPackage(tempFileInfo))
        {
            using (ExcelPackage package = new ExcelPackage(fileInfo))
            {
                workSheets = package.Workbook.Worksheets.GetEnumerator();
                while (workSheets.MoveNext())
                {
                    ExcelWorksheet xlSheet = workSheets.Current;
                    workBookName.Add(workSheets.Current.Name);

                    if (commonTabList.ContainsIgnoreCase(xlSheet.Name))
                    {
                        //do nothing for the common tab
                        continue;
                    }
                    else if (!SelectedTabs.Contains(xlSheet.Name))
                    {
                        if (!SelectedTabs.Contains(xlSheet.Name.Remove(xlSheet.Name.Length - 1)))
                        {
                            newPackage.Workbook.Worksheets.Delete(workSheets.Current.Name);
                            workBookName.Remove(workSheets.Current.Name);
                        }
                    }
                    else if (xlSheet.Hidden.ToString() == "Hidden")
                    {
                        newPackage.Workbook.Worksheets.Delete(workSheets.Current.Name);
                        workBookName.Remove(workSheets.Current.Name);
                    }
                }
            }
return newPackage.GetAsByteArray();
}
}

for getData code:
    public void GetExcel(XmlDocument doc, String path, String templatePathName, String tabNameList = null)
    {
 FileInfo fileInfo = new FileInfo(path);
 FileInfo templateFileInfo = new FileInfo(templatePathName);
     using (ExcelPackage package = new ExcelPackage(fileInfo, templateFileInfo))
        {

// added logic to add corresponding data to excel
     package.save(); _// at this location I am getting exception_
      }
      }
I see Problem in download excel please let me know suggestions.