C#第三次作业

来源:互联网 发布:淘宝助手官网下载 编辑:程序博客网 时间:2024/05/16 13:46
[csharp] view plaincopy
  1. 目标1:用C#读取Excle文件内容  
[csharp] view plaincopy
  1. //代码实现  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.ComponentModel;  
  5. using System.Data;  
  6. using System.Data.OleDb;  
  7. using System.Drawing;  
  8. using System.Linq;  
  9. using System.Text;  
  10. using System.Threading.Tasks;  
  11. using System.Windows.Forms;  
  12.   
  13. namespace WRExc  
  14. {  
  15.     public partial class form1 : Form  
  16.     {  
  17.         public form1()  
  18.         {  
  19.             InitializeComponent();  
  20.         }  
  21.   
  22.         private void Form1_Load(object sender, EventArgs e)  
  23.         {  
  24.   
  25.         }  
  26.    
  27.         private void button1_Click(object sender, EventArgs e)  
  28.         {  
  29.             OpenFileDialog openfile = new OpenFileDialog();  
  30.             openfile.Filter = "工作薄(*.xls)|*.xls|所有文件(*.*)|*.*";  //用Filter属性设置文件格式  
  31.             //点击“打开”窗体显示文件内容  
  32.             if (openfile.FilterIndex == 1 && openfile.ShowDialog() == DialogResult.OK)    
  33.             dataset1(openfile.FileName);     //使用dataset组件  
  34.   
  35.         }  
  36.   
  37.         private void openFileDialog1_FileOk(object sender, CancelEventArgs e)  
  38.         {  
  39.   
  40.         }  
  41.         public DataSet dataset1(string path)   //文件路径设置  
  42.         {  
  43.   
  44.   
  45.             string strConn = "ProviderMICROSOFT.Jet.OLEDB.4.0;" + "Data Source=" + @path + ";" + "Extended Properties=Excel 8.0;";  
  46.             OleDbConnection conn = new OleDbConnection(strConn);  
  47.             conn.Open();  
  48.             string strExcel = "";  
  49.             OleDbDataAdapter myCommand = null;  
  50.             DataSet ds = null;  
  51.             strExcel = "select * from [sheet1$]";     //SQL语句选择表内容  
  52.             myCommand = new OleDbDataAdapter(strExcel, strConn);    //调用OleDbDataAdapter()方法复制给myCommand  
  53.             DataTable table1 = new DataTable();  
  54.             ds = new DataSet();  
  55.             myCommand.Fill(table1);  
  56.             dataGridView1.DataSource = table1;  
  57.             return ds;  
  58.   
  59.         }   
  60.     }  
  61. }  
[csharp] view plaincopy
  1. 演示结果:  
[csharp] view plaincopy
  1. 1窗体界面  
[csharp] view plaincopy
  1. <img src="http://img.blog.csdn.net/20150508211622979?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHkxOTk0ODI5/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />  
[csharp] view plaincopy
  1. 2点击打开后续  
[csharp] view plaincopy
  1. <img width="677" height="475" style="width: 549px; height: 290px;" src="http://img.blog.csdn.net/20150508211653929?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHkxOTk0ODI5/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />  
[csharp] view plaincopy
  1. 3选择事例文件后窗体显示结果  
[csharp] view plaincopy
  1. <img src="http://img.blog.csdn.net/20150508211604630?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHkxOTk0ODI5/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />  
[csharp] view plaincopy
  1. 完成目标要求,这次程序主要难点在于怎么读取Excle
0 0