标签:
通过Nuget安装NPOI类库
1 public class ExcelOperator 2 { 3 /// <summary> 4 /// Init Workbook 5 /// </summary> 6 /// <param name="path">excel file path</param> 7 /// <returns></returns> 8 private IWorkbook InitializeWorkbook(string path) 9 { 10 string extension = Path.GetExtension(path); 11 if (string.IsNullOrEmpty(extension) || 12 ( 13 !".xlsx".Equals(extension.ToLower()) && 14 !".xls".Equals(extension.ToLower()) 15 ) 16 ) 17 { 18 throw new InvalidOperationException("this is not a excel file"); 19 } 20 21 IWorkbook workbook; 22 23 if (".xlsx".Equals(extension.ToLower())) 24 { 25 workbook = new XSSFWorkbook(); 26 } 27 else 28 { 29 workbook = new HSSFWorkbook(); 30 } 31 32 return workbook; 33 } 34 35 /// <summary> 36 /// Init Workbook 37 /// </summary> 38 /// <param name="path">excel file path</param> 39 /// <param name="fileStream"></param> 40 /// <returns></returns> 41 private IWorkbook InitializeWorkbook(string path, out FileStream fileStream) 42 { 43 string extension = Path.GetExtension(path); 44 if (string.IsNullOrEmpty(extension) || 45 ( 46 !".xlsx".Equals(extension.ToLower()) && 47 !".xls".Equals(extension.ToLower()) 48 ) 49 ) 50 { 51 throw new InvalidOperationException("this is not a excel file"); 52 } 53 54 IWorkbook workbook; 55 fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite); 56 57 if (".xlsx".Equals(extension.ToLower())) 58 { 59 workbook = new XSSFWorkbook(fileStream); 60 } 61 else 62 { 63 workbook = new HSSFWorkbook(fileStream); 64 } 65 66 return workbook; 67 } 68 69 /// <summary> 70 /// 将excel中的数据导入到DataTable中 71 /// </summary> 72 /// <param name="path">excel路径</param> 73 /// <param name="sheetName">excel工作薄sheet的名称</param> 74 /// <param name="isFirstRowColumn">第一行是否是DataTable的列名</param> 75 /// <returns>返回的DataTable</returns> 76 public DataTable ExcelToDataTable(string path, string sheetName, bool isFirstRowColumn) 77 { 78 FileStream fileStream = null; 79 80 try 81 { 82 DataTable data = new DataTable(); 83 84 IWorkbook workbook = InitializeWorkbook(path, out fileStream); 85 86 //如果不指定sheet,则获取第一个sheet 87 ISheet sheet = sheetName != null ? workbook.GetSheet(sheetName) : workbook.GetSheetAt(0); 88 89 //check sheet是否存在 90 if (sheet == null) 91 { 92 throw new InvalidOperationException("Sheet does not exist"); 93 } 94 95 IRow firstRow = sheet.GetRow(0); 96 int firstCellNum = firstRow.FirstCellNum; 97 int lastCellNum = firstRow.LastCellNum; 98 99 int startRow = 0; 100 if (isFirstRowColumn) 101 { 102 //添加列 103 for (int i = firstCellNum; i < lastCellNum; i++) 104 { 105 ICell cell = firstRow.GetCell(i); 106 if (cell != null) 107 { 108 string cellValue = cell.StringCellValue; 109 if (cellValue != null) 110 { 111 var column = new DataColumn(cellValue); 112 data.Columns.Add(column); 113 } 114 } 115 } 116 startRow = sheet.FirstRowNum + 1; 117 } 118 else 119 { 120 startRow = sheet.FirstRowNum; 121 } 122 123 //最后一列的标号 124 int rowCount = sheet.LastRowNum; 125 for (int i = startRow; i <= rowCount; i++) 126 { 127 IRow row = sheet.GetRow(i); 128 if (row == null) continue; 129 130 DataRow dataRow = data.NewRow(); 131 for (int j = firstCellNum; j < lastCellNum; j++) 132 { 133 if (row.GetCell(j) != null) 134 dataRow[j] = row.GetCell(j).ToString(); 135 } 136 data.Rows.Add(dataRow); 137 } 138 139 return data; 140 } 141 catch (Exception) 142 { 143 throw; 144 } 145 finally 146 { 147 if (fileStream != null) fileStream.Close(); 148 } 149 } 150 151 /// <summary> 152 /// 将DataTable数据导入到excel中 153 /// </summary> 154 /// <param name="data">要导入的数据</param> 155 /// <param name="sheetName">要导入的excel的sheet的名称</param> 156 /// <param name="isColumnWritten">DataTable的列名是否要导入</param> 157 /// <param name="path">要导入数据的excel路径</param> 158 /// <returns>导入数据行数(包含列名那一行)</returns> 159 public int DataTableToExcel(DataTable data, string sheetName, bool isColumnWritten, string path) 160 { 161 int count; 162 163 IWorkbook workbook = InitializeWorkbook(path); 164 165 if (string.IsNullOrEmpty(sheetName)) 166 { 167 sheetName = "Sheet1"; 168 } 169 170 ISheet sheet = workbook.CreateSheet(sheetName); 171 //写入DataTable的列名 172 if (isColumnWritten) 173 { 174 IRow row = sheet.CreateRow(0); 175 for (int j = 0; j < data.Columns.Count; ++j) 176 { 177 row.CreateCell(j).SetCellValue(data.Columns[j].ColumnName); 178 } 179 count = 1; 180 } 181 else 182 { 183 count = 0; 184 } 185 186 //写入行数据 187 for (int i = 0; i < data.Rows.Count; ++i) 188 { 189 IRow row = sheet.CreateRow(count); 190 for (int j = 0; j < data.Columns.Count; ++j) 191 { 192 row.CreateCell(j).SetCellValue(data.Rows[i][j].ToString()); 193 } 194 count++; 195 } 196 197 using (var fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write)) 198 { 199 workbook.Write(fileStream); 200 } 201 202 return count; 203 } 204 205 }
标签:
原文地址:http://www.cnblogs.com/JustYong/p/4432715.html