//读取数据 private void btRead_Click(object sender, EventArgs e) { int MAXLINE = 5000; int i = 0, j = 0, k = 0, m = 0;//m为总行数 int fileCount = lvFile.Items.Count; string DataTag; int eCount = 0;//有效工作簿数 int sCount = 0;//当前表中工作簿数 Point point; Object missing = Type.Missing; int iCount = lbContent.Items.Count; //重点区域,范围型读取单元格区域 RangeSelector mainRange = new RangeSelector(tbMainRange.Text); //预判断块读取还是固定位置读取,初始化总数组大小 if (mainRange.getWidth() > 0) myArray = new String[MAXLINE, mainRange.getWidth() + iCount + 1];//最多千行 else myArray = new String[MAXLINE, iCount + 1];//最多千行 //開啟一個新的應用程式 myExcel = new Excel.Application(); for (i = 0; i < fileCount; i++) { //停用警告訊息 myExcel.DisplayAlerts = false; //讓Excel文件可見 myExcel.Visible = true; //引用第一個活頁簿 myBook = myExcel.Workbooks.Open(lvFile.Items[i].SubItems[2].Text, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); //設定活頁簿焦點 myBook.Activate(); //判断所有工作簿 sCount = myBook.Worksheets.Count; for (k = 1; k <= sCount; k++) { //大表判断条件 if (cbSheetSelect.Text != "全部" && Int16.Parse(cbSheetSelect.Text) != k) continue; //选择当前表 mySheet = (Worksheet)myBook.Worksheets[k]; //設工作表焦點 mySheet.Activate(); //特征值判断 if (tbSheetPos.Text != "") { point = pointPos(tbSheetPos.Text); if (mySheet.Cells[point.Y, point.X].Value != tbSheetCont.Text) continue; } eCount++; //备注列判断 if (tbDataTag.Text == "") DataTag = lvFile.Items[i].SubItems[0].Text; //未设置备注默认使用文件名 else { Point tagpos = pointPos(tbDataTag.Text); DataTag = Convert.ToString(mySheet.Cells[tagpos.Y, tagpos.X].Value); } string mainStart = tbMainStart.Text; string mainEnd = tbMainEnd.Text; //判断选择哪种模式 if (mainRange.Count() > 1) { mainRange = new RangeSelector(tbMainRange.Text);//重新恢复原区域值 //重点区域起始位置判断 Point nowPos = mainRange.getCurPos(); for (j = 0; j < mainRange.Count(); j++) { string myCell = Convert.ToString(mySheet.Cells[nowPos.Y, nowPos.X].Value); if (mainStart == "") break; if (myCell == mainStart) break; mainRange.acc(); } //mainRange.lineacc(); //移到关键字下一行 mainRange.SetStartVal(mainRange.getCurPos()); //读取内容 while (m < MAXLINE) //最大读取行数上限估计 { nowPos = mainRange.getCurPos(); string lineFirstCell = Convert.ToString(mySheet.Cells[nowPos.Y, nowPos.X].Value); if (lineFirstCell == null|| lineFirstCell=="") break; //首字为空 if (lineFirstCell == tbMainEnd.Text) break; //符合结束字符串 if (mainRange.pos > mainRange.Count()) break;//读取完了就退出 for (j = 0; j < mainRange.getWidth(); j++)//读取一行 { point = mainRange.getCurPos(); myArray[m, j] = Convert.ToString(mySheet.Cells[point.Y, point.X].Value); //不管什么类型都转为字符串 mainRange.acc(); } myArray[m, j] = DataTag; m++; } } else { //准备读取单元格相关信息,固定位置读取单元格 if (iCount >= 1) { List<Array> ListOfLine = new List<Array>(); //所有的读取行集合 String[] myLine = new String[iCount]; //单行对象 RangeSelector[] rsContentA = new RangeSelector[iCount]; for (j = 0; j < iCount; j++) { rsContentA[j] = new RangeSelector(lbContent.Items[j].ToString()); } j = 0; foreach (RangeSelector cont in rsContentA) { cont.acc(); point = cont.getCurPos(); myArray[m, j] = Convert.ToString(mySheet.Cells[point.Y, point.X].Value); //不管什么类型都转为字符串 j++; if (j > iCount) break;//xxxxxxx } myArray[m, j - 1] = DataTag; m++; } } } //关闭当前活页簿 myBook.Close(); System.Windows.Forms.Application.DoEvents(); } myExcel.Quit(); }
//读取数据 private void btRead_Click(object sender, EventArgs e) { int MAXLINE = 5000; int i = 0, j = 0, k = 0, m = 0;//m为总行数 int fileCount = lvFile.Items.Count; string filename; int eCount = 0; //有效工作簿数 int sCount = 0; //当前表中工作簿数 Point point; Object missing = Type.Missing; int iCount = lbContent.Items.Count; //重点区域,范围型读取单元格区域 RangeSelector mainRange = new RangeSelector(tbMainRange.Text); //预判断块读取还是固定位置读取,初始化总数组大小 if (mainRange.getWidth() > 0) { myArray = new String[MAXLINE, mainRange.getWidth() + 1];//最多千行 } else { myArray = new String[MAXLINE, iCount + 1];//最多千行 } //開啟一個新的應用程式 myExcel = new Excel.Application(); for (i = 0; i < fileCount; i++) { //停用警告訊息 myExcel.DisplayAlerts = false; //讓Excel文件可見 myExcel.Visible = true; //引用第一個活頁簿 myBook = myExcel.Workbooks.Open(lvFile.Items[i].SubItems[2].Text, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); //設定活頁簿焦點 myBook.Activate(); //判断所有工作簿 sCount = myBook.Worksheets.Count; for (k = 1; k <= sCount; k++) { //大表判断条件 if (cbSheetSelect.Text != "全部" && Int16.Parse(cbSheetSelect.Text) != k) { continue; } //选择当前表 mySheet = (Worksheet)myBook.Worksheets[k]; //設工作表焦點 mySheet.Activate(); //特征值判断 if (tbSheetPos.Text != "") { point = pointPos(tbSheetPos.Text); if (mySheet.Cells[point.Y, point.X].Value != tbSheetCont.Text) { continue; } } eCount++; filename = lvFile.Items[i].SubItems[0].Text; //提取文件名 string mainStart = tbMainStart.Text; string mainEnd = tbMainEnd.Text; //判断选择哪种模式 if (mainRange.Count() > 1) { mainRange = new RangeSelector(tbMainRange.Text);//重新恢复原区域值 //重点区域起始位置判断 Point nowPos = mainRange.getCurPos(); for (j = 0; j < mainRange.Count(); j++) { string myCell = Convert.ToString(mySheet.Cells[nowPos.Y, nowPos.X].Value); if (mainStart == "") { break; } if (myCell == mainStart) { break; } mainRange.acc(); } //mainRange.lineacc(); //移到关键字下一行 mainRange.SetStartVal(mainRange.getCurPos()); //读取内容 while (m < MAXLINE) //最大读取行数上限估计 { nowPos = mainRange.getCurPos(); string myCell = Convert.ToString(mySheet.Cells[nowPos.Y, nowPos.X].Value); if (myCell == null) { break; } if (mainRange.pos > mainRange.Count()) { break; //读取完了就退出 } for (j = 0; j < mainRange.getWidth(); j++) { point = mainRange.getCurPos(); myArray[m, j] = Convert.ToString(mySheet.Cells[point.Y, point.X].Value); //不管什么类型都转为字符串 mainRange.acc(); } myArray[m, j] = filename; m++; } } else { //准备读取单元格相关信息,固定位置读取单元格 if (iCount >= 1) { List <Array> ListOfLine = new List <Array>(); //所有的读取行集合 String[] myLine = new String[iCount]; //单行对象 RangeSelector[] rsContentA = new RangeSelector[iCount]; for (j = 0; j < iCount; j++) { rsContentA[j] = new RangeSelector(lbContent.Items[j].ToString()); } j = 0; foreach (RangeSelector cont in rsContentA) { cont.acc(); point = cont.getCurPos(); myArray[m, j] = Convert.ToString(mySheet.Cells[point.Y, point.X].Value); //不管什么类型都转为字符串 j++; } myArray[m, j] = filename; m++; } } } //关闭当前活页簿 myBook.Close(); System.Windows.Forms.Application.DoEvents(); } myExcel.Quit(); }