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

Unable to create excel file with ANSI encoding

Hi,


I have to create and saving an excel file  in ANSI encoding. I am using below code but it it is still creating file in UTF-8 encoding.


using (ExcelEngine excelEngine = new ExcelEngine())
                {
                    IApplication application = excelEngine.Excel;
                    application.DefaultVersion = ExcelVersion.Xlsx;
                    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                    IWorkbook workbook = application.Workbooks.Create(1);
                    
                    IWorksheet worksheet = workbook.Worksheets[0];
                    worksheet.ImportDataTable(dataTable, true, 1, 1, true);


                   IListObject table = worksheet.ListObjects.Create("WEEKLYDATA", worksheet.UsedRange);


                   table.BuiltInTableStyle = TableBuiltInStyles.TableStyleMedium14;


                   worksheet.UsedRange.AutofitColumns();



                 
                       
                        string fileName = "ReportFile.csv");


                       Stream excelStream = File.Create(fileName);
                      


                       CultureInfo cultureInfo =CultureInfo.CurrentCulture;
                        Encoding encoding= Encoding.GetEncoding(cultureInfo.TextInfo.ANSICodePage);
                       
                        workbook.SaveAs(excelStream, ",", encoding);
                        excelStream.Dispose();

}


Please help.


3 Replies

KK Konduru Keerthi Konduru Ravichandra Raju Syncfusion Team October 18, 2022 01:36 PM UTC

Hi naina,


Greetings from Syncfusion.


Syncfusion XlsIO saves the Excel file with selected encoding. As you have reported that the encoding is set improperly in saved Excel document, kindly let us know how are you checking the encoding of this saved file. This helps us in investigating the query further.


Regards,

Keerthi.



NR naina ramnani October 18, 2022 01:44 PM UTC

Hi Keerthi, I am opening file in notepad and see the encoding over there. I have set the default encoding for my notepad as ANSI in registry editor



KK Konduru Keerthi Konduru Ravichandra Raju Syncfusion Team October 19, 2022 11:25 AM UTC

Hi naina,


Thanks for the update.


There is an option called Apply to opened ANSI files under UTF-8 encoding in notepad settings. If this option is enabled, even when the ANSI encoded files are opened, encoding is shown as UTF-8 itself.



We suggest you to disable this option, verify and let us know if the issue is resolved.


Regards,

Keerthi.


Loader.
Live Chat Icon For mobile
Up arrow icon