删除Datatable中的空行c#

时间:2017-03-14 02:54:35

标签: c# database

我正在使用VS 2013 c#和asp.net, 一些行是空白的,这些行我想删除

见下面的图片

enter image description here

我尝试了这段代码,但没有用,任何想法

public List<ItemModel> ReadExcel(string Path, string Ext)
        {
            var FileContent = new List<ItemModel>();
            FileStream stream = null;
            stream = new FileStream(Path, FileMode.Open, FileAccess.Read);


            if (Ext == ".xls")
            {
                //1. Reading from a binary Excel file ('97-2003 format; *.xls)
                var Reader = ExcelReaderFactory.CreateBinaryReader(stream);

                //3. DataSet - The result of each spreadsheet will be created in the result.Tables
                DataSet result = null;

                result = Reader.AsDataSet();
                //4. DataSet - Create column names from first row
                // excelReader.IsFirstRowAsColumnNames = true;

                if (result.Tables[0].Rows.Count != 0)
                {
                    for (var x = 0; x < result.Tables[0].Rows.Count; x++)
                    {
                        if (x > 2)
                        {
                            var row = result.Tables[0].Rows[x];

                            var item = new ItemModel
                            {
                                INVNO = "",
                                
                                
                                INVORG =row[0] == null? "" : Convert.ToString(row[0]),
                                ITEM = Convert.ToString(row[1]),
                                ITEMTYPE = Convert.ToString(row[2]),
                                SUBINVCODE = Convert.ToString(row[3]),
                                LOCATORS = row[4] == null ? "" : Convert.ToString(row[4]),
                                LOTNO = row[5] == null ? "" : Convert.ToString(row[5]),
                                COUNTRYOFORIGINCODE = Convert.ToString(row[6])
                            };
                            // modified
                            // modified                         
                            //  item.EXPIRATIONDATE = row[7] == null ? (DateTime?)null : Convert.ToDateTime(Convert.ToString(row[7]));
                            var expdate = Convert.ToString(row[7]);
                            if (expdate == "")
                            {
                                item.EXPIRATIONDATE = null;
                            }
                            else
                            {
                                item.EXPIRATIONDATE = Convert.ToDateTime(row[7]);
                                item.EXPIRATIONDATE = item.EXPIRATIONDATE;
                            }

                            var onhand = Convert.ToString(row[8]);
                            if (row[8] != System.DBNull.Value)
                                {
                                   decimal quantity; 
                                   if(decimal.TryParse(row[8].ToString(), out quantity))
                                   {
                                      item.ONHANDQTY = quantity;
                                   } 
                                }



                            item.ISCONSIGNED = Convert.ToString(row[9]);
                            item.OWNINGORGANIZATION = Convert.ToString(row[10]);

                            var itemcost = Convert.ToString(row[11]);
                            if (row[11] != System.DBNull.Value)
                            {
                                decimal cost;
                                if (decimal.TryParse(row[11].ToString(), out cost))
                                {
                                    item.ITEMCOST = cost;
                                }
                            }

                            var invcost = Convert.ToString(row[12]);
                            if (row[12] != System.DBNull.Value)
                            {
                                decimal invcosty;
                                if (decimal.TryParse(row[12].ToString(), out invcosty))
                                {
                                    item.INVCOST = invcosty;
                                }
                            }


                            item.PLANNERCODE = Convert.ToString(row[13]);
                            item.COMMODITYCODE = Convert.ToString(row[14]);
                            item.COMMCODEDESC = Convert.ToString(row[15]);
                            item.PRIMARYUOMCODE = Convert.ToString(row[16]);
                            item.MINMINMAXQTY = Convert.ToString(row[17]) == "" ? 0 : Convert.ToDecimal(row[17]);
                            item.MAXMINMAXQTY = Convert.ToString(row[18]) == "" ? 0 : Convert.ToDecimal(row[18]);


                            if (Convert.ToString(row[19]) == null)
                            {
                                item.HAZARDOUZITEMFLG = "";
                            }
                            else
                            {
                                item.HAZARDOUZITEMFLG = Convert.ToString(row[19]);
                            }
                            if (Convert.ToString(row[20]) == null)
                            {
                                item.DESCRIPTION = "";
                            }
                            else
                            {
                                item.DESCRIPTION = Convert.ToString(row[20]);
                            }

                            item.ITEMDESCRIPTION = Convert.ToString(row[21]);
                            item.INVENTORYITEMSTATUSCODE = Convert.ToString(row[22]);

                            var ORIGINALDATERECIEVE = Convert.ToString(row[23]);
                            if (ORIGINALDATERECIEVE == "")
                            {
                                item.ORIGINALDATERECIEVE = null;
                            }
                            else
                            {
                                item.ORIGINALDATERECIEVE = Convert.ToDateTime(row[23]);
                            }

                            //item.NBROFDAYSSINCERECPT = Convert.ToInt16(row[24]);

                            item.NBROFDAYSSINCERECPT = Convert.ToString(row[24]) == "" ? 0 : Convert.ToDecimal(row[24]);

                            //item.DATERCVDINSUBINV = Convert.ToDateTime(row[25]);
                            var DATERCVDINSUBINV = Convert.ToString(row[25]);
                            if (DATERCVDINSUBINV == "")
                            {
                                item.DATERCVDINSUBINV = null;
                            }
                            else
                            {
                                item.DATERCVDINSUBINV = Convert.ToDateTime(row[25]);
                            }

                           



                            FileContent.Add(item);
                        }
                    }
                }
                Reader.Close();

1 个答案:

答案 0 :(得分:1)

在将项目添加到FileContent之前检查条件'!String.IsNullOrEmpty'。