JoshClose / CsvHelper

Library to help reading and writing CSV files
http://joshclose.github.io/CsvHelper/
Other
4.69k stars 1.06k forks source link

CsvHelper adds " " around my string #659

Closed ootsun closed 7 years ago

ootsun commented 7 years ago

Hello, when I export my string Museums, art galleries, and botanical and zoological gardens into a xlsx file, CsvHelper adds " " around it. Any idea why?

JoshClose commented 7 years ago

Can you paste the row in for me to take a look at?

ootsun commented 7 years ago
// Write the list into a xls file then save this file
                using (var workbook = new XLWorkbook(XLEventTracking.Disabled))
                {
                    using (var writer = new CsvWriter(new ExcelSerializer(workbook)))
                    {
                        writer.Configuration.RegisterClassMap<LeadClassMap>();
                        writer.WriteRecords(list);
                    }
                    workbook.SaveAs(pathString);
                }

Not sure if it's what u asked to see

ootsun commented 7 years ago

image

ootsun commented 7 years ago

image We can see with visual studio that it's a normal string

JoshClose commented 7 years ago

What if you open the file with a text editor instead of excel?

ootsun commented 7 years ago

PK  LjwJ±ùS º   xl/workbook.xml ¢ (  Ïn!Æ_…̽²nbÛlD/öà¥iRÓž—¸Àf@Ýw롏ÔW(¬MO=1ÿ~ß|ÃÏ×÷|9¸Ž‘¢ ^ÀtRC¯‚¶~'àÌÃ3,ó¡9ÚoCس<ïcCÚ”ú†ó¨Zt2NB>÷L 'SNiǃ1VᨃCŸx]Uœ°“)í#\Ô†ÿ¨ÅžPêØ"&םŜ´îݽËÞñU:°imü¼4€ñ2W‹§x•3–bz/âòH•ì7r;f™åàÑÇ-b~\ù2ô°±¶ÖjÔØÐZ×W¡«ÑXºøg‡Jvª\‘ŸÂOëÙS=»‚WÇ‹_PK LjwJÔ^S¨œ œ  _rels/.rels ¢ (  <?xml version="1.0" encoding="utf-8"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="/xl/workbook.xml" Id="R3df90e6e225a477b" /><Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="/docProps/app.xml" Id="rId1" /><Relationship Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="/package/services/metadata/core-properties/7a17c4849f0b4e2ca72c4a5b525d9f97.psmdcp" Id="R52c10e934b014bcf" /></Relationships>PK  LjwJϵ :   docProps/app.xml ¢ (  “MNÃ0…¯¼oB…P”¸ª  ¢,‘q&­Eb[ö4J¹ ŽÄp(iùìÆo¾Ì¼yR^žžãI]AÖI­2†$ %t&Õ2!kÌ'dÂbn¢Ôj%¸À¢\TaBVˆ&¢Ô‰”Ü =¡|3׶äèŸvIužKgZ¬KPHÃð˜fZ4ÓÜÍbcÀ‘·yÜüwÔƒl¶IëyjL!G»Âj§s fµ€"¦{ý†÷cç ÖVↅ-ÑWb.x§~ Ëyá e>´†8Þ„—ri‹+Œ*¨mpÏ4÷&¤âVr…$pòÑ?ǤÃ:µ­ ãв[mÜ ]L·b[öÙ~-ÇlÔ¾øìf]ò²àš«%üeÅá×+èöVÖƲ„ pWyÊ-~Mkà=˜#Òó:«¶Øw¹×:H­Tx7µÀ§:+Ÿnî¹ß3Kwþ ö PK  LjwJ>çÊ­a ÿ   xl/sharedStrings.xml ¢ (  ’ÉNÃ0†_eä h¢ U]$J…8ÐF¥H\Ýdšy ö8*¼QoÜ9 ‘âp‚âÂeäñ|³ýöÇëÛp²U*´N=bǽ˜êÔdBç#æistÎ&ãávàAj¼¦ 1ðZ<zœþ\„2Ú ¶#V•ƒ(riŠ»ž)Q‡ØÆXÅ)¸6\i‘g®@$%£“8îGŠ ÍÚ6¢±4ž‚äàL*êÕ»aÔû}£SÀÄ8â² ¹Å”Ð[ÈxJ¢{ªµ}XUS»¨ÄÖ;ý/ò’6›¤!ƒSù/Pí7"݈!×6О„Ž7˹n}”Â0£°Ø‰,æÓY²‚Ù}²€«åâ.é"ٍwè•;n r.%Z«3Xâ:Œ.[ïÙiòÖ͹ÍP;ÖUuUÔ;ûÔ½°^›®à2« ´Æ•á‰ƒbÍù ,jã+¸^Ái?~ùµßyü PK  LjwJ'J|2â ¼   xl/rels/workbook.xml.rels ¢ (  µ’ANÃ0E¯bÍžL(¨B¨n7lº¥½€åL⨉my¦´=‹©WÀ aÄ‚M6¶üÇóôÆòíýºÚœÇA½Qâ>x ÷U Š¼ Mï; Giïž³^½Ò$ß`×GV¹Å³'ŸÙ: W!’Ï•6¤ÑH>¦£±Ó.êz‰é'J¦Ú"ý‡Ú¶·ôìq$/€‘IÔì$å ÔÞ¤ŽDž‡²Te2¨m£!m›P8Ÿ‘\ú­2e…Ã㜧숤ÔøŽ?ß-o…ÐbN!ɽTÊLÑ×Zx,',þàúPK  LjwJ´ÁmêM z
 xl/styles.xml ¢ (  íVÛjÜ0ý¡÷F»†–²¬ÚC ¤¥ÙB_e{l«ÑÅHòÆ›ëC?©¿PÝì½Z¶”@¡û¢™ã9£¹IÚï¿­¯FÁÑ´aJæxy±Àd¥j&Û¶yñ]®Ç•±;÷€EŽ!ÍjÌqgm¿"ÄTj.TÒ}k”Ô:U·Äôhm<Mp’-¯ˆ LbïQ¢Ö J Òº­@—›:Ç.žèðZՐcŒÈåšÌdOi”<õâ!¿º¼ìÎZ‰¶”縤8“œ¸”ž"¼\& R\i¤Û2ÇE±¿ôERÑøšrVj–ð† ÆwñK6ÅwOB ‘q>‡˜á òkO­- §¢$ov½ËTª(Ùÿ–Ôjº[f/yI‘”J×®ÙÇåŠ ªm•¤üSÊ>©ïÔ£ô€·äÐXF!ø«²‘hïM4k»³ˆàm¬êÏá9󘑵JœCŒ o4å}{â$W¾œb¨|œß{Ÿ›“Q›Óy—³èú–Äè*)´ïùîn%è"œßʈú¡ØkokÏ §A€< |ÐÊBeãùõ3‚¸ª Æ«k“’›Ÿ¢Ï²+|rÜ—©O£Ecó,ÉÒÉuJ³'—¿‹Z )Çþf·¬ —pŒ,Œö£²4:q{=jÚo&ë°¡5pg´…›=ôe0–5»[jì­»Gf:ÍäÃFl¢Qÿx¼Ÿs!Ï2RÿËý‡å&óÈÝR'OÄŒ#ÿæøΗ–”½·L&-Ôý_ˆËPK  LjwJryNÎm ˆ   xl/worksheets/sheet.xml ¢ (  •ÝnÛ0 …Åðýbç? šmÒ´6¤h‡íZµéX¨,”Ü´}µ]ô‘ö £¥¤H:؝H::!ú÷¯÷ó‹—JEÏ€V=‹»4Ž@g&—z;‹W|™Äóó—éÎà“-\D´â,.«§Ib³a;¦Mß ƒ•pâ61E!3Xš¬©@»¤—¦£A G‡ÙRÖ6Þ«½üš­DîMTˆUB긵ç³wØ.Mã”Ôp‡‘mªJàë(³£ÚâCâ^nKçÉü<9ÙœKrÚÞE„PÌâËîtÝóØžú!agO¢¨½™GcžÚ`ÏâôDötÃÊ—CÖr(D£Ü½ÙÝÂÞÍðøœ¥p¢ Ðì" Þk¡-­¦g¾à¬M¶y" XŸž§íÙÏÞAöA^qd—#ÙãÈ%Gö9òš#¹âÈ!GÞpäˆ#o9rÌ‘kŽœœ‰ïÉQkzÿhM‘:c[sLêCkú)ÛÇ'ÛeÑ%‹²=¿fQ¶é«€vMìÒAg”ŽÓn2™°ÞüµevúýáÙ( &¬¡[îNØ׬õ>Û³äÓãªQj·©ý4ŠJƒòÍh'Ô‚f „§L£ÑÉìS²¤9D2œ»E™¥cOŸ„ž ÜJÒUP³´3¦÷á©‡À™ú°|4Ιª†ApƸŒ‡Gâàš:ªE ø ß rÙ®RrÑ2ô2¹Ùéï%è UGT$â0} CˆÎW"{ºÔùÏR:ðeä(¼í8Ê@©…©ÚNj£þˆ)Ê¥­•x…|o-X_yÏ!ãÄ£‚;ÎF™i´;”ùPK  LjwJu±‘^« »   xl/theme/theme.xml ¢ (  íYMÛDþ+#ß[ÇIœfWM«M6i¡ÝvµŠzœ8{š±Çš™ì67Ô‘qAâÆ•Z‰Eü˜…"(Òþ^;^{œŒ»ÙvEl‰gü¼ß~Ç9þ闫׆ !):–s¹f!y|L#¿cÍÔäRÛº~íÞT p$7qÇ ”Š7m[z°åe“îM¸±‚¥ðí±À‡À$dv½VkÙ!¦‘…"’Žuw2¡AÄ¥•3ï3øŠ”L6<&ö½T¢N‘bÇS'ù‘sÙcÖ±@ΘÉCe!†¥‚«–~,d_»jçTLUk„ƒôsB˜QŒ§õ”Pø£œÒ47®lê «À~¿ßë;ǁ=¬uVÀÍAÛéæ\5Ôâr•{¯æÖšKš„Æ ÁF·Ûu7ʍ‚ ¹BЮµš[õ2A³ pWmènõz­2[´VW6ZÍ%‚0MWàId‹å˜ g7ø6àÛy.0[Ë´ƒHUå]ˆp1 @e¬h„Ô<&쮇Ñ 8•€7 Öne{ž\ÝKÄ!é «Žõ~Œ¡@ Ìñ‹ïŽ_<CÇ/ž=z~ôèǣǏý¢¼‰#_§|õͧ}õúóÙׯž|^A u‚ß¾ÿøן?«@ùò‹§¿?úòËOþøö‰ ¿%ðHÇiH$ºCÑû "ÈHœ‘dZ"Á@MȾ JÈ;sÌŒÀ.)û𞀶DÞ˜=(黈™¢&ä­ ,!w8g].Ì6ÝJÅi6Í"¿B¾˜éÀ=ŒŒâ{KQîÏbÈljdÚ HIÕ]Ç>‰ˆBÉ=>%ÄDwŸÒ’w¨'¸ä…îSÔÅÔì˜!)3ÕMB€æF!ê%íÜC]ÎŒ¶ÉA ‚™‘)a%oÞÀ3…C³Ö8d:ô6VQÑý¹ðJŽ— ‚îÆQL¤4Ýó’Ê·0´(sì°yX† E§Fèm̹ÝæÓ^€Ãج7üžœBÆb´Ë•Y^®™d ÁQuäïQ¢ÎXìP?0'Krg&Nºz©?‡4z]³fºõE³^jÖ[ð3Ér‹®þGó6žE»$Iþ‹¾|Ñ—/úòk|ín\4[Ÿ«S†aå=¡Œí«9#·eÚº%è=ÀfºH‰ò¡>àòD^ è œ^#ÁÕ‡TûŽAŽ“ŠðeÆÛ—(æV%óôlJÁütÏÍ” Çj‡ûÒI3g”®|©‹j$,Ö׸ò¶âœrMyŽ[!Ï}½<[ó)ÔÂÉ›§UÏÔ”fdœx?ãps” ð˜d¡r̶8u}×>Ýuš¼ÆÛÊ['VºÀf•@÷<‚U[ –½Z,*¯Ð!(æÖ] y8îX¼à2Œ¡LZf~Ô±<•Ysjm/Û\‘ N­Ú撐XHµe° Koå/e¢Â„ºÛL؝ ¦þ´¦¶ó¯êa/G˜L&ÄS;Å2»ÇgŠˆý|ˆFl&ö0hÞ\dÙ˜Jx”ÔOêµ™%¹dõ°üê'«Ìâ g=ª­gÀŸ^çJ¤+M?»Bù7´¥qŽ¶¸ÿg[’ô…ñ¶1NÏa0Œ’<íX\¨€C?Šê L©0P Am¤-‹%¯°eɁÖÂL ÏÔõ‘ ÐõT ÙU™¥§psN:dV§¬ãä Ëxñ;"„ “"n%.°P·•Ì)p9p¶©ÆFþà]žŠšUSÑ)cC!ªy–)¥©?´gÃÆÛjqÆp½Â캻þ8†“ J¾ ‘Sá±bò=ÈT ’—ÚY)æ›#к­Û—ðúgg¬"íª¸Ÿëxªy¼QåñS¾¹Ç]ƒÃÝSüm¯¬­yÒÕÊß]|ô „oÙjÆ”ÌÞK=„Óiïäß”ÉL‰¯ý PK LjwJNUûýÚ Ú Q  package/services/metadata/core-properties/7a17c4849f0b4e2ca72c4a5b525d9f97.psmdcp ¢ (  <?xml version="1.0" encoding="utf-8"?>2017-03-23T16:06:15.7944240Z</dcterms:created>2017-03-23T16:06:15.7944240Z</dcterms:modified>PK LjwJ½g].5 5   [Content_Types].xml ¢ (  <?xml version="1.0" encoding="utf-8"?>PK-   LjwJ±ùS º  xl/workbook.xmlPK- LjwJÔ^S¨œ œ L _rels/.relsPK-   LjwJϵ ` :  - docProps/app.xmlPK-   LjwJ>çÊ­a ÿ  × xl/sharedStrings.xmlPK-   LjwJ'J|2â ¼  † xl/_rels/workbook.xml.relsPK-   LjwJ´ÁmêM z
¼ xl/styles.xmlPK-   LjwJryNÎm ˆ  P xl/worksheets/sheet.xmlPK-   LjwJu±‘^« »   xl/theme/theme.xmlPK- LjwJNUûýÚ Ú Q  package/services/metadata/core-properties/7a17c4849f0b4e2ca72c4a5b525d9f97.psmdcpPK- LjwJ½g].5 5  j [Content_Types].xmlPK

¾ ì

JoshClose commented 7 years ago

This might be easier... Can you write a short contained example that recreates the problem?

ootsun commented 7 years ago

So you would like me to give you my C# file but with only what matters for this case? I can give you that, yes :)

JoshClose commented 7 years ago

Yes. Kinda like a unit test.

ootsun commented 7 years ago
using System.Collections.Generic;
using CsvHelper;
using CsvHelper.Excel;
using System.IO;
using ClosedXML.Excel;

namespace ConsoleApplication2
{
    public partial class Program
    {
        static void Main(string[] args)
        {
            IDictionary<string, string> map = new Dictionary<string, string>();
            map.Add("1","Museums, art galleries, and botanical and zoological gardens");
            map.Add("2", "example");
            // Write the list into a xls file then save this file
            using (var workbook = new XLWorkbook(XLEventTracking.Disabled))
            {
                using (var writer = new CsvWriter(new ExcelSerializer(workbook)))
                {
                    writer.WriteRecords(map);
                }
                workbook.SaveAs(@"C:\Users\gabriel\Documents\Mes notes\file2.xlsx", false);
            }
        }
    }
}
ootsun commented 7 years ago

image I got this :/

ootsun commented 7 years ago

It seems to be the comas that causes the problem. How can I do to write "," in my file but not the """" around my string?

JoshClose commented 7 years ago

What is ExcelSerializer?

ootsun commented 7 years ago

I don't really know what this is. I found this solution on the Internet. How would u create a new xlsx file?

JoshClose commented 7 years ago

You may need to contact the CsvHelper.Excel project and ask them this question. That serializer is what is doing the writing.

ootsun commented 7 years ago

Ok ;) Thnaks

christophano commented 7 years ago

In case anyone else has trouble with this the solution (as noted in christophano/CsvHelper.Excel#6) it was fixed by setting writer.Configuration.QuoteNoFields = true;