C#Winform Edition에서 Excel 파일 내용 가져오기

17535 단어 WinForm
  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<Model.ExcelSheet> 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         /// <summary>

 23         ///  excel 

 24         /// </summary>

 25         /// <returns></returns>

 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         /// <summary>

 67         ///  , datagridview 

 68         /// </summary>

 69         /// <param name="sender"></param>

 70         /// <param name="e"></param>

 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         /// <summary>

106         ///  sheet 

107         /// </summary>

108         /// <param name="sender"></param>

109         /// <param name="e"></param>

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         }

좋은 웹페이지 즐겨찾기