一:使用spreadsheetControl1 方法
1:打开excel;
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { //if (Convert.ToInt32(trv_menu.SelectedNode.Tag) == 0) //{ // MessageBox.Show("请先选择业务", "提示信息", MessageBoxButtons.OK); // return; //} OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "(*.xls)|*.xls|(*.xlsx)|*.xlsx|(*.csv)|*.csv"; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { spreadsheetControl1.LoadDocument(openFileDialog1.FileName); } spreadsheetControl1.ReadOnly = true; }
2:读取按行读取excel 处理每行中列的数值
private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { // status1.Visible = true; //读取Excele方法 //当前数据行数 int rowCount = spreadsheetControl1.ActiveWorksheet.Cells.CurrentRegion.RowCount; //当前数据列数 int columnCount = spreadsheetControl1.ActiveWorksheet.Cells.CurrentRegion.ColumnCount; if (rowCount == 1 && columnCount == 1) { MessageBox.Show("内容不能为空", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } for (int i = 0; i < rowCount; i++) { string sr = spreadsheetControl1.ActiveWorksheet.Cells[i, 0].Value.ToString(); if (sr == "") { // blankcount++; blankcountEvent(this, e); // btnblank.Text = blankcount.ToString(); ; continue; } //for (int j = 0; j < columnCount; j++) //{ string zyzsId = spreadsheetControl1.ActiveWorksheet.Cells[i, 0].Value.ToString(); //这里取的是指定列的值. 第一列,第六列和第七列 然后做上传 string parentId = spreadsheetControl1.ActiveWorksheet.Cells[i, 5].Value.ToString(); string parentName = spreadsheetControl1.ActiveWorksheet.Cells[i, 6].Value.ToString(); string ip= "192.168.1.115"; //} string url = "xxxxxxxxxx" + parentName; string data =""/; string result = HttpPost(url, data); if(result=="ok") { // successcount++; successcountEvent(this, e); // btnupsuccount.Text= successcount.ToString(); } else if(result == "fail") { // failedcount++; failedcountEvent(this, e); // btnfailedcount.Text = failedcount.ToString(); } } }
总结:由于数据倒入到spreadsheetControl1 显示在窗体中没有实际操作,占用了资源.在上传时有卡顿现象,卡顿也有其他原因.总之这种是现实方式方式不值得推荐.
二:文件文件名加载到gridcontrol中 ,显示进度的方式
技术交流qq群:143280841