EPPlus:网站

Supported Functions

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的使用的更多相关文章

  1. 利用 NUget包 EPPlus 实现数据导出到Excel(适用于MVC)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAvoAAABpCAIAAADEEBBGAAAJdElEQVR4nO3cy2ob5wLA8TxKnqTrrr

  2. 离线安装nuget包EPPlus

    1先去https://www.nuget.org/packages/EPPlus/4.1.0下载,epplus.4.1.0.nupkg 2找到本地文件位置:H:\DOWNLOAD\ 3在vs的程序包管 ...

  3. 利用 NUget包 EPPlus 实现数据导出到Excel(适用于DTcms)

    首先安装EPPlus 包

  4. C# NPOI导出Excel和EPPlus导出Excel比较

    系统中经常会使用导出Excel的功能. 之前使用的是NPOI,但是导出数据行数多就报内存溢出. 最近看到EPPlus可以用来导出Excel,就自己测了下两者导出上的差异. NPIO官网地址:http: ...

  5. [转]比NPOI更討喜的Excel元件-EPPlus

    本文转自:http://blog.darkthread.net/post-2012-05-12-epplus.aspx 前陣子發表 [潛盾機]將檔案結構匯成Excel文件,從網友佑翔的留言(特此感謝) ...

  6. 【netcore基础】.NET Core使用EPPlus实现MVC API里的Excel导出功能 配置中文表头

    EPPlus 用来操作excel非常方便,不用依赖微软的office包,所以推荐使用. 下面是具体步骤和代码 首先用nuget安装 EPPlus.Core 我装的版本是 1.5.4 然后就可以用 Ex ...

  7. EPPlus实战篇——Excel写入

    .net core 项目 可以向excel写入任何类型(T)的数据,只要T中的field的[Display(Name = "1233", Description = "# ...

  8. EPPlus实战篇——Excel读取

    .net core 项目 可以从excel读取任何类型(T)的数据,只要T中的field的[Display(Name = "1233")]中的name==excel column ...

  9. C# NPOI导出Excel和EPPlus导出Excel

    转自:http://www.cnblogs.com/tanpeng/p/6155749.html 系统中经常会使用导出Excel的功能.之前使用的是NPOI,但是导出数据行数多就报内存溢出. 最近看到 ...

随机推荐

  1. 即时通信系统中实现聊天消息加密,让通信更安全【低调赠送:C#开源即时通讯系统(支持广域网)——GGTalk4.5 最新源码】

    在即时通讯系统(IM)中,加密重要的通信消息,是一个常见的需求.尤其在一些政府部门的即时通信软件中(如税务系统),对即时聊天消息进行加密是非常重要的一个功能,因为谈话中可能会涉及到机密的数据.我在最新 ...

  2. Linux基础命令---pgrep

    pgrep pgrep指令可以按名字或者其他属性搜索指定的进程,显示出进程的id到标准输出. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedo ...

  3. UVA 11100 The Trip, 2007 (贪心)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  4. JustOj 1032: 习题6.7 完数

    题目描述 一个数如果恰好等于它的因子之和,这个数就称为"完数". 例如,6的因子为1.2.3,而6=1+2+3,因此6是"完数". 编程序找出N之内的所有完数, ...

  5. js动态添加和删除table的行例子

    <table id="table_report" class="table table-striped table-bordered table-hover&quo ...

  6. Python笔记 #20# SOM

    SOM(自组织映射神经网络)是一种可以根据输入对象的特征自动进行分类(聚类)的神经网络.向该网络输入任意维度的向量都会得到一个二维图像, 不同特征的输入会被映射到二维图像的不同地方(所以SOM也可以用 ...

  7. 使用sqlalchemy用orm方式写pipeline将scrapy item快速存入 MySQL

    传统的使用scrapy爬下来的数据存入mysql,用的是在pipeline里用pymysql存入数据库, 这种方法需要写sql语句,如果item字段数量非常多的 情况下,编写起来会造成很大的麻烦. 我 ...

  8. 腾讯云 网站开启HTTPS

    下图是我站点的初始化样子,可以看到只是输出一个字符串,啥也没有,并且没有https. 这无所谓,因为我们的重点是https,而不是网站内容 接下来就是配置https的关键步骤了,其实只需要三步而已: ...

  9. linux nfs服务配置挂载以及oracle使用nfs存储挂载注意事项

    服务端共享目录 /home/XXX/nfs_shared 172.16.22.0/24(rw,no_root_squash) service nfs restart 常用命令: 查看所有nfs共享目录 ...

  10. linux设置代理

    在~/.bashrc或者/etc/profile下,添加下面 http_proxy=http://192.168.105.171:80 https_proxy=$http_proxy export h ...