System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
openFileDialog.Filter = "Excel文件|*.xls";
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)//注意,此处一定要手动引入System.Window.Forms空间,否则你如果使用默认的DialogResult会发现没有OK属性
{
StringBuilder sbr = new StringBuilder();
using (FileStream fs = File.OpenRead(openFileDialog.FileName)) //打开myxls.xls文件
{
HSSFWorkbook workbook = new HSSFWorkbook(fs); //把xls文件中的数据写入wk中
HSSFSheet sheet = workbook.GetSheetAt(0) as HSSFSheet;
path.Content = string.Format("共计读取到数据:{0}条", sheet.LastRowNum);
//遍历行
for (int i = 1; i < sheet.LastRowNum; i++)
{
HSSFRow itemrow = sheet.GetRow(i) as HSSFRow;
string value = itemrow.GetCell(0).StringCellValue;
}
}
}