Prev: How to extract/integrate strings from Dialog and StringTablein a DLL?
Next: Adding Reference to Excel - which one?
From: GiJeet on 22 Apr 2010 10:33 Hello, I have a winforms app with some values in the app.config file I use to create a connection string to excel. Some of the values require quotes around the values within the connection string. Here is an example of the connection string: string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=Book1.xls;Extended Properties=""Excel 8.0;HDR=YES;"""; Note the quotes around the Extended Properties values. I have the following String.Format code (which doesnt work): sConnStr = String.Format( "Provider={0};Data Source={1};Extended Properties={2};HDR={3};", Provider, FilePath, ExtendedProperties, SheetHeader ); Where Provider, FilePath, ExtendedProperties, SheetHeader are all vars holding the values from the app.config. However there are no quotes around the Extended Properties values. I tried to escape it but it produces an error: sConnStr = String.Format( "Provider={0};Data Source={1};Extended Properties=/{2};HDR={3};/", Provider, FilePath, ExtendedProperties, SheetHeader ); I even tried to put the quotes around the values in the app.config but that threw an error also. Heres the app.config section. <appSettings> <add key="BSFileName" value="C:\_ExcelReports\BalanceSheet.xlsx"/> <add key="BSProvider" value="Microsoft.ACE.OLEDB.12.0"/> <add key="BSExtendedProperties" value="Excel 12.0 Xml"/> <add key="BSHeader" value="NO"/> <add key="BSSheetName" value="Sheet1"/> <add key="BSCell_TotalAssets" value="B4"/> </appSettings> So, how to have quotes around values in a String.Format function? Help is appreciated. You can even combine the ExtendedProperties & SheetHeader into one variable. Thanks G
From: Alberto Poblacion on 22 Apr 2010 10:54 "GiJeet" <gijeet(a)yahoo.com> wrote in message news:d45de0b8-23e4-4301-aab5-33fe3cfea736(a)h27g2000yqm.googlegroups.com... > I tried to escape it but it produces an error: > sConnStr = String.Format( > "Provider={0};Data Source={1};Extended > Properties=/�{2};HDR={3};/�", > Provider, FilePath, ExtendedProperties, SheetHeader > ); You have used the wrong escape character. It should be \ rather than /. Also, be careful with the double-quote character. The text you pasted in the message contains � instead of " (they may look almost the same, depending on the font that you are using, but they are different characters). > I even tried to put the quotes around the values in the app.config but > that threw an error also. > [...] > <add key="BSExtendedProperties" value="Excel 12.0 Xml"/> If you want to insert quotes in the xml file, you can use " <add key="BSExtendedProperties" value=""Excel 12.0 Xml""/>
From: GiJeet on 22 Apr 2010 11:21 Thanks, I'll fix the code and try the html quotes.
From: Arne Vajhøj on 22 Apr 2010 21:05
On 22-04-2010 10:33, GiJeet wrote: > I tried to escape it but it produces an error: > sConnStr = String.Format( > "Provider={0};Data Source={1};Extended > Properties=/�{2};HDR={3};/�", > Provider, FilePath, ExtendedProperties, SheetHeader > ); Besides using the correct escape \ and the correct quote ", then you may find using @ more readable. sConnStr = String.Format("Provider={0};Data Source={1};Extended Properties=\"{2};HDR={3};\"", Provider, FilePath, ExtendedProperties, SheetHeader); vs. sConnStr = String.Format(@"Provider={0};Data Source={1};Extended Properties=""{2};HDR={3};""", Provider, FilePath, ExtendedProperties, SheetHeader); Arne |