首页 文章

在读取Excel工作表时,如果列的值是numberic,那么它在datatable中返回null

提问于
浏览
1

我正在从Excel工作表中读取值 . 列通常包含 String ,但有时它可能包含数值 . 将Excel工作表读入数据表数值时,读取为空白 .

它将90004读为 null ,但是如果我按数字对该列进行排序,它会读取数值并将字符串值作为 null ,如果我按字符串对此列进行排序,则它会读取字符串值并将数字设为null .

AC62614 abc     EA  MISC
AC62615 pqr     EA  MISC
AC62616 xyz     EA  MISC
AC62617 test    EA  90004
AC62618 test3   TO  MISC
AC62619 test3   TO  STEEL

我的代码:

public static DataTable ReadExcelFile(FileUpload File1, string strSheetName)
    {
        string strExtensionName = "";
        string strFileName = System.IO.Path.GetFileName(File1.PostedFile.FileName);
        DataTable dtt = new DataTable();
        if (!string.IsNullOrEmpty(strFileName))
        {
            //get the extension name, check if it's a spreadsheet
            strExtensionName = strFileName.Substring(strFileName.IndexOf(".") + 1);
            if (strExtensionName.Equals("xls") || strExtensionName.Equals("xlsx"))
            {
                /*Import data*/
                int FileLength = File1.PostedFile.ContentLength;
                if (File1.PostedFile != null && File1.HasFile)
                {

                    //upload the file to server
                    //string strServerPath = "~/FolderName"; 
                    FileInfo file = new FileInfo(File1.PostedFile.FileName);
                    string strServerFileName = file.Name;
                    string strFullPath =     HttpContext.Current.Server.MapPath("UploadedExcel/" + strServerFileName);
                    File1.PostedFile.SaveAs(strFullPath);

                    //open connection out to read excel
                    string strConnectionString = string.Empty;
                    if (strExtensionName == "xls")
                        strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
                                                + strFullPath
                                                + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
                    else if (strExtensionName == "xlsx")
                        strConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
                                                + strFullPath
                                                + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";

                    if (!string.IsNullOrEmpty(strConnectionString))
                    {
                        OleDbConnection objConnection = new OleDbConnection(strConnectionString);
                        objConnection.Open();
                        DataTable oleDbSchemaTable = objConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                        bool blExists = false;
                        foreach (DataRow dtr in oleDbSchemaTable.Rows)
                        {
                            //reads from the spreadsheet called 'Sheet1'
                            if (dtr["TABLE_NAME"].ToString() == "" + strSheetName + "$")
                            {
                                blExists = true;
                                break;
                            }
                        }
                        if (blExists)
                        {
                            OleDbCommand objCmd = new OleDbCommand(string.Format("Select * from [{0}$]", strSheetName), objConnection);
                            OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
                            objAdapter1.SelectCommand = objCmd;
                            DataSet objDataSet = new DataSet();
                            objAdapter1.Fill(objDataSet);
                            objConnection.Close();
                            dtt = objDataSet.Tables[0];
                        }

                    }
                }
            }
        }
        return dtt;
    }

1 回答

  • 2

    如果在连接字符串中将IMEX = 2更改为IMEX = 1,则列将被解释为文本 . 然后,您可以获取工作表的所有数据,如果值是数字,则使用Int32.TryParse()进行检查 .

相关问题