• winform导入excel或者csv


    if (txt01.Text != "")
    {
    this.lbzhantie.Items.Clear();
    this.dtzhuanhuo.Rows.Clear();
    if (txt01.Text.Contains(".xls"))
    {
    try
    {
    string strConn;
    // strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txt01.Text + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
    strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + txt01.Text + ";" + "Extended Properties='Excel 12.0;HDR=False;IMEX=1';";
    OleDbConnection OleConn = new OleDbConnection(strConn);
    OleConn.Open();
    String sql = "SELECT * FROM [Sheet1$]";

    OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
    DataSet OleDsExcle = new DataSet();
    OleDaExcel.Fill(OleDsExcle, "Sheet1");
    // OleConn.Close();

    DataTable dt = OleDsExcle.Tables["Sheet1"];
    //增加到表中
    int count = 0;
    this.lbzhantie.Items.Clear();
    for (int i = 0; i < dt.Rows.Count; i++)
    {
    if (dt.Rows[i][18].ToString() == "3")
    {

    //if (dt.Rows[i][3].ToString().Contains(","))
    //{
    // string[] upscode = dt.Rows[i][3].ToString().Split(',');
    // foreach (string j in upscode)
    // {
    // this.lbzhantie.Items.Add(dt.Rows[i][2] + " " + j);


    // }

    //}
    //else
    //{
    // this.lbzhantie.Items.Add(dt.Rows[i][2] + " " + dt.Rows[i][3].ToString());
    //}
    this.lbzhantie.Items.Add(dt.Rows[i][1].ToString());
    lbcount.Visible = true;
    lbcount.Text = "Paste: " + (lbzhantie.Items.Count);

    }

    if (dt.Rows[i][2].ToString() != "" && dt.Rows[i][2] != null)
    {
    string line = dt.Rows[i][5].ToString().ToUpper();
    //if (line == "C")
    //{
    // line = "D";
    //}
    int num = Convert.ToInt32(alb.SelectAWBList1234(dt.Rows[i][1].ToString()));
    if (num == 0)
    {
    //新增客编、备注、运单状态
    SqlServerHelper sqlhelper1 = new SqlServerHelper();
    string sqlstr = string.Format("insert into test() values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}')",
    dt.Rows[i][1],
    dt.Rows[i][2],
    dt.Rows[i][10],
    dt.Rows[i][11],
    dt.Rows[i][16],
    line,
    dt.Rows[i][6].ToString().Replace("'", ""),
    dt.Rows[i][7],
    dt.Rows[i][4],
    dt.Rows[i][12],
    dt.Rows[i][13],
    dt.Rows[i][9],
    dt.Rows[i][3].ToString().Replace(",", ","),
    dt.Rows[i][0],
    dt.Rows[i][17],
    dt.Rows[i][18],
    dt.Rows[i][19],
    dt.Rows[i][20].ToString().Trim(),
    dt.Rows[i][15],
    dt.Rows[i][6].ToString().Replace("'", ""));

    sqlhelper1.ExecuteCommand(sqlstr.ToString());
    count++;
    }
    // string test = dt.Rows[i][17].ToString();

    }
    }
    MessageBox.Show("总共" + Convert.ToInt32(dt.Rows.Count - 1) + "条数据 " + count + "条上传成功 " + Convert.ToInt32(dt.Rows.Count - 1 - count) + "条重复记录");
    }
    catch (Exception err)
    {
    MessageBox.Show("数据绑定Excel失败!失败原因:" + err.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

    }
    }
    else
    {
    int js = 0;
    string alldata = "";
    using (System.IO.StreamReader sr = new System.IO.StreamReader(txt01.Text, System.Text.Encoding.Default))
    {
    alldata = sr.ReadToEnd();

    }

    if (alldata.Length > 10)
    {
    string[] linedataS = null;
    string linedata = "";

    linedataS = alldata.Split(' ');
    SqlServerHelper sqlhelper = new SqlServerHelper();
    for (int j = 1; j < linedataS.Length - 1; j++)
    {
    linedata = linedataS[j];

    string[] sCol = linedata.Split(',');
    if (sCol[18].ToString() == "3")
    {

    this.lbzhantie.Items.Add(sCol[1].ToString());
    lbcount.Visible = true;
    lbcount.Text = "count: " + (lbzhantie.Items.Count);

    }
    string test = sCol[1].ToString();
    if (sCol[2] != "" && sCol[2] != null)
    {
    string line = sCol[5].ToUpper();
    int count = Convert.ToInt32(alb.SelectAWBList1234(sCol[1]));
    if (count == 0)
    {
    SqlServerHelper sqlhelper1 = new SqlServerHelper();
    try
    {
    //StringBuilder sql = new StringBuilder(200);
    StringBuilder sql = new StringBuilder(500);
    sql.Append("insert into test() values(");

    sql.Append("'");
    sql.Append(sCol[1]);

    sql.Append("','");
    sql.Append(sCol[2]);

    sql.Append("','");
    sql.Append(sCol[10].Replace("'",""));

    sql.Append("','");
    sql.Append(sCol[11]);

    sql.Append("','");
    sql.Append(sCol[16]);

    sql.Append("','");
    sql.Append(line);

    sql.Append("','");
    sql.Append(sCol[6].ToString().Replace("'", "‘"));

    sql.Append("','");
    sql.Append(sCol[7]);

    sql.Append("','");
    sql.Append(sCol[4]);

    sql.Append("','");
    sql.Append(sCol[12]);

    sql.Append("','");
    sql.Append(sCol[13]);

    sql.Append("','");
    sql.Append(sCol[9]);

    sql.Append("','");
    sql.Append(sCol[3].ToString().Replace(",", ","));

    sql.Append("','");
    sql.Append(sCol[0]);
    sql.Append("','");
    sql.Append(sCol[17]);

    sql.Append("','");
    sql.Append(sCol[18]);

    sql.Append("','");
    sql.Append(sCol[19]);

    sql.Append("','");
    sql.Append(sCol[20].ToString().Trim());

    sql.Append("','");
    sql.Append(sCol[15]);

    sql.Append("','");
    sql.Append(sCol[6].ToString().Replace("'", "‘"));

    sql.Append("')");

    string temp = sql.ToString();
    sqlhelper1.ExecuteCommand(sql.ToString());
    js++;

    }
    catch (Exception ex)
    {
    continue;
    }
    }
    }

    }
    }
    MessageBox.Show(js + "条上传成功");

    }

    }
    else
    {
    MessageBox.Show("Please choose excel file!");
    }

  • 相关阅读:
    golang操作redis简单例子
    运行gin官方样例
    golang读写yaml
    Pagehelper分页插件Mybatis
    博客园右下角小人动画
    黑客窃取马来西亚用户的银行数据
    微软获得法院命令删除用于攻击乌克兰的域名
    MyEclipse中Lombok的安装及使用
    SVN安装及IDEA集成SVN
    TNS:listener does not currently know of service requested in connect descripto
  • 原文地址:https://www.cnblogs.com/panjuan/p/4757660.html
Copyright © 2020-2023  润新知