Articles in this section
Category / Section

Encode the file name when it is in different language

1 min read

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files.

How to encode file name when it is in different language?

While viewing the excel file in browser sometimes the file name gets changed. We would prefer to see the filename exactly displayed in the File Download dialog box. Normally we have used the “Content-Disposition” header field to force the download, and the “filename” parameter is used to suggest a filename for the downloaded file.

If the filename just contains all US-ASCII characters, then it displays the filename correctly. The problem only happens when the filename contains non-US-ASCII characters such as Swedish or Arabic. The reason is that, the filename parameter is limited to US-ASCII. To overcome this problem, you can use HttpUtility.UrlEncode class to encode the file name in response object. This will preserve the language.

The below code snippet illustrates this.

C#

using (ExcelEngine engine = new ExcelEngine())
{
    IApplication application = engine.Excel;
    IWorkbook workbook = application.Workbooks.Create(1);
    workbook.Version = ExcelVersion.Excel97to2003;
 
    IWorksheet sheet = workbook.Worksheets[0];
    sheet.Range["A1"].Text = "Swedish character";
 
    workbook.SaveAs(HttpUtility.UrlEncode("å+ä+ö.xls"), Response, ExcelDownloadType.Open);
}

 

VB.NET

Using engine As ExcelEngine = New ExcelEngine
    Dim application As IApplication = engine.Excel
    Dim workbook As IWorkbook = application.Workbooks.Create(1)
    workbook.Version = ExcelVersion.Excel97to2003
 
    Dim sheet As IWorksheet = workbook.Worksheets(0)
    sheet.Range("A1").Text = "Swedish character"
 
    workbook.SaveAs(HttpUtility.UrlEncode("å+ä+ö.xls"), Response, ExcelDownloadType.Open)
End Using

 

A complete working sample to encode the file name  when it is different language can be downloaded from the EncodeFileName.zip.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied