c# 用OpenXmL读取.xlsx格式的Excel文件 返回DataTable
1.需要引用的dll : DocumentFormat.OpenXml.dll ---需要安装一下OpenXml再引用
WindowsBase ---直接在项目里添加引用
2.方法:
1 /// <summary> 2 /// 读取.xlsx格式的Excel文件数据,读取其特定名称的工作薄 3 /// </summary> 4 /// <param name="filePath">文件路径 如 D:\\excel1.xls</param> 5 /// <param name="sheetName">工作薄名称 如 Sheet1</param> 6 /// <param name="fieldNames">要转化成的DataTable的列名数组</param> 7 /// <returns></returns> 8 public static DataTable ReadExcelToDataTable(Stream fileStream, string sheetName, string[] fieldNames) 9 { 10 11 using (var document = SpreadsheetDocument.Open(fileStream, false)) 12 { 13 var sheets = document.WorkbookPart.Workbook.Descendants<Sheet>().Where(p => p.Name == sheetName); 14 var enumerable = sheets as Sheet[] ?? sheets.ToArray(); 15 if (!enumerable.Any()) 16 { 17 return null; 18 } 19 var wsp = (WorksheetPart)document.WorkbookPart.GetPartById(enumerable.First().Id); 20 //获取Excel中共享数据 21 var stringTable = document.WorkbookPart.SharedStringTablePart.SharedStringTable; 22 var rows = wsp.Worksheet.Descendants<Row>();//获得Excel中得数据行 23 DataTable table = new DataTable(); 24 foreach (var name in fieldNames) 25 { 26 table.Columns.Add(name, typeof(string)); 27 } 28 //因为须要将数据导入到DataTable中,所以我们假定Excel的第一行是列名,从第二行开端是行数据 29 var _count = 0; 30 var enumerable1 = rows as Row[] ?? rows.ToArray(); 31 var total = enumerable1.Count() - 1; 32 foreach (var row in enumerable1) 33 { 34 if (row.RowIndex > 1) 35 { 36 _count++; 37 GetDataRow(row, stringTable, ref table, fieldNames); //Excel第二行同时为DataTable的第一行数据 38 if (_count % 100 != 0) continue; 39 var per = (100 * _count / total - 5) <= 0 ? 1 : (100 * _count / total - 5); 40 var perS = per.ToString(CultureInfo.InvariantCulture) + "%"; 41 42 HttpContext.Current.Response.Write("<script>top.process('" + perS + "');</script>"); 43 HttpContext.Current.Response.Flush(); 44 } 45 } 46 return table; 47 } 48 } 49 /// 获取Excel行数据 50 private static void GetDataRow(IEnumerable<OpenXmlElement> row, OpenXmlElement stringTable, ref DataTable table, string[] fieldNames) 51 { 52 if (stringTable == null) throw new ArgumentNullException("stringTable"); 53 var dic = new Dictionary<int, string>(); 54 var i = 0; 55 foreach (Cell cell in row) 56 { 57 GetValue(i, cell, stringTable, ref dic); 58 i++; 59 } 60 if (dic.Count == 0) 61 { 62 return; 63 } 64 var dr = table.NewRow(); 65 int index = 0; 66 foreach (var name in fieldNames) 67 { 68 dr[name] = dic[index]; 69 index++; 70 } 71 table.Rows.Add(dr); 72 } 73 /// 获取Excel单元格数据 74 private static void GetValue(int i, CellType cell, OpenXmlElement stringTable, ref Dictionary<int, string> dic) 75 { 76 if (stringTable == null) throw new ArgumentNullException("stringTable"); 77 //因为Excel的数据存储在SharedStringTable中,须要获取数据在SharedStringTable 中的索引 78 var value = string.Empty; 79 try 80 { 81 if (cell.ChildElements.Count == 0) 82 return; 83 value = cell.CellValue.InnerText; 84 if ((cell.DataType != null) && (cell.DataType == CellValues.SharedString)) 85 { 86 value = stringTable.ChildElements[Int32.Parse(value)].InnerText; 87 } 88 dic.Add(i, value); 89 90 } 91 catch (Exception) 92 { 93 } 94 }
posted on 2017-06-09 08:44 alex5211314 阅读(447) 评论(0) 编辑 收藏 举报