using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Microsoft.Office.Interop.Excel; using Microsoft.Office.Core; using System.IO; namespace TestTvw { //添加引用Microsoft Excel 12.0 //表单上添加一个ListView控件 一个TreeView控件 public partial class frmMain : Form { public frmMain() { InitializeComponent(); CreateTvw(); CreateLvw(); } public void CreateLvw() { listView1.View = View.Details; //还有其他列的话,添加列头 listView1.Columns.Add("书名"); } public void CreateTvw() { TreeNode n = new TreeNode(); n.Text = "book"; n.Name = "b"; this.treeView1.Nodes.Add(n); //或者直接添加 //this.treeView1.Nodes.Add("book", "book"); } private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { if (e.Node.Text != "book") { return; } string path = System.Windows.Forms.Application.StartupPath + "//test.xlsx";//你的excel文件的路径 if (File.Exists(path)) { Microsoft.Office.Interop.Excel.Application ex; Microsoft.Office.Interop.Excel.Workbook eBook; Microsoft.Office.Interop.Excel.Worksheet eSheet; ex = new Microsoft.Office.Interop.Excel.ApplicationClass(); eBook = ex.Workbooks.Open(path, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); eSheet = (Microsoft.Office.Interop.Excel.Worksheet)eBook.Sheets.get_Item(1); for (int i = 1; i <=eSheet.UsedRange.Rows.Count ;i++ ) { //这里只取第一列 Microsoft.Office.Interop.Excel.Range r = eSheet.get_Range(string.Format("A{0}", i), Type.Missing); listView1.Items.Add(r.Value2.ToString()); } eBook.Close(false, Type.Missing, Type.Missing); } } } } //自动生成代码 namespace TestTvw { partial class frmMain { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.treeView1 = new System.Windows.Forms.TreeView(); this.listView1 = new System.Windows.Forms.ListView(); this.SuspendLayout(); // // treeView1 // this.treeView1.Location = new System.Drawing.Point(12, 12); this.treeView1.Name = "treeView1"; this.treeView1.Size = new System.Drawing.Size(192, 442); this.treeView1.TabIndex = 0; this.treeView1.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick); // // listView1 // this.listView1.Location = new System.Drawing.Point(217, 16); this.listView1.Name = "listView1"; this.listView1.Size = new System.Drawing.Size(402, 437); this.listView1.TabIndex = 1; this.listView1.UseCompatibleStateImageBehavior = false; // // frmMain // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(646, 480); this.Controls.Add(this.listView1); this.Controls.Add(this.treeView1); this.Name = "frmMain"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion private System.Windows.Forms.TreeView treeView1; private System.Windows.Forms.ListView listView1; } } using System; using System.Collections.Generic; using System.Windows.Forms; namespace TestTvw { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new frmMain()); } } }