BaoKai_202508-Wms-Jingwang..../WMS.FrmOut/NPOIHelper.cs

159 lines
5.3 KiB
C#
Raw Permalink Normal View History

2025-08-24 09:35:55 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.HSSF.Record;
using WMS.Common;
using NPOI.XSSF.UserModel;
namespace WMS.FrmCK
{
class NPOIHelper
{
private static string GetCellValue(ICell cell)
{
if (cell == null)
return string.Empty;
switch (cell.CellType)
{
case CellType.Blank:
return string.Empty;
case CellType.Boolean:
return cell.BooleanCellValue.ToString();
case CellType.Error:
return cell.ErrorCellValue.ToString();
case CellType.Numeric:
case CellType.Unknown:
default:
return cell.ToString();//This is a trick to get the correct value of the cell. NumericCellValue will return a numeric value no matter the cell value is a date or a number
case CellType.String:
return cell.StringCellValue;
case CellType.Formula:
try
{
HSSFFormulaEvaluator e = new HSSFFormulaEvaluator(cell.Sheet.Workbook);
e.EvaluateInCell(cell);
return cell.ToString();
}
catch
{
return cell.NumericCellValue.ToString();
}
}
}
public static DataTable Import(OpenFileDialog fileDialog)
{
DataTable table = new DataTable();
Stream excelFileStream;
try
{
excelFileStream = fileDialog.OpenFile();
}
catch (IOException e)
{
if (e.ToString().Contains("正由另一进程使用"))
{
SystemCommon.ShowErrorMessageBox("所选Excel文件处于打开状态请关闭后重试");
}
else
{
SystemCommon.ShowErrorMessageBox("文件读取失败");
}
return null;
}
if (excelFileStream == null)
{
excelFileStream.Close();
SystemCommon.ShowErrorMessageBox("文件读取失败");
return null;
}
//读取excel
using (excelFileStream)
{
IWorkbook workbook = new HSSFWorkbook();
if (fileDialog.SafeFileName.Substring(fileDialog.SafeFileName.Length - 1, 1) == "s")
{
try
{
workbook = new HSSFWorkbook(excelFileStream);//xls
}
catch (LeftoverDataException)
{
SystemCommon.ShowErrorMessageBox("导入失败请将所选Excel文件另存为一个新的Excel文件后再次导入");
return null;
}
catch (IOException)
{
SystemCommon.ShowErrorMessageBox("导入失败请将所选Excel文件另存为一个新的Excel文件后再次导入");
return null;
}
catch (Exception)
{
SystemCommon.ShowErrorMessageBox("导入失败读取Excel文件时遇到未知错误");
return null;
}
}
else
{
workbook = new XSSFWorkbook(excelFileStream);//xlsx
}
ISheet sheet = workbook.GetSheetAt(0);//取第一个表
IRow headerRow = sheet.GetRow(0);//第一行为标题行
if (headerRow == null)
{
SystemCommon.ShowErrorMessageBox("导入失败所选Excel没有数据");
return null;
}
int cellCount = headerRow.LastCellNum;//LastCellNum = PhysicalNumberOfCells
int rowCount = sheet.LastRowNum;//LastRowNum = PhysicalNumberOfRows - 1
//handling header.
for (int i = headerRow.FirstCellNum; i < cellCount; i++)
{
DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue);
table.Columns.Add(column);
}
for (int i = (sheet.FirstRowNum + 1); i <= rowCount; i++)
{
IRow row = sheet.GetRow(i);
DataRow dataRow = table.NewRow();
if (row != null)
{
for (int j = row.FirstCellNum; j < cellCount; j++)
{
if (row.GetCell(j) != null)
{
dataRow[j] = GetCellValue(row.GetCell(j));
}
}
}
table.Rows.Add(dataRow);
}
}
return table;
}
}
}