| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using Business;
- using Business.Core.Utilities;
- using Business.Dto;
- using Business.ResourceExamineManagement;
- using NPOI.HSSF.UserModel;
- using NPOI.SS.UserModel;
- using NPOI.XSSF.UserModel;
- using Volo.Abp.Application.Services;
- using Volo.Abp.MultiTenancy;
- namespace Business.VSM
- {
- /// <summary>
- /// Sqe数据导入接口实现
- /// </summary>
- public class SqeExcelService : ApplicationService, ISqeExcelService
- {
- private readonly ICurrentTenant _currentTenant;
- //LoggerHelper WLLog;
- public SqeExcelService()
- {
- // WLLog = new LoggerHelper();
- }
- /// <summary>
- /// 导入Excel文件的sheetName的sheet页面到数据表
- /// </summary>
- /// <param name="filePath"></param>
- /// <param name="sheetName"></param>
- /// <returns></returns>
- public DataTable ImportExcelSheetToDataTable_test(string filePath, string sheetName)
- {
- FileStream file = null;
- DataTable dt = new DataTable();
- int index = 0;
- try
- {
- file = new FileStream(filePath, FileMode.Open, FileAccess.Read);
- //打开Excel对象
- IWorkbook workbook;
- IRow row;
- string fileExt = Path.GetExtension(filePath).ToLower();
- workbook = null;
- if (fileExt == ".xlsx")
- {
- workbook = new XSSFWorkbook(file);
- }
- else
- {
- if (fileExt == ".xls")
- {
- workbook = new HSSFWorkbook(file);
- }
- else
- {
- workbook = null;
- }
- }
- if (workbook == null) { return null; }
- //Excel的Sheet对象
- NPOI.SS.UserModel.ISheet sheet = workbook.GetSheet(sheetName);
- //获取Sheet的所有的行
- System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
- //int colCountMax = 0;
- dt.TableName = sheetName;
- //判断是否拥有首行
- bool firstLine = true;
- //if (!haveColumnName)
- //{
- // firstLine = false;
- //}
- bool bDate = false;
- while (rows.MoveNext())
- {
- index++;
- bDate = false;
- if (fileExt == ".xlsx")
- {
- row = (XSSFRow)rows.Current;
- }
- else
- {
- row = (HSSFRow)rows.Current;
- }
- if (!firstLine)
- {
- //生成表结构
- //仅当首次调用时会进行循环生成
- for (int i = dt.Columns.Count; i < row.Cells.Count; i++)
- {
- dt.Columns.Add(Convert.ToChar(((int)'A') + i).ToString());
- }
- }
- else
- {
- //依据首行进行字段生成
- for (int i = dt.Columns.Count; i < row.Cells.Count; i++)
- {
- dt.Columns.Add(row.Cells[i].ToString());
- }
- }
- //没有首行,生成数据
- if (!firstLine)
- {
- DataRow dr = dt.NewRow();
- for (int i = 0; i < row.LastCellNum - 1; i++)
- {
- NPOI.SS.UserModel.ICell cell = row.GetCell(i);
- String s = "";
- if (cell != null)
- {
- switch (cell.CellType)
- {
- case NPOI.SS.UserModel.CellType.Unknown:
- break;
- case NPOI.SS.UserModel.CellType.Numeric:
- if (HSSFDateUtil.IsCellDateFormatted(cell))
- {
- if (index < 6)
- {
- s = cell.DateCellValue.ToString();
- }
- else
- {
- s = cell.ToString();
- }
- }
- else
- {
- s = cell.NumericCellValue.ToString();
- }
- break;
- case NPOI.SS.UserModel.CellType.String:
- s = cell.StringCellValue;
- break;
- case NPOI.SS.UserModel.CellType.Formula:
- if (i == 0 || i == 1)
- {
- s = cell.StringCellValue;
- }
- else
- {
- try
- {
- if (cell.CellFormula == "序号")
- {
- s = cell.ToString();
- }
- else
- {
- s = cell.NumericCellValue != null ? cell.NumericCellValue.ToString() : null;
- }
- }
- catch (Exception ex)
- {
- //WLLog.Error("Excel导入问题:", ex);
- new NLogHelper("SqeExcelService").WriteLog("ImportExcelSheetToDataTable", "Excel导入问题" + ex.Message, _currentTenant.Id.ToString());
- }
- }
- break;
- case NPOI.SS.UserModel.CellType.Blank:
- if (bDate)
- {
- //对于日期的特殊处理
- s = dr[i - 1].ToString();
- }
- else
- {
- s = cell.StringCellValue;
- }
- break;
- case NPOI.SS.UserModel.CellType.Boolean:
- break;
- case NPOI.SS.UserModel.CellType.Error:
- break;
- default:
- break;
- }
- }
- dr[i] = s;
- }
- dt.Rows.Add(dr);
- }
- firstLine = false;
- }
- return dt;
- }
- catch (Exception ex)
- {
- //WLLog.Error("Excel导入问题:", ex);
- new NLogHelper("SqeExcelService").WriteLog("ImportExcelSheetToDataTable", "Excel导入问题" + ex.Message, _currentTenant.Id.ToString());
- }
- finally
- {
- if (file != null)
- {
- file.Close();
- }
- }
- return dt;
- }
- public DataTable ImportExcelSheetToDataTable_process(string filePath, string sheetName)
- {
- FileStream file = null;
- DataTable dt = new DataTable();
- int index = 0;
- try
- {
- file = new FileStream(filePath, FileMode.Open, FileAccess.Read);
- //打开Excel对象
- IWorkbook workbook;
- IRow row;
- string fileExt = Path.GetExtension(filePath).ToLower();
- workbook = null;
- if (fileExt == ".xlsx")
- {
- workbook = new XSSFWorkbook(file);
- }
- else
- {
- if (fileExt == ".xls")
- {
- workbook = new HSSFWorkbook(file);
- }
- else
- {
- workbook = null;
- }
- }
- if (workbook == null) { return null; }
- //Excel的Sheet对象
- NPOI.SS.UserModel.ISheet sheet = workbook.GetSheet(sheetName);
- //获取Sheet的所有的行
- System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
- //int colCountMax = 0;
- dt.TableName = sheetName;
- //判断是否拥有首行
- bool firstLine = true;
- //if (!haveColumnName)
- //{
- // firstLine = false;
- //}
- bool bDate = false;
- while (rows.MoveNext())
- {
- index++;
- bDate = false;
- if (fileExt == ".xlsx")
- {
- row = (XSSFRow)rows.Current;
- }
- else
- {
- row = (HSSFRow)rows.Current;
- }
- if (!firstLine)
- {
- //生成表结构
- //仅当首次调用时会进行循环生成
- for (int i = dt.Columns.Count; i < row.Cells.Count; i++)
- {
- dt.Columns.Add(Convert.ToChar(((int)'A') + i).ToString());
- }
- }
- else
- {
- //依据首行进行字段生成
- for (int i = dt.Columns.Count; i < row.Cells.Count; i++)
- {
- dt.Columns.Add(row.Cells[i].ToString());
- }
- }
- //没有首行,生成数据
- if (!firstLine)
- {
- DataRow dr = dt.NewRow();
- for (int i = 0; i < row.LastCellNum - 1; i++)
- {
- NPOI.SS.UserModel.ICell cell = row.GetCell(i);
- String s = "";
- if (cell != null)
- {
- switch (cell.CellType)
- {
- case NPOI.SS.UserModel.CellType.Unknown:
- break;
- case NPOI.SS.UserModel.CellType.Numeric:
- if (HSSFDateUtil.IsCellDateFormatted(cell))
- {
- if (index < 6)
- {
- s = cell.DateCellValue.ToString();
- }
- else
- {
- s = cell.ToString();
- }
- }
- else
- {
- s = cell.NumericCellValue.ToString();
- }
- break;
- case NPOI.SS.UserModel.CellType.String:
- s = cell.StringCellValue;
- break;
- case NPOI.SS.UserModel.CellType.Formula:
- try
- {
- if (cell.CellFormula == "序号")
- {
- s = cell.ToString();
- }
- else
- {
- s = cell.NumericCellValue != null ? cell.NumericCellValue.ToString() : null;
- }
- }
- catch (Exception ex)
- {
- //WLLog.Error("Excel导入问题:", ex);
- new NLogHelper("SqeExcelService").WriteLog("ImportExcelSheetToDataTable", "Excel导入问题" + ex.Message, _currentTenant.Id.ToString());
- }
- break;
- case NPOI.SS.UserModel.CellType.Blank:
- if (bDate)
- {
- //对于日期的特殊处理
- s = dr[i - 1].ToString();
- }
- else
- {
- s = cell.StringCellValue;
- }
- break;
- case NPOI.SS.UserModel.CellType.Boolean:
- break;
- case NPOI.SS.UserModel.CellType.Error:
- break;
- default:
- break;
- }
- }
- dr[i] = s;
- }
- dt.Rows.Add(dr);
- }
- firstLine = false;
- }
- return dt;
- }
- catch (Exception ex)
- {
- //WLLog.Error("Excel导入问题:", ex);
- new NLogHelper("SqeExcelService").WriteLog("ImportExcelSheetToDataTable", "Excel导入问题" + ex.Message, _currentTenant.Id.ToString());
- }
- finally
- {
- if (file != null)
- {
- file.Close();
- }
- }
- return dt;
- }
- /// <summary>
- /// 获取Excel文件Sheet名称列表,与Jet引擎不一致,获取的时候没有$多余字符。
- /// </summary>
- /// <param name="filePath"></param>
- /// <returns></returns>
- public List<String> GetSheetNames(string filePath)
- {
- List<String> tables = null;
- FileStream file = null;
- try
- {
- tables = new List<string>();
- file = new FileStream(filePath, FileMode.Open, FileAccess.Read);
- HSSFWorkbook hssfworkbook = new HSSFWorkbook(file);
- int i = 0;
- while (true)
- {
- try
- {
- String s = hssfworkbook.GetSheetName(i);
- if (s != null)
- {
- tables.Add(s);
- i++;
- }
- else
- {
- break;
- }
- }
- catch (Exception ex)
- {
- break;
- }
- }
- }
- catch (Exception ex)
- {
- //WLLog.Error("Excel导入问题:", ex);
- new NLogHelper("SqeExcelService").WriteLog("ImportExcelSheetToDataTable", "Excel导入问题"+ex.Message, _currentTenant.Id.ToString());
- }
- finally
- {
- if (file != null)
- {
- file.Close();
- }
- }
- return tables;
- }
- }
- }
|