C#打开窗口的文档操作

private void btnLook_Click(object sender, EventArgs e)

        {

            //打开文件进行导入

            OpenFileDialog openFile = new OpenFileDialog();

            //路径

            openFile.InitialDirectory = "C:\\";

            //选择文件类型

            openFile.Filter = "工作薄(*.xls)|*.xls|C#文件|*.cs|所有文件|*.*";

            openFile.FilterIndex = 1;

            openFile.RestoreDirectory = true;

            //保存路径到文本框中

            if (openFile.ShowDialog() == DialogResult.OK)

            {

                user = openFile.FileName;

            }

            //判断文本框内的内容从而判断是否选择了文件

            try

            {

                if (user != "")

                {

                    //由excel表导入到数据库的缓存中

                    ds = ExecelToDS(user);

                    MessageBox.Show("数据导入成功!");

                    btnLook.Visible = false;

                }

                else

                {

                    MessageBox.Show("您还没有选择文件!");

                }

            }

            catch (Exception)

            {

                MessageBox.Show("您还没有导入文件呦!");

            }

        }


private DataSet setDataSet(string path)

{

string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";

            OleDbConnection conn = new OleDbConnection(strConn);

            conn.Open();

            string strExcel = "";

            OleDbDataAdapter myCommand = null;

            DataSet ds = null;

            //excel默认的第一个表

            strExcel = "select * from [sheet1$] where 是否中奖='否'";

            myCommand = new OleDbDataAdapter(strExcel, strConn);

            ds = new DataSet();

            myCommand.Fill(ds, "table1");

            return ds;

}

猜你喜欢

转载自blog.csdn.net/qq_39665357/article/details/81252490
今日推荐