Nuget EPPlus的使用
EPPlus:网站
Excel Merge Operate
public class ExcelMergeOperate
{
private static Logger _logger = LogManager.GetCurrentClassLogger(); #region private method private static FileInfo CreateNewExcleFile(string excelPath)
{
FileInfo newFile = new FileInfo(excelPath);
if (newFile.Exists)
{
newFile.Delete(); // ensures we create a new workbook
newFile = new FileInfo(excelPath);
}
return newFile;
} private static void AddSheet(ExcelWorksheets fromSheets ,ExcelWorksheets toSheets,string defualtSheetName="")
{
foreach (var sheet in fromSheets)
{
//check name of worksheet, in case that worksheet with same name already exist exception will be thrown by EPPlus
string workSheetName = defualtSheetName != null?defualtSheetName:( sheet.Name!=null?sheet.Name : DateTime.Now.ToString("yyyyMMddhhssmmm"));
foreach (var masterSheet in toSheets)
{
if (sheet.Name == masterSheet.Name)
{
workSheetName = string.Format("{0}_{1}", workSheetName, DateTime.Now.ToString("yyyyMMddhhssmmm"));
}
}
sheet.ConditionalFormatting.RemoveAll();
//add new sheet
toSheets.Add(workSheetName, sheet);
}
} private static void MergeExcelsFromList<T>(List<T> date, string mergeExcelPath, string defualtSheetName = "date", bool create = false) where T : class
{ try
{
FileInfo newFile = create ? CreateNewExcleFile(mergeExcelPath) : new FileInfo(mergeExcelPath); ;
using (ExcelPackage masterPackage = new ExcelPackage(newFile))
{
//Create the Worksheet
var sheet = masterPackage.Workbook.Worksheets.Add(defualtSheetName);
//Create the format object to describe the text file
var format = new ExcelTextFormat();
format.TextQualifier = '"';
format.SkipLinesBeginning = ;
format.SkipLinesEnd = ;
//Now read the file into the sheet. Start from cell A1. Create a table with style 27. First row contains the header.
Console.WriteLine("Load the text file...");
//Load directories ordered by Name...
var range = sheet.Cells["A1"].LoadFromCollection(
from line in date
select line,
true, TableStyles.Medium9);
masterPackage.Save();
}
}
catch (Exception ex)
{
_logger.Error(ex.ToString());
}
} #endregion #region public method public static void MergeExcelsFromMutiExcelPath(List<string> excelPaths,string mergeExcelPath,bool create=false)
{
try
{
//excelPaths = new string[] { @"D:\Jimmy Team Project\Doc\2017-11-20日报表汇总\fang_Westwin Report _GlasessShop.xlsx", @"D:\Jimmy Team Project\Doc\2017-11-20日报表汇总\总结new2017-11-20.xlsx" };
//mergeExcelPath = @"D:\result.xlsx";
FileInfo newFile = create ? CreateNewExcleFile(mergeExcelPath): new FileInfo(mergeExcelPath);
using (ExcelPackage masterPackage = new ExcelPackage(newFile))
{
foreach (var file in excelPaths)
{ using (ExcelPackage pckg = new ExcelPackage(new FileInfo(file)))
{
AddSheet(pckg.Workbook.Worksheets, masterPackage.Workbook.Worksheets);
}
}
masterPackage.Save();
}
}
catch (Exception ex)
{
_logger.Error(ex.ToString());
}
} public static void MergeExcelsFromMutiExcelStream(List<Stream> streams, string mergeExcelPath, bool create = false)
{
try
{
FileInfo newFile = create ? CreateNewExcleFile(mergeExcelPath) : new FileInfo(mergeExcelPath);
using (ExcelPackage masterPackage = new ExcelPackage(newFile))
{
foreach (var stream in streams)
{
using (ExcelPackage pckg = new ExcelPackage(stream))
{
AddSheet(pckg.Workbook.Worksheets, masterPackage.Workbook.Worksheets);
}
}
masterPackage.Save();
}
}
catch (Exception ex)
{
_logger.Error(ex.ToString());
}
} public static void MergeExcelsFromCSVFile<T, M>(string csvPath, string mergeExcelPath, string defualtSheetName = "csv", bool create = false, string csvDelimiter = ",") where T : class where M : CsvClassMap<T>
{
using (FileStream fs = new FileStream(csvPath, FileMode.Open, FileAccess.Read))
{
List<T> date = CSVHelper<T>.GetEntityFromCSV<M>(fs, csvDelimiter);
MergeExcelsFromList(date, mergeExcelPath, defualtSheetName, create);
}
} public static void MergeExcelsFromCSVFile<T>(string csvPath, string mergeExcelPath, string defualtSheetName = "csv", bool create = false, string csvDelimiter = ",") where T : class
{
using (FileStream fs = new FileStream(csvPath, FileMode.Open, FileAccess.Read))
{
List<T> date = CSVHelper<T>.GetEntityFromCSV(fs, csvDelimiter);
MergeExcelsFromList(date, mergeExcelPath, defualtSheetName, create);
}
} #endregion }
general sheet from list
public void GenalralSheetFromList<T>(List<T> list, ref ExcelWorksheet ws, string dateFormate= "yyyy-mm-dd HH:mm:ss") where T:class
{
if (list == null || list.Count == ) return;
var startIndex = ;
var row = ;
PropertyInfo[] props = list[].GetType().GetProperties();
foreach (PropertyInfo pi in props)
{
ws.Cells[row, startIndex].Value = pi.Name;
if (pi.PropertyType == typeof(DateTime)|| pi.PropertyType == typeof(DateTime?))
{
ws.Column(startIndex).Style.Numberformat.Format = dateFormate;
}
startIndex++;
}
foreach (T item in list)
{
startIndex = ;
row++;
foreach (PropertyInfo pi in props)
{
ws.Cells[row, startIndex++].Value = item.GetType()
.GetProperty(pi.Name)
.GetValue(item, null); }
}
return;
}
Nuget EPPlus的使用的更多相关文章
- 利用 NUget包 EPPlus 实现数据导出到Excel(适用于MVC)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAvoAAABpCAIAAADEEBBGAAAJdElEQVR4nO3cy2ob5wLA8TxKnqTrrr
- 离线安装nuget包EPPlus
1先去https://www.nuget.org/packages/EPPlus/4.1.0下载,epplus.4.1.0.nupkg 2找到本地文件位置:H:\DOWNLOAD\ 3在vs的程序包管 ...
- 利用 NUget包 EPPlus 实现数据导出到Excel(适用于DTcms)
首先安装EPPlus 包
- C# NPOI导出Excel和EPPlus导出Excel比较
系统中经常会使用导出Excel的功能. 之前使用的是NPOI,但是导出数据行数多就报内存溢出. 最近看到EPPlus可以用来导出Excel,就自己测了下两者导出上的差异. NPIO官网地址:http: ...
- [转]比NPOI更討喜的Excel元件-EPPlus
本文转自:http://blog.darkthread.net/post-2012-05-12-epplus.aspx 前陣子發表 [潛盾機]將檔案結構匯成Excel文件,從網友佑翔的留言(特此感謝) ...
- 【netcore基础】.NET Core使用EPPlus实现MVC API里的Excel导出功能 配置中文表头
EPPlus 用来操作excel非常方便,不用依赖微软的office包,所以推荐使用. 下面是具体步骤和代码 首先用nuget安装 EPPlus.Core 我装的版本是 1.5.4 然后就可以用 Ex ...
- EPPlus实战篇——Excel写入
.net core 项目 可以向excel写入任何类型(T)的数据,只要T中的field的[Display(Name = "1233", Description = "# ...
- EPPlus实战篇——Excel读取
.net core 项目 可以从excel读取任何类型(T)的数据,只要T中的field的[Display(Name = "1233")]中的name==excel column ...
- C# NPOI导出Excel和EPPlus导出Excel
转自:http://www.cnblogs.com/tanpeng/p/6155749.html 系统中经常会使用导出Excel的功能.之前使用的是NPOI,但是导出数据行数多就报内存溢出. 最近看到 ...
随机推荐
- asp.net web form 的缺点
与mvc相比,web form 的缺点: 代码架构麻烦. 以页面或控件为单位,把逻辑放在了一起,代码架构简单.平铺直叙,修改.维护比较麻烦. 不方便单元测试. 功能堆加在了一起,不方便对单个的功能进 ...
- ASP.Net 获取Form表单值
新建一HtmlPage1.html,如下post发送() <body> <form enctype="multipart/form-data" action=&q ...
- QString 与 string转换
[1]QString 转换为string QString qString("好好学习天天向上"); std::string stdString = qString.toStdStr ...
- linux常用命令:touch 命令
linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件. 1.命令格式: touch [选项]... 文件... 2.命令参数: -a ...
- 微信小程序制作家庭记账本之一
制作的第一天,思索着制作手机端APP还是微信小程序,首先是想到制作APP但是各种收费让我不得不换一条路,所以开始制作小程序,下载了微信小程序开发工具,试着学习制作方法,但是似乎没有成效,但我坚信要一步 ...
- Android获取全局Context的方法
Android获取全局Context的方法 Android--应用全局获取Context - 超宇的博客 - CSDN博客https://blog.csdn.net/chaoyu168/article ...
- 读QT5.7源码(三)Q_OBJECT 和QMetaObject
Qt meta-object系统基于三个方面: 1.QObject提供一个基类,方便派生类使用meta-object系统的功能: 2.Q_OBJECT宏,在类的声明体内激活meta-object功 ...
- .NET 常用ORM之Gentle.Net
.Net常用的就是微软的EF框架和Nhibernate,这两个框架用的都比较多就不做详细介绍了,今天我们来看看Gentle.Net,Gentle.Net是一个开源的优秀O/R Mapping的对象持久 ...
- Array和ArrayList不同
Employee[] array = new Employee[10]; ArrayList<Employee> staff = new ArrayList<>(); 不同 A ...
- The Little Prince-12/14
The Little Prince-12/14 世界上有那么多的玫瑰花,但是只有你们是一直陪伴在我身边,我们相互灌溉. ————世界上有那么多的人,但是只有你们是一直陪伴在我身边,我们相互关心. “在 ...