We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

help - copy workbook or sheets??

Hi, It''s been a late frustrating night. I have tried several ways to copy one workbook to another in memory (I have a baseline workbook, that has 15 sheets in it that I need to make several copies of to pump data into).

Is there a easy way to do this? like:
IWorkbook newWB = oldWB.CopyToNew()

I want to do this in memory to speed up my app vs. doing a disk based copy.....

I have tried cut/paste to clipboard - it crashes.
I have tried copying the sheets collection from one to the other with AddCopy() - it crashes.
Finally I wrote a loop and tried to use AddCopy() on each worksheet.. - it crashes..

Here is the code sinppet and exception... Please tell me what is wrong....

----------------------------------------
IWorkbook workbook = application.Workbooks.Create();
for (int i = 0; i < templateWorkbook.Worksheets.Count; i++ )
{
workbook.Worksheets.AddCopy(templateWorkbook.Worksheets[i]);
}
------------------------------------------


System.ArgumentOutOfRangeException was unhandled
Message="startIndex cannot be larger than length of string.\rParameter name: startIndex"
Source="mscorlib"
ParamName="startIndex"
StackTrace:
at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
at Syncfusion.XlsIO.Implementation.WorkbookImpl.AddSheetReference(String sheetName)
at Syncfusion.XlsIO.Implementation.Collections.WorksheetNamesCollection.FillFrom(WorksheetNamesCollection sourceNames, IDictionary hashNewWorksheetNames, ExcelNamesMergeOptions option)
at Syncfusion.XlsIO.Implementation.WorksheetImpl.CopyNames(WorksheetImpl basedOn, Hashtable hashNewNames)
at Syncfusion.XlsIO.Implementation.WorksheetImpl.CopyFrom(WorksheetImpl worksheet, Hashtable hashStyleNames, Hashtable hashWorksheetNames, IDictionary dicFontIndexes, ExcelWorksheetCopyFlags flags, Hashtable hashExtFormatIndexes)
at Syncfusion.XlsIO.Implementation.Collections.WorksheetsCollection.AddCopy(IWorksheet sheet)
at hdcReportGenerator.ReportGenerator.ProcessReport(Int32 rpt_num) in C:\svn\hdcReportGenerator\hdcReportGenerator\ReportGenerator.cs:line 53
at hdcReportGenerator.ReportGenerator.ProcessAllReports() in C:\svn\hdcReportGenerator\hdcReportGenerator\ReportGenerator.cs:line 43
at hdcReportGenerator.Form1.btnProcess_Click(Object sender, EventArgs e) in C:\svn\hdcReportGenerator\hdcReportGenerator\Form1.cs:line 33
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at hdcReportGenerator.Program.Main() in C:\svn\hdcReportGenerator\hdcReportGenerator\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

1 Reply

MW Melba Winshia Syncfusion Team August 30, 2006 01:36 PM UTC

Hi David,

I was not able to reproduce the problem. Here is the sample that I used for testing.
Copying_files.zip


Please take a look at the sample above and let me know if you have any other questions. Would it be possible to provide me your sample xls file in which you are able to reproduce the issue? It will help me in investigating further on this issue.


Regards,
Melba

Loader.
Live Chat Icon For mobile
Up arrow icon