标签:
最近项目用到的读取Excel 为DataTable 兼容2003、2007、2010。记录一下,以后会用到
引用 NPOI.dll 和 EPPlus.dll
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Data; 6 using OfficeOpenXml; 7 using System.IO; 8 using NPOI.SS.UserModel; 9 using NPOI.HSSF.Util; 10 using NPOI.HSSF.UserModel; 11 12 public class ExcelUtil 13 { 14 /// <summary> 15 /// Excel 转 DataTable 约定 Excel第一行为列名,即为转换后的Datatable中的DataColumn列 16 /// </summary> 17 /// <param name="file">Excel文件路径</param> 18 /// <param name="sheet">sheet名称</param> 19 /// <returns></returns> 20 public static DataTable ExcelSheet2DataTable(string file, string sheet) 21 { 22 DataTable dt = null; 23 FileInfo existingFile = new FileInfo(file); 24 try 25 { 26 ExcelPackage package = new ExcelPackage(existingFile); 27 int vSheetCount = package.Workbook.Worksheets.Count; //获取总Sheet页 28 29 ExcelWorksheet worksheet = package.Workbook.Worksheets[sheet];//选定 指定页 30 31 int maxColumnNum = worksheet.Dimension.End.Column;//最大列 32 int minColumnNum = worksheet.Dimension.Start.Column;//最小列 33 34 int maxRowNum = worksheet.Dimension.End.Row;//最小行 35 int minRowNum = worksheet.Dimension.Start.Row;//最大行 36 37 DataTable vTable = new DataTable(); 38 DataColumn vC; 39 for (int j = 1; j <= maxColumnNum; j++) 40 { 41 string colname = worksheet.Cells[1, j].Text; 42 if (vTable.Columns.Contains(colname)) 43 { 44 colname += "1";//对于可能的重名列的处理 45 } 46 vC = new DataColumn(colname, typeof(string)); 47 48 vTable.Columns.Add(vC); 49 } 50 51 for (int n = 2; n <= maxRowNum; n++) 52 { 53 DataRow vRow = vTable.NewRow(); 54 for (int m = 1; m <= maxColumnNum; m++) 55 { 56 vRow[m - 1] = worksheet.Cells[n, m].Value; 57 } 58 vTable.Rows.Add(vRow); 59 } 60 dt = vTable; 61 worksheet = null; 62 sheet = null; 63 } 64 catch (Exception vErr) 65 { 67 try 68 { 69 using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read)) 70 { 71 using (HSSFWorkbook workbook = new HSSFWorkbook(fs)) //根据EXCEL文件流初始化工作簿 72 { 73 //var sheet1 = workbook.GetSheetAt(0); //获取第一个sheet 74 var sheet1 = workbook.GetSheet(sheet); //获取sheet 75 DataTable table = new DataTable();// 76 var row1 = sheet1.GetRow(0);//获取第一行即标头 77 78 int cellCount = row1.LastCellNum; //第一行的列数 79 int rowCount = sheet1.LastRowNum; //总行数 80 81 //把第一行的数据添加到datatable的列名 82 for (int i = row1.FirstCellNum; i < cellCount; i++) 83 { 84 string colname = string.Empty; 85 if (row1.GetCell(i).CellType == NPOI.SS.UserModel.CellType.STRING) 86 { 87 colname = row1.GetCell(i).StringCellValue; 88 } 89 else 90 { 91 colname = row1.GetCell(i).ToString(); 92 } 93 if (table.Columns.Contains(colname)) 94 { 95 colname += "1"; 96 } 97 DataColumn column = new DataColumn(colname); 98 table.Columns.Add(column); 99 } 100 //把每行数据添加到datatable中 104 for (int i = (sheet1.FirstRowNum + 1); i < sheet1.LastRowNum; i++) 105 { 106 HSSFRow row = sheet1.GetRow(i) as HSSFRow; 107 DataRow dataRow = table.NewRow(); 108 109 for (int j = row.FirstCellNum; j < cellCount; j++) 110 { 111 if (row.GetCell(j) != null) 112 dataRow[j] = row.GetCell(j).ToString(); 113 } 114 115 table.Rows.Add(dataRow); 116 } 117 dt = table; 118 } 119 } 120 } 121 catch 122 { 123 124 } 125 } 126 return dt; 127 } 128 }
标签:
原文地址:http://www.cnblogs.com/clarlespeng/p/4829074.html