Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1 Star 0 Fork 319

凤凰智能/视频学习+在线考试+题库+直播

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
Tags (8)
master
2.3
2.1.1.21041
2.0.8627
2.0.8499
2.0.8460
2.0.8384
2.0.8355
2.0Beta
master
Branches (1)
Tags (8)
master
2.3
2.1.1.21041
2.0.8627
2.0.8499
2.0.8460
2.0.8384
2.0.8355
2.0Beta
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
Tags (8)
master
2.3
2.1.1.21041
2.0.8627
2.0.8499
2.0.8460
2.0.8384
2.0.8355
2.0Beta
Excel.cs 15.13 KB
Copy Edit Raw Blame History
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
using Newtonsoft.Json.Linq;
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;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace Song.ViewData.Helper
{
public class Excel
{
#region 读取或解析Excel文件
/// <summary>
/// 从Excel中读取一个工作薄,生成Datatable对象。
/// </summary>
/// <param name="xlsFile">excel文件的物理地址</param>
/// <param name="sheetIndex">工作簿索引</param>
/// <param name="cfgFile">配置文件</param>
/// <returns></returns>
public static DataTable SheetToDatatable(string xlsFile, int sheetIndex, string cfgFile)
{
//创建工作薄对象
IWorkbook workbook = createWorkbook(xlsFile);
//判断是xls还是xlsx
string ext = Path.GetExtension(xlsFile).ToLower();
ISheet sheet = workbook.GetSheetAt(sheetIndex);
System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
DataTable dt = new DataTable();
//配置信息
DataTable dtConfig = Config(cfgFile);
try
{
rows.MoveNext();
//创建Datatable结构
IRow firsRow = createRow(ext, rows.Current);
for (int i = 0; i < firsRow.LastCellNum; i++)
{
ICell cell = firsRow.GetCell(i);
if (cell == null || string.IsNullOrWhiteSpace(cell.ToString())) continue;
dt.Columns.Add(new DataColumn(cell.ToString(), getColumnType(cell.ToString(), dtConfig)));
}
//导入工作薄的数据
while (rows.MoveNext())
{
IRow row = createRow(ext, rows.Current);
DataRow dr = dt.NewRow();
for (int i = 0; i < dt.Columns.Count; i++)
{
ICell cell = row.GetCell(i);
if (cell == null) continue;
string value = string.Empty;
//读取Excel格式,根据格式读取数据类型
switch (dt.Columns[i].DataType.FullName)
{
case "System.DateTime": //日期类型
try
{
if (DateUtil.IsValidExcelDate(cell.NumericCellValue))
{
try
{
value = cell.DateCellValue.ToString();
}
catch
{
value = cell.ToString();
}
}
else
{
value = cell.NumericCellValue.ToString();
}
}
catch
{
value = cell.ToString();
}
object obj = WeiSha.Core.Param.Method.ConvertToAnyValue.Get(value).ChangeType(dt.Columns[i].DataType);
dr[i] = obj == null ? DateTime.Now : (System.DateTime)obj;
break;
default:
value = getCellValue(cell, ext, workbook);
dr[i] = WeiSha.Core.Param.Method.ConvertToAnyValue.Get(value).ChangeType(dt.Columns[i].DataType);
break;
}
}
dt.Rows.Add(dr);
}
}
catch (Exception ex)
{
return dt;
}
return dt;
}
/// <summary>
/// 获取Excel单元格的值
/// </summary>
/// <param name="cell"></param>
/// <param name="ext">excel的扩展名,用于判断是xls还是xlsx</param>
/// <param name="workbook">文档对象</param>
/// <returns></returns>
private static string getCellValue(ICell cell, string ext, IWorkbook workbook)
{
string val = string.Empty;
switch (cell.CellType)
{
case CellType.Numeric:
if (HSSFDateUtil.IsCellDateFormatted(cell))//日期类型
{
val = cell.DateCellValue.ToString("yyyy-MM-dd");
}
else//其他数字类型
{
val = cell.NumericCellValue.ToString();
}
break;
case CellType.Blank:
val = string.Empty;
break;
case CellType.Formula: //此处是处理公式数据,获取公式执行后的值
if (ext == ".xlsx")
{
XSSFFormulaEvaluator eva = new XSSFFormulaEvaluator(workbook);
if (eva.Evaluate(cell).CellType == CellType.Numeric)
{
val = eva.Evaluate(cell).NumberValue.ToString();
}
else
{
val = eva.Evaluate(cell).StringValue;
}
}
else
{
HSSFFormulaEvaluator eva = new HSSFFormulaEvaluator(workbook);
if (eva.Evaluate(cell).CellType == CellType.Numeric)
{
val = eva.Evaluate(cell).NumberValue.ToString();
}
else
{
val = eva.Evaluate(cell).StringValue;
}
}
break;
default:
val = cell.StringCellValue;
break;
}
return string.IsNullOrWhiteSpace(val) ? val : val.Trim();
}
/// <summary>
/// 获取文档中的所有工作薄
/// </summary>
/// <param name="xlsFile"></param>
/// <returns>name:工作簿名称; count:记录数</returns>
public static JArray Sheets(string xlsFile)
{
JArray arr = new JArray();
//创建工作薄对象
IWorkbook workbook = createWorkbook(xlsFile);
int sheetNum = workbook.NumberOfSheets;
for (int i = 0; i < sheetNum; i++)
{
JObject jo = new JObject();
jo.Add("name", workbook.GetSheetAt(i).SheetName);
jo.Add("count", workbook.GetSheetAt(i).LastRowNum);
arr.Add(jo);
}
return arr;
}
/// <summary>
/// 获取工作薄的列表,即第一行的标题
/// </summary>
/// <param name="xlsFile">excel的物理地址</param>
/// <param name="sheetIndex">工作簿的索引</param>
/// <returns>name:工作簿名称;index:工作簿索引;count:记录数;columns:列名 </returns>
public static JObject Columns(string xlsFile, int sheetIndex)
{
//excel文档对象
IWorkbook workbook = createWorkbook(xlsFile);
//工作簿对象
ISheet sheet = workbook.GetSheetAt(sheetIndex);
JObject jo = new JObject();
jo.Add("name", sheet.SheetName);
jo.Add("index", sheetIndex);
jo.Add("count", sheet.LastRowNum);
JArray arr = new JArray();
//获取列
System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
rows.MoveNext();
IRow firsRow = (IRow)rows.Current;
for (int i = 0; i < firsRow.LastCellNum; i++)
{
ICell cell = firsRow.GetCell(i);
JObject o = new JObject();
o.Add("Name", cell == null ? "(null)" + i : cell.ToString());
arr.Add(o);
}
jo.Add("columns", arr);
return jo;
}
/// <summary>
/// 通过Excel文档,创建对应的处理对象
/// </summary>
/// <param name="xlsFile"></param>
/// <returns></returns>
public static IWorkbook createWorkbook(string xlsFile)
{
//创建工作薄对象
IWorkbook workbook = null;
using (FileStream file = new FileStream(xlsFile, FileMode.Open, FileAccess.Read))
{
//根据扩展名判断excel版本
string ext = Path.GetExtension(xlsFile).ToLower();
if (ext == ".xls") workbook = new HSSFWorkbook(file);
if (ext == ".xlsx") workbook = new XSSFWorkbook(file);
}
return workbook;
}
/// <summary>
/// 创建行对象
/// </summary>
/// <param name="ext"></param>
/// <param name="obj"></param>
/// <returns></returns>
private static IRow createRow(string ext, object obj)
{
IRow row = null;
if (ext == ".xls") row = (HSSFRow)obj;
if (ext == ".xlsx") row = (XSSFRow)obj;
return row;
}
/// <summary>
/// 获取列名与字段名的对应关系的设置
/// </summary>
/// <param name="file">配置文件的路径(绝对路径)</param>
public static DataTable Config(string file)
{
//构造表
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Column", Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("Field", Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("DataType", Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("Format", Type.GetType("System.String")));
//配置文件的路径
//string path = App.Get["ExcelInputConfig"].VirtualPath;
string config = WeiSha.Core.Server.MapPath(file);
if (!System.IO.File.Exists(config)) return dt;
//填充表
XmlDocument resXml = new XmlDocument();
resXml.Load(config);
XmlNodeList nodes = resXml.GetElementsByTagName("item");
foreach (XmlNode n in nodes)
{
XmlElement el = (XmlElement)n;
DataRow dr = dt.NewRow();
dr["Column"] = el.Attributes["Column"].Value;
dr["Field"] = el.Attributes["Field"].Value;
dr["DataType"] = el.Attributes["DataType"] != null ? el.Attributes["DataType"].Value : null;
dr["Format"] = el.Attributes["Format"] != null ? el.Attributes["Format"].Value : null;
dt.Rows.Add(dr);
}
return dt;
}
/// <summary>
/// 获取列的数据类型
/// </summary>
/// <param name="colname"></param>
/// <param name="dtConfig">配置信息</param>
/// <returns></returns>
private static System.Type getColumnType(string colname, DataTable dtConfig)
{
System.Type type = null;
foreach (DataRow dr in dtConfig.Rows)
{
if (colname.ToLower().Trim() == dr["Column"].ToString().ToLower().Trim())
{
if (dr["DataType"].ToString().ToLower().Trim() == "date") type = Type.GetType("System.DateTime");
}
}
if (type == null) type = Type.GetType("System.String");
return type;
}
#endregion
#region 删除文件
/// <summary>
/// 删除文件
/// </summary>
/// <param name="file">文件名</param>
/// <param name="folder">文件所在的文件夹</param>
/// <param name="uploadkey">上传文件所在的根文件夹,此处为配置项的Key值,配置项来自web.config的Upload节点</param>
public static bool DeleteFile(string file, string folder, string uploadkey)
{
//校验文件
if (string.IsNullOrWhiteSpace(file)) return false;
file = WeiSha.Core.Server.LegalName(file);
if (string.IsNullOrWhiteSpace(file)) return false;
//上传的根文件夹
string rootPhy = WeiSha.Core.Upload.Get[uploadkey].Physics;
//校验文件夹
if (!string.IsNullOrWhiteSpace(folder)) rootPhy += WeiSha.Core.Server.LegalPath(folder);
if (!rootPhy.EndsWith("\\")) rootPhy += "\\";
if (!Directory.Exists(rootPhy)) return false;
//删除文件
string filePath = rootPhy + file;
if (!File.Exists(filePath)) return false;
File.Delete(filePath);
return true;
}
/// <summary>
/// 获取文件列表
/// </summary>
/// <param name="folder">文件所在的文件夹</param>
/// <param name="uploadkey">上传文件所在的根文件夹,此处为配置项的Key值,配置项来自web.config的Upload节点</param>
/// <param name="rule">按文件名查询时的规则</param>
/// <returns></returns>
public static JArray Files(string folder, string uploadkey, string rule)
{
//上传的根文件夹
string rootPhy = WeiSha.Core.Upload.Get[uploadkey].Physics; //物理路径
string rootVir = WeiSha.Core.Upload.Get[uploadkey].Virtual; //虚拟路径
//校验文件夹
if (!string.IsNullOrWhiteSpace(folder))
{
folder = WeiSha.Core.Server.LegalPath(folder);
rootPhy += folder;
rootVir += folder;
if (!rootPhy.EndsWith("\\")) rootPhy += "\\";
if (!rootVir.EndsWith("/")) rootVir += "/";
}
//
JArray jarr = new JArray();
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(rootPhy);
if (!dir.Exists) return jarr;
//
if (string.IsNullOrWhiteSpace(rule)) rule = "*.xls";
FileInfo[] files = dir.GetFiles(rule).OrderByDescending(f => f.CreationTime).ToArray();
foreach (FileInfo f in files)
{
string name = Path.GetFileNameWithoutExtension(f.Name);
JObject jo = new JObject();
jo.Add("name", name);
jo.Add("file", f.Name);
jo.Add("url", rootVir + f.Name);
jo.Add("date", f.CreationTime);
jo.Add("size", f.Length);
jarr.Add(jo);
}
return jarr;
}
#endregion
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

社区版完全免费,功能完整。直播、视频学习、试题练习、测试、考试、学习证明、成绩打印,实现"学、练、考"一体。适用于在线教育、知识付费、企业内训、员工考评、企业商学院等。
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/PhoenixGroup/LearningSystem.git
git@gitee.com:PhoenixGroup/LearningSystem.git
PhoenixGroup
LearningSystem
视频学习+在线考试+题库+直播
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /