issue header changed after export data to excel ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

issue header changed after export data to excel ?

I work on sql server 2017 I run script depend on python language v 3.10 . I need to export data to excel fileStudentExport.xlsx already exist, and keep header without change after export. header of excel file StudentExport.xlsx before export data to it as below StudentId,StudentName after run script query to export data to StudentExport.xlsx and Header changed to StudentId,Name my issue is header changed from column name StudentName to Name (exist on sql) I export data to excel by this line InputDataSet.to_excel(FullFilePath,sheet_name=TableName.split(".")[-1],index=False) I try to change InputDataSet.to_excel it to keep header on excel file StudentExport.xlsx without change as below InputDataSet.to_excel(FullFilePath,sheet_name=TableName.split(".")[-1], header=False, startrow= 1,index=False) but it give me data without header and header row blank so can any one help me to export data to excel file without change or overwrite header ? Notes Not practical way to change column name from Name to StudentName on sql server table create view to use it as excel header . expected result StudentId StudentName 1 ahmed script Query I run it as below for lookup SET @PythonScript = N'import shutil FullFilePath = ExcelFilePath+"StudentExport.xlsx" shutil.copy(FixedPath,ExportPath) InputDataSet.to_excel(FullFilePath,sheet_name=TableName.split(".")[-1],index=False) 'f exec sp_execute_external_script @language = N'Python' ,@script = @PythonScript ,@input_data_1 = @SQL ,@params = N'@ExcelFilePath NVARCHAR(MAX), @TableName NVARCHAR(200),@FixedPath NVARCHAR(MAX),@ExportPath NVARCHAR(MAX)' ,@ExcelFilePath = @ExportPath -- file path where Excel files are placed ,@TableName = @TableName ,@FixedPath=@FixedPath ,@ExportPath=@ExportPath sql server table CREATE TABLE [dbo].[students]( [StudentId] [int] NOT NULL, [Name] [varchar](50) NULL, CONSTRAINT [PK_students] PRIMARY KEY CLUSTERED ( [StudentId] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE =

3rd May 2022, 10:41 PM
ahmed elbarbary
1 Answer
0
To export data to an Excel file without changing or overwriting the header, you can follow these steps: Retrieve the existing header from the Excel file before exporting the data. Export the data to a temporary Excel file without the header. Open the temporary file and write the header back to the first row. Save the temporary file with the desired header intact. Optionally, delete the temporary file.
29th May 2023, 10:15 AM
Jared
Jared - avatar