C# Excel表格数据导入+表格中的空白行去除

问题一:Excel表格数据导入

解决办法:

简明扼要,直接粘贴代码

....此处省略代码万万行....

 list.Add("create table [" + user + "ZJWEEKPLAN]" + "(cinvcode nvarchar(200)");
                SqlDBHelper.ExecuteSqlTran(list);
                string str = "", resultFile = "";
                System.Windows.Forms.OpenFileDialog openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
                openFileDialog1.InitialDirectory = "C:";//打开C盘
                openFileDialog1.Filter = "Excel 工作簿(*.xlsx)|*.xlsx|Excel 97-2003工作簿(*.xls)|*.xls";

                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    resultFile = openFileDialog1.FileName;//获取到文件名
                }
                if (!string.IsNullOrEmpty(resultFile))
                {
                    DataTable dt = ExcelHelper.ReadFromExcel(resultFile);//写到中间表
                  }
public static string restosql = "";
        public static void DataTableToSQLServer(DataTable dt, string connectString, string tableName)
        {
            string connectionString = connectString;

            using (SqlConnection destinationConnection = new SqlConnection(connectionString))
            {
                destinationConnection.Open();

                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection))
                {
                    try
                    {
                        bulkCopy.DestinationTableName = tableName;//要插入的表的表名
                        bulkCopy.BatchSize = dt.Rows.Count;
                        bulkCopy.ColumnMappings.Add("产品编码", "cinvcode");//映射字段名 DataTable列名 ,数据库 对应的列名  
                        
                        bulkCopy.WriteToServer(dt);
                        restosql = "success";
                    }
                    catch (Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show("提示:"+ex.Message,"提示");
                    }
                    
                }
            }

问题二:表格里面删除了行,看起来空白没数据。。导入做了空白校验报错

解决办法:去除空行。将刚刚导入好的DT表格走一遍这个方法

        /// <summary>
        /// 移除空行
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        protected DataTable RemoveEmpty(DataTable dt)
        {
            List<DataRow> removelist = new List<DataRow>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                bool rowdataisnull = true;
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (!string.IsNullOrEmpty(dt.Rows[i][j].ToString().Trim()))
                    {

                        rowdataisnull = false;
                    }
                }
                if (rowdataisnull)
                {
                    removelist.Add(dt.Rows[i]);
                }

            }
            //移除空行
            for (int i = 0; i < removelist.Count; i++)
            {
                dt.Rows.Remove(removelist[i]);
            }
            return dt;
        }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YTH.IT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值