Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
40
Broken Data Connections
posted

I have pre-existing Excel spreadhsheets (see attached) that I am attempting to open, modify, save, and then present to the user in Excel 2016.  Here is the code I am running the workbooks through...  It attempts to added additional sheets to the workbook as needed..by copying a sheet called "Template".  I have a attached two spreadsheets.  One before going through the code, one after going through the code.  You can look at the Data sheet and the InventoryGroupData sheets to see what I am talking about.   Any ideas as to why this is breaking?

public bool OpenExcelWorkbook(
ReportData.OutputDataDataTable p_ExcelDataTab,
int p_BracketID,
int p_Year,
string p_SubmitType,
string p_UOM,
string p_WorkBookName)
{
Workbook ExcelWorkbook = new Workbook (WorkbookFormat.Excel2007MacroEnabled);

var Bracket = context.tblBrackets.FirstOrDefault(b => b.BracketID == p_BracketID);
var GroupsList = context.tblGroups.Where(g => g.tblBrackets.BracketID == p_BracketID);

FileStream stream = File.OpenRead(p_WorkBookName);
ExcelWorkbook = Workbook.Load(stream);
stream.Close();

bool SheetFound = false;
Worksheet ExistingWorkSheet = null;
Worksheet ParameterWorkSheet = null;
Worksheet CurrentGroupSheet = null;

foreach (tblGroup CurrentGroup in GroupsList)
{
if (!CurrentGroup.tblProdCodes.Any()) continue;

string GroupName = CurrentGroup.Group.ToString().Trim();
SheetFound = false;

try
{
ExistingWorkSheet = ExcelWorkbook.Worksheets[GroupName];
if (ExistingWorkSheet != null)
{
SheetFound = true;
}
}
catch
{
SheetFound = false;
}

if(!SheetFound)
{
Worksheet BlankSheet = ExcelWorkbook.Worksheets.Add(GroupName);
CopyTemplateToNewWorksheet("Template", BlankSheet, ExcelWorkbook);
CurrentGroupSheet = ExcelWorkbook.Worksheets[GroupName];
CurrentGroupSheet.MoveToIndex(0);
}

CurrentGroupSheet = ExcelWorkbook.Worksheets[GroupName];
CurrentGroupSheet.Rows[2].Cells[4].Value = "Group ID";
CurrentGroupSheet.Rows[2].Cells[6].Value = CurrentGroup.GroupID;
}

List SheetsToRemove = new List();
foreach(Worksheet cs in ExcelWorkbook.Worksheets)
{
bool IsCurrentGroup = GroupsList.Where(y => y.Group == cs.Name).Any();
if(!IsCurrentGroup)
{
string CellValue = cs.Rows[2].Cells[4].Value != null ? cs.Rows[2].Cells[4].Value.ToString().Trim() : string.Empty;
if (CellValue == "GroupID")
{
SheetsToRemove.Add(cs.Name);
}
}
}

foreach(string SheetName in SheetsToRemove)
{
ExcelWorkbook.Worksheets.Remove(ExcelWorkbook.Worksheets[SheetName]);
}

ParameterWorkSheet = ExcelWorkbook.Worksheets["Parameters"];
ParameterWorkSheet.Rows[1].Cells[1].Value = p_BracketID;
ParameterWorkSheet.Rows[2].Cells[1].Value = p_Year - 1;
ParameterWorkSheet.Rows[3].Cells[1].Value = p_Year;
ParameterWorkSheet.Rows[4].Cells[1].Value = p_UOM;
ParameterWorkSheet.Rows[5].Cells[1].Value = p_SubmitType;

ExcelWorkbook.Save(p_WorkBookName);

return true;
}

private void CopyTemplateToNewWorksheet (string p_TemplateName, Worksheet p_NewWorksheet, Workbook p_CurrentWorkbook)
{
Worksheet TemplateWorkSheet = p_CurrentWorkbook.Worksheets[p_TemplateName];

foreach(WorksheetColumn sc in TemplateWorkSheet.Columns)
{
WorksheetColumn dc = p_NewWorksheet.Columns[sc.Index];
dc.CellFormat.SetFormatting(CreateFormatCopy(p_CurrentWorkbook, sc.CellFormat));
dc.Width = sc.Width;
dc.Hidden = sc.Hidden;
}

foreach (WorksheetRow sr in TemplateWorkSheet.Rows)
{
WorksheetRow dr = p_NewWorksheet.Rows[sr.Index];
dr.CellFormat.SetFormatting(CreateFormatCopy(p_CurrentWorkbook, sr.CellFormat));
dr.Height = sr.Height;
dr.Hidden = sr.Hidden;


foreach (WorksheetCell scell in sr.Cells)
{
WorksheetCell dcell = dr.Cells[scell.ColumnIndex];
dcell.CellFormat.SetFormatting(CreateFormatCopy(p_CurrentWorkbook, scell.CellFormat));
dcell.Value = scell.Value;
}
}
return;
}

static IWorksheetCellFormat CreateFormatCopy(Workbook workbook, IWorksheetCellFormat sourceCellFormat)
{
IWorksheetCellFormat copy = workbook.CreateNewWorksheetCellFormat();
copy.SetFormatting(sourceCellFormat);
return copy;
}

Infragistics SpreadSheet Examples.zip
Parents
  • 18204
    Offline posted

    Hello Shawn,

    Thank you for posting in our forums!

    I have a couple questions to help better understand the issue.

    1. Once loaded in, does the Worksheet object for those two sheets have any Tables in their Tables collection available?
    2. What version and build of Infragistics are you experiencing this with? e.g. 16.2.20162.2013

    If there is no table there, for a workaround you could manually add the Table back in by following this documentation.

    Looking forward to hearing back from you.

Reply Children