We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Exclude the TxtSaveOptions setting on the CSV header

Hi

I like to export a datatable to csv. This csv will have the first row as header.

Dim opts As TxtSaveOptions = New TxtSaveOptions(SaveFormat.CSV)
opts.QuoteType = TxtValueQuoteType.Always

workbook.Save("c:\file.csv", opts)Dim opts As TxtSaveOptions = New TxtSaveOptions(SaveFormat.CSV)
opts.QuoteType = TxtValueQuoteType.Always

workbook.Save("c:\file.csv", opts)

Results:-

"ColumnA","ColumnB","ColumnC"
"Data1a","Data1b","Data1c"
"Data2a","Data2b","Data2c"

Is there a way to exclude the double quotes in the headers since only the data should be enclosed in quotes?

Please advise

Thanks in advance.

Hi Yi Wen,

Thanks for your posting and using Aspose.Cells.

As a workaround, you can read all text of your CSV file and then from the first line remove all double quotes and re-save your file. It will make your header double quote less.

Please see the following code for your reference. I have attached the sample csv file and the output csv file for your reference.

VB.NET


Dim filePath As String = “F:\Shak-Data-RW\Downloads\sample.csv”


'Read all csv text

Dim fileText As String = File.ReadAllText(filePath)


'Get the first line from it, this is a string to find

Dim strToFind As String = fileText.Substring(0, fileText.IndexOf(vbCrLf))


'Remove double quotes from string to find, now it has become string to replace

Dim strToReplace As String = strToFind.Replace("""", “”)


'Find all the strings to find and replace them with string to replace

fileText = fileText.Replace(strToFind, strToReplace)


'Now save your csv text in your csv file.

'Your csv header row will now be double quote less

File.WriteAllText(filePath & “.out.csv”, fileText)

Thanks, works perfectly.

Hi,


Good to know that your issue is resolved now. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.

Thank you.