>此解决方案使用schema.ini文件进行精确控制。 以下代码演示了此方法:
<code class="language-csharp">using System.Data; using System.Data.OleDb; using System.Globalization; using System.IO; static DataTable LoadCsvIntoDataTable(string filePath, bool isFirstRowHeader) { string header = isFirstRowHeader ? "Yes" : "No"; string directory = Path.GetDirectoryName(filePath); string fileName = Path.GetFileName(filePath); string sqlQuery = $"SELECT * FROM [{fileName}]"; using (OleDbConnection connection = new OleDbConnection( $"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={directory};Extended Properties=\"Text;HDR={header}\"")) using (OleDbCommand command = new OleDbCommand(sqlQuery, connection)) using (OleDbDataAdapter adapter = new OleDbDataAdapter(command)) { DataTable dataTable = new DataTable(); dataTable.Locale = CultureInfo.CurrentCulture; adapter.Fill(dataTable); return dataTable; } }</code>> file提供的配置。
>
以上是如何有效地将CSV文件加载到.NET DATATABLE并将数字值作为文本处理?的详细内容。更多信息请关注PHP中文网其他相关文章!