添加引用 Microsoft.Office.Tools.Excel.dll

解决方案 »

  1.   

    添加引用   Microsoft.Office.Tools.Excel.dll 只可以
               using Microsoft.Office.Tools.Excel;
           而不可以
               using Microsoft.Office.Interop.Excel;我要用的是Microsoft.Office.Interop.Excel的DLL或EXE位置
     
      

  2.   


    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.SqlClient;
    using System.Data.OleDb;
    using Excel;
    using System.Net.NetworkInformation;namespace shixi8
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        string strConn;
            OleDbConnection conn;
            OleDbDataAdapter myCommand;
            DataSet ds;
            int row;
            int col;
            int loc;
            private void Load_Click(object sender, EventArgs e)
            {
                try
                {
                    openFileDialog1.Filter = "EXCEL(*.xls)|*.xls|所有文件(*.*)|*.*";
                    openFileDialog1.FileName = "";
                    if (openFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + openFileDialog1.FileName + ";" + "Extended Properties=Excel 8.0;";
                        conn = new OleDbConnection(strConn);
                        conn.Open();
                        string strExcel = "";
                        myCommand = null;
                        ds = null;
                        strExcel = "select * from [sheet1$]";
                        myCommand = new OleDbDataAdapter(strExcel, strConn);
                        ds = new DataSet();
                        myCommand.Fill(ds, "table1");
                        dataGridView1.DataSource = ds.Tables[0];
                    }
                }
                catch (Exception ex)
                {                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }                
            private void Search_Click(object sender, EventArgs e)
            {
                row = dataGridView1.RowCount;
                col = dataGridView1.ColumnCount;
                for (int i = 0; i < row - 1; i++)
                    for (int j = 0; j < col - 1; j++)
                        if (dataGridView1.Rows[i].Cells[j].Value.ToString() == textBox2.Text)
                        {
                            dataGridView1.Rows[i].Selected = true;
                            loc = i;
                        }
            }        private void Delete_Click(object sender, EventArgs e)
            {
                try
                {
                    for (int i = 0; i < loc; i++)
                    {
                        dataGridView1.Rows.RemoveAt(0);
                    }
                    int rows2 = dataGridView1.Rows.Count;                for (int i = 1; i < rows2 - 1; i++)
                    {
                        dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Count - 2);
                    }
                }
                catch (Exception ex)
                {                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }        private void button1_Click(object sender, EventArgs e)
            {
                
                //建立excel对象
                Excel.Application excel = new Excel.Application();
                excel.Application.Workbooks.Add(true);
                excel.Visible = true;
                //生成字段名称            for (int i = 0; i < dataGridView1.ColumnCount; i++)
                {
                    excel.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
                }
                //填充数据
                for (int i = 0; i < dataGridView1.RowCount - 1; i++)
                {
                    for (int j = 0; j < dataGridView1.ColumnCount; j++)
                    {
                        if (dataGridView1[j, i].ValueType == typeof(string))
                        {
                            excel.Cells[i + 2, j + 1] = "'" + dataGridView1[j, i].Value.ToString();
                        }
                        else
                        {
                            excel.Cells[i + 2, j + 1] = dataGridView1[j, i].Value.ToString();
                        }
                    }
                }        }        //private void Save_Click(object sender, EventArgs e)
            //{
            //    Excel.Application app = new Excel.Application();
            //    app.Application .Workbooks.Add(true);
            //    app.Visible = true;
            //    app.Cells[1,1]=dataGridView1.Columns[0].HeaderText;
            //    for (int i = 0; i < dataGridView1.Rows.Count; i++)
            //    {
            //        if (dataGridView1[i, 0].ValueType == typeof(string))
            //            app.Cells[2, i + 1] = "'" + dataGridView1[i, 0].Value.ToString();
            //        else
            //            app.Cells[2, i + 1] = dataGridView1[i, 0].Value.ToString();
            //    }
            //    app.Quit();        //}        
        }
    }以前编的处理excel   
    用Excel组件,在文件名单.xls中Sheet1的所有学生名单中,找到本人的姓名,同时删除其他同学的行,即仅保留本人所在的行。
    在应用里面要添加excel组件