Npoi
1 /************************************************************************ 2 * Copyright (c) 2019 All Rights Reserved. 3 *创建人:wyh 4 *创建时间:2019-04-01 13:26:43 5 *描述 6 *======================================================================= 7 *修改标记 8 *修改时间:2019-04-01 13:26:43 9 *修改人:wyh 10 *描述: 11 ************************************************************************/ 12 using System; 13 using System.Collections.Generic; 14 using System.Data; 15 using System.IO; 16 using System.Linq; 17 using System.Text; 18 using System.Threading.Tasks; 19 using NPOI.HSSF.UserModel; 20 using NPOI.SS.UserModel; 21 using NPOI.XSSF.UserModel; 22 23 namespace lmsHTETest 24 { 25 public class ExcelHelper : IDisposable 26 { 27 private string fileName = null; //文件名 28 private IWorkbook workbook = null; 29 private FileStream fs = null; 30 private bool disposed; 31 32 33 public ExcelHelper(string fileName) 34 { 35 this.fileName = fileName; 36 disposed = false; 37 } 38 39 /// <summary> 40 /// 将DataTable数据导入到excel中 41 /// </summary> 42 /// <param name="data">要导入的数据</param> 43 /// <param name="isColumnWritten">DataTable的列名是否要导入</param> 44 /// <param name="sheetName">要导入的excel的sheet的名称</param> 45 /// <returns>导入数据行数(包含列名那一行)</returns> 46 // public int DataTableToExcel(DataTable data, string sheetName, bool isColumnWritten) 47 // { 48 public bool DataTableToExcel(DataTable dt,string sheetname) 49 { 50 bool result = false; 51 IWorkbook workbook = null; 52 FileStream fs = null; 53 IRow row = null; 54 ISheet sheet = null; 55 ICell cell = null; 56 try 57 { 58 if (dt != null && dt.Rows.Count > 0) 59 { 60 workbook = new HSSFWorkbook(); 61 sheet = workbook.CreateSheet(sheetname);//创建一个名称为Sheet0的表 62 int rowCount = dt.Rows.Count;//行数 63 int columnCount = dt.Columns.Count;//列数 64 65 //设置列头 66 //row = sheet.CreateRow(0);//excel第一行设为列头 67 //for (int c = 0; c < columnCount; c++) 68 //{ 69 // cell = row.CreateCell(c); 70 // cell.SetCellValue(dt.Columns[c].ColumnName); 71 //} 72 73 //设置每行每列的单元格, 74 for (int i = 0; i < rowCount; i++) 75 { 76 row = sheet.CreateRow(i + 1); 77 for (int j = 0; j < columnCount; j++) 78 { 79 cell = row.CreateCell(j);//excel第二行开始写入数据 80 cell.SetCellValue(dt.Rows[i][j].ToString()); 81 } 82 } 83 using (fs = File.OpenWrite(@"D:/myxls.xls")) 84 { 85 workbook.Write(fs);//向打开的这个xls文件中写入数据 86 result = true; 87 } 88 } 89 return result; 90 } 91 catch (Exception ex) 92 { 93 if (fs != null) 94 { 95 fs.Close(); 96 } 97 return false; 98 } 99 } 100 101 /// <summary> 102 /// 将excel中的数据导入到DataTable中 103 /// </summary> 104 /// <param name="sheetName">excel工作薄sheet的名称</param> 105 /// <param name="isFirstRowColumn">第一行是否是DataTable的列名</param> 106 /// <returns>返回的DataTable</returns> 107 public DataTable ExcelToDataTable(string sheetName, bool isFirstRowColumn) 108 { 109 ISheet sheet = null; 110 DataTable data = new DataTable(); 111 int startRow = 0; 112 try 113 { 114 fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); 115 if (fileName.IndexOf(".xlsx") > 0) // 2007版本 116 workbook = new XSSFWorkbook(fs); 117 else if (fileName.IndexOf(".xls") > 0) // 2003版本 118 workbook = new HSSFWorkbook(fs); 119 120 if (sheetName != null) 121 { 122 sheet = workbook.GetSheet(sheetName); 123 if (sheet == null) //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet 124 { 125 sheet = workbook.GetSheetAt(0); 126 } 127 } 128 else 129 { 130 sheet = workbook.GetSheetAt(0); 131 } 132 if (sheet != null) 133 { 134 IRow firstRow = sheet.GetRow(0); 135 int cellCount = firstRow.LastCellNum; //一行最后一个cell的编号 即总的列数 136 137 if (isFirstRowColumn) 138 { 139 for (int i = firstRow.FirstCellNum; i < cellCount; ++i) 140 { 141 ICell cell = firstRow.GetCell(i); 142 if (cell != null) 143 { 144 string cellValue = cell.StringCellValue; 145 if (cellValue != null) 146 { 147 DataColumn column = new DataColumn(cellValue); 148 data.Columns.Add(column); 149 } 150 } 151 } 152 startRow = sheet.FirstRowNum + 1; 153 } 154 else 155 { 156 startRow = sheet.FirstRowNum; 157 } 158 159 //最后一列的标号 160 int rowCount = sheet.LastRowNum; 161 for (int i = startRow; i <= rowCount; ++i) 162 { 163 IRow row = sheet.GetRow(i); 164 if (row == null) continue; //没有数据的行默认是null 165 166 DataRow dataRow = data.NewRow(); 167 168 for (int j = row.FirstCellNum; j < cellCount; ++j) 169 { 170 if (row.FirstCellNum != -1) 171 { 172 if (row.GetCell(j) != null) 173 { 174 if (row.GetCell(j).CellType == CellType.Formula &&row.GetCell(j).ColumnIndex ==5) 175 { 176 row.GetCell(j).SetCellType(CellType.String); 177 dataRow[j] = Math.Round(decimal.Parse(row.GetCell(j).StringCellValue),2); 178 } 179 else 180 { 181 dataRow[j] = row.GetCell(j).ToString(); 182 } 183 } //同理,没有数据的单元格都默认是null 184 185 } 186 187 } 188 data.Rows.Add(dataRow); 189 } 190 } 191 192 return data; 193 } 194 catch (Exception ex) 195 { 196 Console.WriteLine("Exception: " + ex.Message); 197 return null; 198 } 199 } 200 201 public void Dispose() 202 { 203 Dispose(true); 204 GC.SuppressFinalize(this); 205 } 206 207 protected virtual void Dispose(bool disposing) 208 { 209 if (!this.disposed) 210 { 211 if (disposing) 212 { 213 if (fs != null) 214 fs.Close(); 215 } 216 217 fs = null; 218 disposed = true; 219 } 220 } 221 } 222 }
使用
1 using (ExcelHelper excelHelper = new ExcelHelper(FileName)) 2 { 3 excellist = excelHelper.ExcelToDataTable("正误名单", true); 4 } 5 var ccount = excellist.Columns.Count; 6 var rcount = excellist.Rows.Count; 7 var str = ""; 8 var j = 0; 9 for (int i = 0; i < rcount; i+=j) 10 { 11 var strexerciseid = excellist.Rows[i][0].ToString(); 12 }
本文来自博客园,作者:鹤哥只手遮天,转载请注明原文链接:https://www.cnblogs.com/hegezhishouzhetian/p/10659282.html