| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Web;
- using NPOI.SS.UserModel;
- /// <summary>
- /// 操作ExcelFile的简单方法
- /// xuwei add 2024-01-05
- /// </summary>
- public class ExcelFile
- {
- public IWorkbook GetWorkbook;
- public ExcelFile()
- {
- }
- public ExcelFile(string fileName)
- {
- Open(fileName);
- }
- //打开excel
- public void Open(string fileName)
- {
- FileStream fs = File.Open(HttpContext.Current.Server.MapPath(fileName), FileMode.Open, FileAccess.Read);
- GetWorkbook = WorkbookFactory.Create(fs);
- fs.Close();
- }
- //保存excel
- public void SaveAs(string fileName)
- {
- MemoryStream ms = new MemoryStream();
- GetWorkbook.Write(ms);
- FileStream nfs = new FileStream(HttpContext.Current.Server.MapPath(fileName), FileMode.Create, FileAccess.Write);
- nfs.Write(ms.ToArray(), 0, ms.ToArray().Length);
- nfs.Flush();
- nfs.Close();
- ms.Close();
- }
- }
|