vb.net - Line breaks lost when exporting datagrid to excel in CSV format WPF -
this works fine, assuming there no line breaks in cells.
dgvresults.selectallcells() dgvresults.clipboardcopymode = datagridclipboardcopymode.includeheader applicationcommands.copy.execute(nothing, dgvresults) dim result [string] = directcast(clipboard.getdata(dataformats.commaseparatedvalue), string) clipboard.clear() dgvresults.unselectallcells() try dim file new system.io.streamwriter("c:\export.csv") file.writeline(result) file.close() process.start("c:\export.csv") catch ex exception messagebox.show(ex.message, "error") end try
this how add line breaks
dim x new list(of string) each item in res.properties("proxyaddresses") x.add(item) next allsmtpaddresses = string.join(controlchars.lf, x)
when export this, doesn't take consideration there line breaks, , ignores them... excel formatting little wonky. i've tried: environment.newline, vbcrlf, , controlchars.lf. think excel doesn't know line breaks, w.e wants, , creates new rows them.
any idea on how attack this?
updated results @jimmy
this it's supposed like...
i don't believe possible without modifying rows prior export. found example code may help,
public sub writecsv(grid1 object, outputfile string) ' create csv file grid data exported. dim sw new streamwriter(outputfile) ' first write headers. dim dt datatable = directcast(grid1.datasource, dataset).tables(0) dim icolcount integer = dt.columns.count integer = 0 icolcount - 1 sw.write(dt.columns(i)) if < icolcount - 1 sw.write(",") end if next sw.write(sw.newline) ' write rows. each dr datarow in dt.rows integer = 0 icolcount - 1 sw.write("""") 'lets encapsulate fields in quotes, quoted csv file! if not convert.isdbnull(dr(i)) sw.write(dr(i).tostring()) end if sw.write("""") if < icolcount - 1 sw.write(system.globalization.cultureinfo.currentculture.textinfo.listseparator) end if next sw.write(sw.newline) next sw.close() end sub
Comments
Post a Comment