Excel to DataGridView  using excel 2007

| |
[不指定 2008/06/20 17:14 | by Else ]
[codes=C#]private void Form4_Load(object sender, EventArgs e)

        {

            object oMissing = Missing.Value;

            Microsoft.Office.Interop.Excel.Application ExcelObj =
                new Microsoft.Office.Interop.Excel.Application();

            Microsoft.Office.Interop.Excel.Workbook WBook;

            Microsoft.Office.Interop.Excel.Worksheet WSheet;

            Microsoft.Office.Interop.Excel.Range Selection;

            WBook = ExcelObj.Workbooks.Open(@"c:\test\1.xlsx",

                oMissing, oMissing, oMissing, oMissing, oMissing,

                oMissing, oMissing, oMissing, oMissing, oMissing,

                oMissing, oMissing, oMissing, oMissing);



            WSheet = (Microsoft.Office.Interop.Excel.Worksheet)WBook.Sheets.get_Item(1);

            // Conversion

            Selection = WSheet.Cells;

            Selection.Select();

            Selection.NumberFormat = "@";

            WBook.Save();



            string ConnectionString =

                "Provider=Microsoft.ACE.OLEDB.12.0;" +

                "Data Source=" + @"c:\test\1.xlsx;" +

                "Excel 12.0;HDR=YES;";



            OleDbConnection ExcelConnection = new OleDbConnection(ConnectionString);

            OleDbDataAdapter ad = new OleDbDataAdapter();



            ad.SelectCommand = new OleDbCommand("SELECT * FROM [Sheet1$]", ExcelConnection);

            DataTable dt = new DataTable();

            ad.Fill(dt);



            this.dataGridView1.DataSource = dt;

        }

[/codes]

作者:Else 's Blog
地址:http://www.aixq.com/post/1077/
版权所有。转载时必须链接形式注明作者和原始出处及本声明!
  • 中查看更多“Excel to DataGridView  using excel 2007”相关内容
  • 中查看更多“Excel to DataGridView  using excel 2007”相关内容
  • 中查看更多“Excel to DataGridView  using excel 2007”相关内容
  • 中查看更多“Excel to DataGridView  using excel 2007”相关内容
  • 中查看更多“Excel to DataGridView  using excel 2007”相关内容
  • 中查看更多“Excel to DataGridView  using excel 2007”相关内容

  • 最后编辑: Else 编辑于2008/06/20 17:20
    Tags: , ,
    代码 程序 编程 | 评论(0) | 引用(0) | 阅读(645)
    发表评论
    表情
    emotemotemotemotemot
    emotemotemotemotemot
    emotemotemotemotemot
    emotemotemotemotemot
    emotemotemotemotemot
    打开HTML
    打开UBB
    打开表情
    隐藏
    昵称   密码   游客无需密码
    网址   电邮   [注册]