C#-Gridview直接依欄位名稱匯入
又是一個匯入程式,
這個功能是依欄位名稱做匯入,
程式如下:
using Microsoft.Office.Interop.Excel; int _rowindex = 2; Microsoft.Office.Interop.Excel.ApplicationClass app; Microsoft.Office.Interop.Excel.Workbook workBook; Microsoft.Office.Interop.Excel.Worksheet workSheet; private void Inport_Click(object sender, EventArgs e) { string _setpath; string _path; if (openFileDialog1.ShowDialog() == DialogResult.OK) { //取得檔案路徑名稱 _setpath = openFileDialog1.FileName; _path = _setpath; app = new Microsoft.Office.Interop.Excel.ApplicationClass(); workBook = app.Workbooks.Open(_path, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.ActiveSheet; //資料匯入 while (((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[_rowindex, 1]).Value2.ToString() != "") { System.Windows.Forms.Application.DoEvents(); WRITEIN(((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[_rowindex, 3]).Value2.ToString()); _rowindex = _rowindex + 1; //progressBar1.Value = progressBar1.Value + 1; if (((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[_rowindex, 1]).Value2 == null) { app.Quit(); break; } } } }
//匯入設定值函式 public void WRITEIN(string _ID) { if (ibs.Count > 0) { ibs.BeginEdit(); DataRowView add = (DataRowView)ibs.Current; add["StudentNo"] = checknull(((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[_rowindex, 1]).Value2.ToString()); add["ChtName"] = checknull(((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[_rowindex, 2]).Value2.ToString()); ibst.EndEdit(); id.ApplyUpdates(); } else { ibs.AddNew(); DataRowView add = (DataRowView)ibs.Current; add["StudentNo"] = checknull(((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[_rowindex, 1]).Value2.ToString()); add["ChtName"] = checknull(((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[_rowindex, 2]).Value2.ToString()); ibs.EndEdit(); id.ApplyUpdates(); } }
自我LV~