C#Winform版获取Excel文件的内容

  1 public MainForm()
  2         {
  3             InitializeComponent();
  4             //this.ofd_in.DefaultExt = ".xlsx";
  5             this.ofd_in.Filter = "Excel文件(*.xlsx)|*.xlsx";
  6         }
  7         // code.GetSheetName getname = new code.GetSheetName();
  8         private void btnDr_Click(object sender, EventArgs e)
  9         {
 10             if (ofd_in.ShowDialog() == System.Windows.Forms.DialogResult.OK)
 11             {
 12                 this.txtFileName.Text = ofd_in.FileName.ToString();
 13                 List lst = code.GetSheetName.ExcelSheetName(ofd_in.FileName.ToString());
 14                 cmbBox.DataSource = lst;
 15                 cmbBox.DisplayMember = "SheetName";
 16                 cmbBox.ValueMember = "Id";
 17 
 18                 //ImportExcel(ofd_in.FileName, "[Sheet1$]");//传递excel文件名,和表名
 19             }
 20         }
 21 
 22         /// 
 23         /// 从选择的excel导入
 24         /// 
 25         /// 
 26         public void ImportExcel(string ExcelName, string Tablename)
 27         {
 28             // string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ExcelName + ";Extended Properties='Excel 8.0;HDR=No;IMEX=1';";//连接excel文件的字符串
 29 
 30             string strcon = "Provider=Microsoft.Ace.OleDb.12.0;data source=" + ExcelName + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'"; //此连接可以操作.xls与.xlsx文件 (支持Excel2003 和 Excel2007 的连接字符串)
 31             if (ExcelName == null)
 32             {
 33                 return;
 34             }
 35             OleDbConnection odcon = new OleDbConnection(strcon);//建立连接
 36             odcon.Open();//打开连接
 37 
 38             //把数据集dataset放到控件中显示
 39             OleDbDataAdapter odda = new OleDbDataAdapter("select * from " + Tablename, odcon);
 40             DataSet ds = new DataSet();
 41             try
 42             {
 43                 odda.Fill(ds);
 44             }
 45             catch (Exception ex)
 46             {
 47                 MessageBox.Show(ex.Message);
 48             }
 49             if (ds != null)
 50             {
 51 
 52                 this.dgvdata.DataSource = ds.Tables[0];
 53 
 54             }
 55             else
 56             {
 57                 MessageBox.Show("没有数据");
 58             }
 59         }
 60 
 61         private void btnClose_Click(object sender, EventArgs e)
 62         {
 63             this.Close();
 64         }
 65 
 66         /// 
 67         /// 保存,选中的datagridview的值
 68         /// 
 69         /// 
 70         /// 
 71         private void btnSave_Click(object sender, EventArgs e)
 72         {
 73             int selectedCellCount = dgvdata.GetCellCount(DataGridViewElementStates.Selected);
 74             if (selectedCellCount > 0)
 75             {
 76                 if (dgvdata.AreAllCellsSelected(true))
 77                 {
 78                     MessageBox.Show("All cells are selected", "Selected Cells");
 79                 }
 80                 else
 81                 {
 82                     StringBuilder sb = new StringBuilder();
 83 
 84                     for (int i = 0;
 85                         i < selectedCellCount; i++)
 86                     {
 87                         sb.Append("Row: ");
 88                         int rows = dgvdata.SelectedCells[i].RowIndex;
 89                         sb.Append(rows.ToString());
 90                         sb.Append(", Column: ");
 91                         int columns = dgvdata.SelectedCells[i].ColumnIndex;
 92                         sb.Append(columns.ToString());
 93                         sb.Append(",Value: ");
 94                         sb.Append(dgvdata.Rows[rows].Cells[columns].Value.ToString());
 95                         sb.Append(Environment.NewLine);
 96                     }
 97 
 98                     sb.Append("Total: " + selectedCellCount.ToString());
 99                     MessageBox.Show(sb.ToString(), "Selected Cells");
100                 }
101             }
102         }
103 
104        
105         /// 
106         /// 加载对应的sheet页
107         /// 
108         /// 
109         /// 
110         private void btnSearch_Click(object sender, EventArgs e)
111         {
112             dgvdata.DataSource = null;
113             var sheetname = this.cmbBox.Text.Trim(); ;
114             ImportExcel(this.txtFileName.Text, "["+sheetname+"]");
115         }

 

转载于:https://www.cnblogs.com/Lauyj/p/4364812.html

你可能感兴趣的:(C#Winform版获取Excel文件的内容)