Encode the file name when it is in different language
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. It also converts Excel documents to PDF files.
How to encode file name when it is in a different language?
While viewing the Excel file in a browser, sometimes the file name gets changed. We would prefer to see the filename exactly displayed in the File Download dialog box. Normally, we use 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 contains only US-ASCII characters, it displays the filename correctly. The problem arises 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 the HttpUtility.UrlEncode class to encode the file name in the 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 in a different language can be downloaded from the EncodeFileName.zip.
Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through formulas, adding charts in worksheets or workbooks, organizing and analyzing data through tables and pivot tables, appending multiple records to a worksheet using template markers, and most importantly, PDF and image conversions, etc., with code examples.
Refer here to explore the rich set of Syncfusion Essential XlsIO features.
Note:
Starting with v16.2.0.x, if you reference Syncfusion assemblies from the trial setup or from the NuGet feed, include a license key in your projects. Refer to this link to learn about generating and registering a Syncfusion license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about encoding the file name when it is in a different language in XlsIO.
You can refer to our XlsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums or feedback portal. We are always happy to assist you!