我正在为我在医院工作的朋友做一个应用程序。如何从每个单元格获取数据?
该应用程序基本上只是从Excel中抓取数据,对其进行排序并显示。 我坚持抓住所有的数据。 excel文件有3列和n行。第一列的单元格并不总是被填充。
我尝试了采取这些数据的不同方式,并且始终无法使其正常工作。我试图将空单元格包含到搜索中,但现在temp会将所有空字符串返回给我。
如何迭代每一行直到结束,并在每次迭代时从每列中获取数据,即使它是空的 - return“”?
Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(currentExcelLocation, 0, true, 5, "", "", false, Excel.XlPlatform.xlWindows,
"", false, true, 0, false, false, false);
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(3);
int iTotalRows = excelWorksheet.UsedRange.Rows.Count;
for (int i = 1; i <= iTotalRows; ++i)
{
string tempL = (excelWorksheet.Cells[i, 1] as Excel.Range).SpecialCells(Excel.XlCellType.xlCellTypeBlanks).Text.ToString();
if (tempL != "") { temp = tempL; }
string tempS = (excelWorksheet.Cells[i, 2] as Excel.Range).Value2.ToString();
string tempD = (excelWorksheet.Cells[i, 3] as Excel.Range).Value2.ToString();
DataScheme.Symptoms tempSD = new DataScheme.Symptoms(tempS, tempD);
if (data.FindIndex(a => a.Location == temp) < 0) { data.Add(new DataScheme(temp)); }
data.Find(b => b.Location == temp).SympList.Add(tempSD);
textBox6.Text += temp + Environment.NewLine;
}
excelWorkbook.Close(0);
excelApp.Quit();
Marshal.FinalReleaseComObject(excelApp);
Marshal.FinalReleaseComObject(excelWorkbook);
Marshal.FinalReleaseComObject(excelSheets);
Marshal.FinalReleaseComObject(excelWorksheet);
最后你的意思是每行最后一个数据单元? –