using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; namespace WX.CRM.Common { public class NPOIHelper { #region 读取EXCEL数据 static HSSFWorkbook hssfworkbook; static IWorkbook xssfworkbook; public static void InitializeWorkbook_xls(string filePath) { using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { hssfworkbook = new HSSFWorkbook(file); } } public static void InitializeWorkbook_xlsx(string filePath) { using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { //xssfworkbook = new XSSFWorkbook(file); xssfworkbook = WorkbookFactory.Create(file); } } public static List> ConvertToDataTable2(int[] rs) { ISheet sheet = hssfworkbook.GetSheetAt(0); System.Collections.IEnumerator rows = sheet.GetRowEnumerator(); List> dt = new List>(); int rowindex = 0; while (rows.MoveNext()) { IRow row = (HSSFRow)rows.Current; if (rs.Contains(rowindex)) { List dr = row.Cells.Select(cell => cell.ToString().Trim()).ToList(); dt.Add(dr); } rowindex++; } return dt; } public static DataTable ConvertToDataTable(int skip = 0) { ISheet sheet = hssfworkbook.GetSheetAt(0); System.Collections.IEnumerator rows = sheet.GetRowEnumerator(); DataTable dt = new DataTable(); int rowindex = 0; int cols = 0; while (rows.MoveNext()) { if (skip-- > 0) { continue; } IRow row = (HSSFRow)rows.Current; if (rowindex == 0) { cols = row.LastCellNum; for (int i = 0; i < cols; i++) { ICell cell = row.GetCell(i); if (cell == null) { dt.Columns.Add(Convert.ToChar(('A') + i).ToString()); } else { dt.Columns.Add(cell.ToString().Trim() + i.ToString()); } } } else { DataRow dr = dt.NewRow(); for (int i = 0; i < cols; i++) { ICell cell = row.GetCell(i); if (cell == null) { dr[i] = null; } else { dr[i] = cell.ToString().Trim(); } } dt.Rows.Add(dr); } rowindex++; } return dt; } public static DataSet ConvertToDataTable_xlsx(int[] datecol, out int total) { total = 0; DataSet ds = new DataSet(); ISheet sheet = xssfworkbook.GetSheetAt(0); System.Collections.IEnumerator rows = sheet.GetRowEnumerator(); DataTable dt = new DataTable(); int cols = 0; bool beginread = false, hasreadhead = false; string _tradeDate = ""; while (rows.MoveNext()) { IRow row = (XSSFRow)rows.Current; if (row.GetCell(0).ToString().Trim().IndexOf("交易商代码:") == 0) { _tradeDate = row.GetCell(2).ToString().Replace("日期:", "").Trim(); beginread = true; continue; } if (row.GetCell(0).ToString().Trim().IndexOf("合计") == 0) { beginread = false; continue; } if (!beginread) { continue; } if (!hasreadhead) { cols = row.LastCellNum; for (int i = 0; i < cols; i++) { ICell cell = row.GetCell(i); if (cell == null) { dt.Columns.Add(Convert.ToChar(('A') + i).ToString()); } else { dt.Columns.Add(cell.ToString().Trim()); } } dt.Columns.Add("TradeDate");//交易日期 hasreadhead = true; } else { if (row.GetCell(0).ToString().Trim() == "成交序号") { continue; } DataRow dr = dt.NewRow(); bool isemptyrow = true; for (int i = 0; i < cols; i++) { ICell cell = row.GetCell(i); if (cell == null) { dr[i] = null; } else { #region 检查日期 bool isdt = false; DateTime _dt = DateTime.MinValue; if (datecol != null && datecol.Contains(i)) { try { _dt = cell.DateCellValue; if (_dt != DateTime.MinValue) isdt = true; } catch { isdt = false; } } #endregion if (isdt) dr[i] = _dt.ToString(); else dr[i] = cell.ToString().Trim(); if (isemptyrow && !string.IsNullOrEmpty(dr[i].ToString())) { isemptyrow = false; } } } dr[cols] = _tradeDate; if (!isemptyrow) dt.Rows.Add(dr); } } ds.Tables.Add(dt); return ds; } public static DataTable ConvertToDataTable_xlsx() { DataTable dt = new DataTable(); ISheet sheet = xssfworkbook.GetSheetAt(0); System.Collections.IEnumerator rows = sheet.GetRowEnumerator(); int cols = 0; if (rows.MoveNext()) { IRow row = (IRow)rows.Current; cols = row.LastCellNum; for (int i = 0; i < cols; i++) { ICell cell = row.GetCell(i); if (cell == null) { dt.Columns.Add(Convert.ToChar(('A') + i).ToString()); } else { dt.Columns.Add(cell.ToString().Trim()); } } } while (rows.MoveNext()) { IRow row = (IRow)rows.Current; { DataRow dr = dt.NewRow(); for (int i = 0; i < cols; i++) { ICell cell = row.GetCell(i); if (cell == null) { dr[i] = null; } else { dr[i] = cell.ToString().Trim(); } } dt.Rows.Add(dr); } } return dt; } /// /// 转让盈亏明细导入 /// /// /// public static DataTable ImportExcelX(string filePath) { IWorkbook workbook = null; using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { workbook = WorkbookFactory.Create(filePath); } ISheet sheet = workbook.GetSheetAt(0); System.Collections.IEnumerator rows = sheet.GetRowEnumerator(); DataTable dt = new DataTable(); int cols = 0, colindex = 0; int rowcount = 0; while (rows.MoveNext()) { rowcount++; IRow row = (IRow)rows.Current; cols = row.LastCellNum; for (; colindex < cols; colindex++) { dt.Columns.Add(Convert.ToChar(('A') + colindex).ToString()); } DataRow dr = dt.NewRow(); for (int i = 0; i < cols; i++) { ICell cell = row.GetCell(i); if (cell == null) { dr[i] = null; } else { dr[i] = cell.ToString().Trim(); } } dt.Rows.Add(dr); } return dt; } #endregion } }