unvell / ReoGrid

Fast and powerful .NET spreadsheet component, support data format, freeze, outline, formula calculation, chart, script execution and etc. Compatible with Excel 2007 (.xlsx) format and working on .NET 3.5 (or client profile), WPF and Android platform.
https://reogrid.net
MIT License
1.33k stars 396 forks source link

Grid Excel Custom Export function #423

Open ByungchunBang opened 3 years ago

ByungchunBang commented 3 years ago

Describe the feature

I need custom excel export function. condition

so , i made code as below.

        var grid_memory =  unvell.ReoGrid.ReoGridControl.CreateMemoryWorkbook();
        grid_memory.AddWorksheet(grid_test.CurrentWorksheet); // grid_test is UI grid.
        grid_memory.Worksheets[0].DeleteRows(3, 1);
        grid_memory.Save("test.xls");          

but i got error message. unvell.ReoGrid.WorkbookException: 'Specified worksheet belongs to another workbook, remove from another workbook firstly.'

could you advise ?

Which edition of ReoGrid is targeted

jingwood commented 3 years ago

Maybe you have to clone the worksheet?

grid_memory.AddWorksheet(grid_test.CurrentWorksheet.Clone());
ByungchunBang commented 3 years ago

Hi Jingwood, Thanks for your reply. I applied that code but i got same error. image

jingwood commented 3 years ago

OK, I will check this and reply you if there is a solution.

smpleader commented 1 year ago

Hello, @jingwood As to my check with function public Clone(string newName = null) ( line 36 file Reogrid\ReoGrid\Core\Worksheet\Utility.cs ) It always checks workbook not null, then after clone, it's always stick to existing workbook. ( line 38 ) My proposals: 1- Add new function SwitchWorkbook( IWorkbook wb )

2- Change line 36 to public Worksheet Clone(string newName = null, IWorkbook? wb = null)

Let me know what do you think. PS: great work with Reogrid. Thanks for a nice product. Regards, Minh

jingwood commented 1 year ago

@smpleader Thank you for your suggestion.

Moving a worksheet from one workbook to another can be a complex process, as named ranges and formulas may still contain references to other worksheets inside the original workbook. I'm considering the following workflow:

  1. Clone the worksheet and remove all references.
  2. Insert the cloned worksheet into the new workbook.
  3. Delete the original worksheet from the old workbook.

The cloning operation is crucial for addressing issues related to references. I recall that the problem lies in the fact that cloning doesn't remove the reference to the old workbook. Perhaps the solution for this issue is to ensure that the cloning process removes the reference from the old workbook. What do you think about this approach?

smpleader commented 1 year ago

Hello @jingwood I agree with your approach. In any case, the developer should be aware of referenced cells, at his side. We can consider the extra: 1- Clone a workbook 2- Safe clone: check if any reference of sheet before the clone to new workbook More about scenario: I would like to use Memory workbook as a storage, and show a certain sheet in a different form. Looking forward to hear more from you. Thank you.