Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Mon Feb 28, 2022 2:30 pm

I have a need to change the order of columns on a worksheet. How can I do this.

trozzi1957
 
Posts: 5
Joined: Sun Oct 14, 2018 10:58 pm

Tue Mar 01, 2022 3:51 am

Hello,

Thanks for your inquiry!

Please refer to the following code to achieve your needs. Here I also attached my test file for your reference. If there is any question, just feel free to contact us.

Code: Select all
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"E:\testdoc\exc3.xlsx");
            Worksheet worksheet = workbook.Worksheets[0];
            int[] neworder = new int[] { 4, 1, 2, 0, 5, 3, 6 };
            Worksheet newSheet = workbook.Worksheets.Add("temp");
            newSheet.CopyFrom(worksheet);
            for(int i= 0; i < neworder.Count(); i++)
            {
                newSheet.Columns[i].Copy(worksheet.Columns[neworder[i]], true, true);
            }
            workbook.Worksheets.Remove(newSheet);
            workbook.SaveToFile("res.xlsx", Spire.Xls.FileFormat.Version2013);           
            Process.Start("res.xlsx");


Sincerely,
Marcia
E-iceblue support team
Attachments
input.zip
(9.09 KiB) Downloaded 90 times
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Mon Mar 07, 2022 8:40 am

Hello,

Hope you are doing well!

Has the issue been solved now? Could you please give us some feedback at your convenience?

Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Return to Spire.XLS