标题:大侠们:走过路过,千万不要错过。帮帮忙!
只看楼主
hamyapin
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-2-8
结帖率:0
已结贴  问题点数:10 回复次数:5 
大侠们:走过路过,千万不要错过。帮帮忙!
winform中上传Excle2003和Excle2010的差别是什么?目前我只能上传2003,如果上传Excle2010代码有哪些变化???求救与各位大侠!!
搜索更多相关主题的帖子: 2003 
2012-02-28 13:51
sailorli123
Rank: 1
等 级:新手上路
帖 子:6
专家分:7
注 册:2012-2-20
得分:5 
没遇到这种问题
2012-02-28 15:52
yimaoqian198
Rank: 4
等 级:业余侠客
帖 子:108
专家分:287
注 册:2010-5-10
得分:5 
把你写的代码贴上来,没有代码不知道从哪方面解决
2012-02-28 16:14
hamyapin
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-2-8
得分:0 
回复 3楼 yimaoqian198
//把excel数据读入dataset返回l数据集
        private DataSet xsldata()
        {
            OpenFileDialog openFile = new OpenFileDialog();//打开文件对话框。
            openFile.Filter = ("Excel 文件(*.xls)|*.xls");//后缀名。
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                string filename = openFile.FileName;
                int index = filename.LastIndexOf("\\");//截取文件的名字
                filename = filename.Substring(index + 1);

                //这是读取excel链接,目前只支持2003
                string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + openFile.FileName + ";Extended Properties='Excel 8.0;IMEX=1'";
                System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(strCon);
                string strCom = "SELECT * FROM [sheet1$]";
                Conn.Open();
                System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, Conn);
                DataSet ds = new DataSet();
                myCommand.Fill(ds, "[sheet1$]");
                Conn.Close();
                return ds;
            }
            else
                return null;

        }

        #region 上传客户资料
        /// <summary>
        /// 上传客户资料
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 上传客户信息表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //将读取excel后的数据转换成dataset
            DataSet ds = xsldata();
            if (ds != null)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    using (SqlConnection conn = new SqlConnection(DBhelper.constring))
                    {

                        StringBuilder bu = new StringBuilder();
                        bu.AppendLine("insert LenovoKeHuInfo (number,C_name,L_area,L_name,zhiwei,phone1,phone2,QQorE_mail,zhuyingleirong,hezuoyixiang,yonghudengji,beizhu)");
                        bu.AppendLine("values");
                        bu.AppendFormat("('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}')",
                        ds.Tables[0].Rows[i][0],
                        ds.Tables[0].Rows[i][1],
                        ds.Tables[0].Rows[i][2],
                        ds.Tables[0].Rows[i][3],
                        ds.Tables[0].Rows[i][4],
                        ds.Tables[0].Rows[i][5],
                        ds.Tables[0].Rows[i][6],
                        ds.Tables[0].Rows[i][7],
                        ds.Tables[0].Rows[i][8],
                        ds.Tables[0].Rows[i][9],
                        ds.Tables[0].Rows[i][10],
                        ds.Tables[0].Rows[i][11]
                        );
                        SqlCommand comm = new SqlCommand(bu.ToString(), conn);
                        conn.Open();
                        comm.ExecuteNonQuery();
                    }
                }
            }
        }
        #endregion
麻烦帮忙看一下,,谢谢
2012-02-28 22:18
yimaoqian198
Rank: 4
等 级:业余侠客
帖 子:108
专家分:287
注 册:2010-5-10
得分:0 
把这句string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + openFile.FileName + ";Extended Properties='Excel 8.0;IMEX=1'";
改为 string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = " + openFile.FileName  + ";Extended Properties ='Excel 12.0'";
2012-02-29 09:24
hamyapin
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-2-8
得分:0 
回复 5楼 yimaoqian198
comm.ExecuteNonQuery();
这一句报了一个异常,是什么原因啊 。。。。
2012-03-01 13:23



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-361771-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.241676 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved