Dynamically change column names in excel template

Hi Team,

I have an excel template. I want to dynamically allow user to change the column headings for the template but want them to get saved in it… He should be able to generate the excel with the new names without any change being reflected in the template. We allowing the user to select the column from drop-down which he wishes to rename and provide the new value in a text box. Do you have any idea to do this using aspose ?

@dewan.ishi,

Thanks for providing us some details.

Please implement your task in MS Excel manually and save the Excel file to provide us here to demonstrate your requirements, we will check and help you through on how to do it via Aspose.Cells APIs.

1 Like

Hi @Amjad_Sahi : There is template file named template.xlsx. This should remain the default template. But suppose i have a user who wants to change the default template with different column names and want to generate the excel with the names provided. We want to allow him to change the name as per his wish. But at the same time we dont want those changes to effect our template file in any way. PFA the file.template_change.zip (12.1 KB)

@dewan.ishi

Thanks for using Aspose APIs.

Please see the following sample code, its input and output Excel files, its comments and screenshot for a reference. The code changes the Col5 to TestCol and it does not affect the input Excel file.

Download Links:
Input and Output Excel Files.zip (12.0 KB)

C#

//Load template Excel file
Workbook wb = new Workbook("Template.xlsx");

//Access first worksheet
Worksheet ws = wb.Worksheets[0];

//Change Col5 to TestCol
Cell cell = ws.Cells["G4"];
cell.PutValue("TestCol");

//Save the output Excel file
wb.Save("output.xlsx");

Screenshot:

@shakeel.faiz : this is you know the location what if i only know the column name.so i have to iterate through all the names and replace them with the corresponding names given by the user ?

@dewan.ishi,

You may find the specified column name (string/text) in the sheet and replace it with your desired data using Find and Search options provided by Aspose.Cells APIs. Please see the following article for your reference:

Please see the following code and its comments. The code searches your desired column name and then replaces it with new column name.

C#

//Specify the column name which you want to find/search
string colName = "Col2";

//Specify the new column name
string newColName = "TestCol";

//Load template Excel file
Workbook wb = new Workbook("Template.xlsx");

//Access first worksheet
Worksheet ws = wb.Worksheets[0];

//You will have to find your cell
FindOptions opts = new FindOptions();
opts.LookAtType = LookAtType.EntireContent;
opts.LookInType = LookInType.Values;

//This is your needed cell
Cell cell = ws.Cells.Find(colName, null, opts);

//Change Col5 to TestCol
cell.PutValue(newColName);

//Save the output Excel file
wb.Save("output.xlsx");