当前位置:编程学习 > C#/ASP.NET >>

对EXCLE进行操作~出现语法问题!!求解

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;

namespace chouyang
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //定义连接excel的公用函数,参数是excel文件路径
        public OleDbConnection Con(string xlsPath)
        {
            //定义连接excel文件的字符串,字符串包含配置信息,"Provider = MicroSoft.Jet.OLEDB.4.0"表示打开OleDb的版本,
            //"Data Source"表示数据源,这里需要填写excel的路径,";Extended Properties = \"Excel 8.0;"指示打开excel的版本为03即后缀名为.xls的文件
            //"HDR = Yes;"表示所打开的excel第一行是标题,不做为数据使用
            string constr = "Provider = MicroSoft.Jet.OLEDB.4.0;Data Source =" + xlsPath + ";Extended Properties = \"Excel 8.0; HDR = Yes;\";";
            //定义打开excel数据源的连接
            OleDbConnection con = new OleDbConnection(constr);
            //返回OleDbConnection对象的实例
            return con;
        }
        //open按钮操作
        private void button1_Click(object sender, EventArgs e)
        {
            //创建OpenFileDialog实例ofd
            OpenFileDialog ofd = new OpenFileDialog();
            //初始化打开文件目录
            ofd.InitialDirectory = @"D:\excel";
            //过滤除excel文件以外的文件
            ofd.Filter = "excel|*.xls";
            //定义选择打开文件之后的操作
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                //将选择的文件路径赋值给定义的textBox1
                textBox1.Text = ofd.FileName;
                //使用定义好的公用方法Con创建excel数据源的连接
                OleDbConnection con = Con(textBox1.Text);
                //打开数据源excel连接
                con.Open();
                //创建DataTable对象,将excel的sheet信息存入DataTable对象
                //GetOleDbSchemaTable方法可以得到excel表中的sheet信息,OleDbSchemaGuid.Tables是GetOleDbSchemaTable方法的参数
                //获取excel表的框架信息
                DataTable dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                //将DataTabe对象dt赋值给下拉菜单comboBox1的数据源
                //dataGridView1.DataSource = dt;
                comboBox1.DataSource = dt;
                //定义下拉菜单comboBox1显示为excel的sheet名称
                comboBox1.DisplayMember = "TABLE_NAME";
                //定义下拉菜单comboBox1的值为excel的sheet名称
                comboBox1.ValueMember = "TABLE_NAME";
                //创建用于填充数据的适配器OleDbDataAdapter
                //获取指定工作薄中的,满足Select * From [" + comboBox1.SelectedValue + "],条件的数据内容
                OleDbDataAdapter oda = new OleDbDataAdapter("Select * From [" + comboBox1.SelectedValue + "]", con);
                //创建DataTable对象用于接收数据
                DataTable ndt = new DataTable();
                //将excel中的数据填充到DataTable对象dt中
                oda.Fill(ndt);
                //将dt的数据显示在dataGridView1中
                dataGridView1.DataSource = ndt;
            }
        }
        //Begin按钮操作
        private void button2_Click(object sender, EventArgs e)
        {
            OleDbConnection con = Con(textBox1.Text);
            OleDbCommand cmd = new OleDbCommand("", con);
            int begin = Convert.ToInt32(textBox3.Text);
            int end = Convert.ToInt32(textBox4.Text);
            int count = Convert.ToInt32(textBox2.Text);
            int[] arr = new int[count];
            RandomKDiffer(begin, end, count, arr);
            string insertRow;
            for (int i = 0; i < count; i++)
            {
                insertRow = "select * from [序号$] where  A= arr[i]"; 
                cmd.CommandText = insertRow;
                //打开数据源excel连接
                con.Open();
                //执行插入操作
                cmd.ExecuteNonQuery();
            }
            //关闭数据源连接
            con.Close();
        }
        //随机抽样函数
        private void RandomKDiffer(int n, int m, int k, int[] arrayK)
        {
            int i = 0;
            int a, j;
            Random random = new Random();
            while (i < k)
            {
                a = random.Next(m - n + 1) + n;
                for (j = 0; j < i; j++)
                {
                    if (a == arrayK[j])
                    {
                        break;
                    }
                }
                if (j == i)
                {
                    arrayK[i] = a;
                    i++;
                }
            }
        }
    }
}

对EXCLE进行随机抽样出现“System.Data.OleDb.OleDbException”类型的未经处理的异常在 System.Data.dll 中发生 
其他信息: 语法错误 (操作符丢失) 在查询表达式 'A= arr[i]' 中。
刚入门!!求教 C# EXCLE 抽样 语法 --------------------编程问答-------------------- insertRow = "select * from [序号$] where  A=“+ arr[i]; 

  
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/ --------------------编程问答--------------------
Quote: 引用 1 楼 feiyun0112 的回复:

insertRow = "select * from [序号$] where  A=“+ arr[i]; 

 修改后的问题:'序号$' 不是一个有效名称。请确认它不包含无效的字符或标点,且名称不太长。
但是在EXCLE表中有序号这一单元名 --------------------编程问答--------------------
引用 2 楼 Lkyzlwj 的回复:
Quote: 引用 1 楼 feiyun0112 的回复:

insertRow = "select * from [序号$] where  A=“+ arr[i]; 

 修改后的问题:'序号$' 不是一个有效名称。请确认它不包含无效的字符或标点,且名称不太长。
但是在EXCLE表中有序号这一单元名


你确定有 序号 这个这个sheet?你看看有没有空格 --------------------编程问答--------------------
引用 3 楼 happy09li 的回复:
Quote: 引用 2 楼 Lkyzlwj 的回复:

Quote: 引用 1 楼 feiyun0112 的回复:

insertRow = "select * from [序号$] where  A=“+ arr[i]; 

 修改后的问题:'序号$' 不是一个有效名称。请确认它不包含无效的字符或标点,且名称不太长。
但是在EXCLE表中有序号这一单元名


你确定有 序号 这个这个sheet?你看看有没有空格


嗯~这个我大意了~
修改后报错:至少一个参数没有被指定值? --------------------编程问答-------------------- 将insertRow = "select * from [序号$] where  A= arr[i]"; 
改为insertRow = "select * from [cheet1$] where  A=" + arr[i];  --------------------编程问答--------------------
引用 4 楼 Lkyzlwj 的回复:
Quote: 引用 3 楼 happy09li 的回复:

Quote: 引用 2 楼 Lkyzlwj 的回复:

Quote: 引用 1 楼 feiyun0112 的回复:

insertRow = "select * from [序号$] where  A=“+ arr[i]; 

 修改后的问题:'序号$' 不是一个有效名称。请确认它不包含无效的字符或标点,且名称不太长。
但是在EXCLE表中有序号这一单元名


你确定有 序号 这个这个sheet?你看看有没有空格


嗯~这个我大意了~
修改后报错:至少一个参数没有被指定值?


你怎么改的? --------------------编程问答--------------------
引用 5 楼 Lkyzlwj 的回复:
将insertRow = "select * from [序号$] where  A= arr[i]"; 
改为insertRow = "select * from [cheet1$] where  A=" + arr[i]; 





insertRow = "select * from [cheet1$] where  A=" + arr[i]; 

是sheet1  吧 --------------------编程问答-------------------- 你确定有 序号 这个这个sheet?你看看有没有空格


嗯~这个我大意了~
修改后报错:至少一个参数没有被指定值?

你怎么改的?


将insertRow = "select * from [序号$] where  A= arr[i]"; 
 改为insertRow = "select * from [cheet1$] where  A=" + arr[i];   --------------------编程问答-------------------- 将insertRow = "select * from [序号$] where  A= arr[i]"; 
改为insertRow = "select * from [cheet1$] where  A=" + arr[i]; 





insertRow = "select * from [cheet1$] where  A=" + arr[i]; 

是sheet1  吧
嗯~同样报错:至少一个参数没有被指定值~是因为空格问题?不好意思~这个弄到我头都大了 --------------------编程问答-------------------- arr[i]压根就没有值,怎么能直接用呢,还有insertRow = "select * from [序号$] where  A= arr[i]"; 这句,工作簿名称不能错,还有列A的类型不能错,你再仔细检查下吧
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,