C#问题与excel的oledb连接字符串

问题描述 投票:1回答:2

我收到一个像“c:\ test \ abc.xlsx”这样的字符串,它表示我的excel路径。我必须依靠我收到的东西,不能硬编码。现在该如何确保单个“\”被转义并变为“\”

string **PATH** = "c:\test\abc.xlsx"
string conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=**PATH**;Extended Properties=Excel 12.0;";
c# oledb
2个回答
1
投票

单个“\”似乎没有正确转义。

尝试:string path = @"c:\test\abc.xlsx";

领先的@符号将为您正确地逃脱它


1
投票

应该很简单:

String thePath = "c:\\test\\abc.xlsx"
String conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + thePath + ";Extended Properties=Excel 12.0;";
© www.soinside.com 2019 - 2024. All rights reserved.