当前位置:   article > 正文

NpoiHelper_nopihelper

nopihelper

NpoiHelper

using DBUtility;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
namespace NpoiHelper
{
	internal class NpoiHelper
	{
		public static DataTable ExcelToTable(string file)
		{
			DataTable dataTable = new DataTable();
			string a = Path.GetExtension(file).ToLower();
			DataTable result;
			using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
			{
				bool flag = a == ".xlsx";
				IWorkbook workbook;
				if (flag)
				{
					workbook = new XSSFWorkbook(fileStream);
				}
				else
				{
					bool flag2 = a == ".xls";
					if (flag2)
					{
						workbook = new HSSFWorkbook(fileStream);
					}
					else
					{
						workbook = null;
					}
				}
				bool flag3 = workbook == null;
				if (flag3)
				{
					result = null;
					return result;
				}
				ISheet sheetAt = workbook.GetSheetAt(0);
				IRow row = sheetAt.GetRow(sheetAt.FirstRowNum);
				List<int> list = new List<int>();
				for (int i = 0; i < (int)row.LastCellNum; i++)
				{
					object valueType = NpoiHelper.GetValueType(row.GetCell(i));
					bool flag4 = valueType == null || valueType.ToString() == string.Empty;
					if (flag4)
					{
						dataTable.Columns.Add(new DataColumn("Columns" + i.ToString()));
					}
					else
					{
						dataTable.Columns.Add(new DataColumn(valueType.ToString()));
					}
					list.Add(i);
				}
				for (int j = sheetAt.FirstRowNum + 1; j <= sheetAt.LastRowNum; j++)
				{
					DataRow dataRow = dataTable.NewRow();
					bool flag5 = false;
					foreach (int current in list)
					{
						dataRow[current] = NpoiHelper.GetValueType(sheetAt.GetRow(j).GetCell(current));
						bool flag6 = dataRow[current] != null && dataRow[current].ToString() != string.Empty;
						if (flag6)
						{
							flag5 = true;
						}
					}
					bool flag7 = flag5;
					if (flag7)
					{
						dataTable.Rows.Add(dataRow);
					}
				}
			}
			result = dataTable;
			return result;
		}
		public static void TableToExcel(DataTable dt, string file)
		{
			string a = Path.GetExtension(file).ToLower();
			bool flag = a == ".xlsx";
			IWorkbook workbook;
			if (flag)
			{
				workbook = new XSSFWorkbook();
			}
			else
			{
				bool flag2 = a == ".xls";
				if (flag2)
				{
					workbook = new HSSFWorkbook();
				}
				else
				{
					workbook = null;
				}
			}
			bool flag3 = workbook == null;
			if (!flag3)
			{
				ISheet sheet = string.IsNullOrEmpty(dt.TableName) ? workbook.CreateSheet("Sheet1") : workbook.CreateSheet(dt.TableName);
				IRow row = sheet.CreateRow(0);
				for (int i = 0; i < dt.Columns.Count; i++)
				{
					ICell cell = row.CreateCell(i);
					cell.SetCellValue(dt.Columns[i].ColumnName);
				}
				for (int j = 0; j < dt.Rows.Count; j++)
				{
					IRow row2 = sheet.CreateRow(j + 1);
					for (int k = 0; k < dt.Columns.Count; k++)
					{
						ICell cell2 = row2.CreateCell(k);
						bool flag4 = PageValidate.IsNumber(dt.Rows[j][k].ToString());
						if (flag4)
						{
							cell2.SetCellValue((double)Convert.ToInt32(dt.Rows[j][k].ToString()));
						}
						else
						{
							bool flag5 = PageValidate.IsDecimal(dt.Rows[j][k].ToString());
							if (flag5)
							{
								cell2.SetCellValue(Convert.ToDouble(dt.Rows[j][k].ToString()));
							}
							else
							{
								cell2.SetCellValue(dt.Rows[j][k].ToString());
							}
						}
					}
				}
				MemoryStream memoryStream = new MemoryStream();
				workbook.Write(memoryStream);
				byte[] array = memoryStream.ToArray();
				using (FileStream fileStream = new FileStream(file, FileMode.Create, FileAccess.Write))
				{
					fileStream.Write(array, 0, array.Length);
					fileStream.Flush();
				}
			}
		}
		private static object GetValueType(ICell cell)
		{
			bool flag = cell == null;
			object result;
			if (flag)
			{
				result = null;
			}
			else
			{
				switch (cell.CellType)
				{
				case CellType.Numeric:
					result = cell.NumericCellValue;
					return result;
				case CellType.String:
					result = cell.StringCellValue;
					return result;
				case CellType.Blank:
					result = null;
					return result;
				case CellType.Boolean:
					result = cell.BooleanCellValue;
					return result;
				case CellType.Error:
					result = cell.ErrorCellValue;
					return result;
				}
				result = "=" + cell.CellFormula;
			}
			return result;
		}
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/216188?site
推荐阅读
相关标签
  

闽ICP备14008679号