| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- using NPOI.HSSF.UserModel;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- //using NPOI.XSSF.UserModel;
- using NPOI.SS.UserModel;
- using NPOI.SS.Util;
- namespace Core.StlMes.Client.PlnSaleOrd
- {
- public class NPOIHelper
- {
- /// <summary>读取excel
- /// 默认第一行为标头
- /// </summary>
- /// <param name="strFileName">excel文档路径</param>
- /// <returns></returns>
- public static DataTable Import(string strFileName)
- {
- DataTable dt = new DataTable();
- HSSFWorkbook hssfworkbook;
- using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read))
- {
- hssfworkbook = new HSSFWorkbook(file);
- }
- ISheet sheet = hssfworkbook.GetSheetAt(0);
- System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
- //HSSFRow headerRow = sheet.GetRow(0);
- IRow headerRow = sheet.GetRow(0);
- int cellCount = headerRow.LastCellNum;
- for (int j = 0; j < cellCount; j++)
- {
- ICell cell = headerRow.GetCell(j);
- dt.Columns.Add(cell.ToString());
- }
- for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
- {
- IRow row = sheet.GetRow(i);
- DataRow dataRow = dt.NewRow();
- for (int j = row.FirstCellNum; j < cellCount; j++)
- {
- if (row.GetCell(j) != null)
- dataRow[j] = row.GetCell(j).ToString();
- }
- dt.Rows.Add(dataRow);
- }
- return dt;
- }
- }
- }
|