ASP.NET、C#、MVC 3、代码优先项目
我正在尝试从 Excel 电子表格中导入数据。我已将所有单元格格式化为文本。
导入工作表中的示例行如下。
Account Card ThreeCode Route
04562954830287127 32849321890233127 183 154839254
04562954830287128 32849321890233128 233
04562954830287129 32849321890233129 082
04562954830287130 32849321890233130 428
当我在调试中运行并深入了解 ds DataSet 时,Account 和 Card 列作为字符串导入,3-Digit 和 Route 列作为双精度导入。问题出现在数据行 3 中以 0 (082) 开头的 3 位数字。它作为 System.DBNull 导入并且为空。我需要能够导入带前导零的 3 位代码。有没有办法强制导入为所有字符串或解决此问题的另一种方法?我在网上搜索并没有找到解决方案。这将从浏览器运行,因此与本地计算机上的注册表、dll 或 ini 文件无关。导入代码如下。预先感谢您的任何帮助。
public ActionResult ExcelToDS(string Path = "C;\File.xls")
{
string strConn = "Provider= Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + "; " + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open(); string strExcel = "";
OleDbDataAdapter myCommand = null;
DataTable dt = new DataTable();
strExcel = "select * from [Import$]";
DataSet ds = null;
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet(); myCommand.Fill(ds, "table1");