Setting the Encoding Type

Hi,

I'm using Aspose.Cells for .Net and I've (finally) upgraded to the latest version.

I'm just fixing up my code to comply with the new workbook.Save standard but I can't seem to find where to set the encoding type. Using the old version I used this:

workbook.Save("Output.txt", FileFormatType.TabDelimited, SaveType.OpenInExcel, System.Web.HttpContext.Current.Response, System.Text.Encoding.ASCII)

and I've converted to this:

workbook.Save(Me.Response, "Output.txt", ContentDisposition.Attachment, New XlsSaveOptions(SaveFormat.TabDelimited))

I tried creating the XlsSaveOptions separatly thinking that the option may be there, but to no avail.

Any ideas?

Thanks,
Steve

Hi,

I think, you should use TxtSaveOptions.

Here is a code example.

C#


workbook.Save(HttpContext.Current.Response, “Output.txt”,
ContentDisposition.Attachment,
new
TxtSaveOptions(SaveFormat.TabDelimited));

HttpContext.Current.Response.End();

Hi,

Thanks for the response. I've made the changes you've suggested for the text file, however I've also got another scenario where I need to set the encoding type. This time it's a CSV, and my requirements state it needs to be in 7 bit ASCII format and that Unicode formats should not be used.

My code is currently:

workbook.Save("y:\upload\" & filename, SaveFormat.CSV)

But I can't find where to set the encoding type.

Cheers,
Steve.

Hi,

You need to set encoding using Workbook.Settings.Encoding property.

Here is a complete code that saves workbook as CSV file in ASCII or UNICODE encoding.

C#


//Create a workbook

Workbook workbook = new Workbook();


//Access a sheet

Worksheet worksheet = workbook.Worksheets[0];


//Add some sample data

worksheet.Cells[“A1”].PutValue(123);

worksheet.Cells[“A2”].PutValue(456);


//We set econding of the workbook here. Only valid for csv files

workbook.Settings.Encoding = Encoding.ASCII;


//uncomment it if you want to save in unicode

// workbook.Settings.Encoding = Encoding.Unicode;


//Save the workbook in csv format

workbook.Save(“f:\downloads\output.csv”, SaveFormat.CSV);

Beautiful - thank you so much!