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. canvas添加水印

    <canvas id="canvas"></canvas><canvas id="water"></canvas> ...

  2. Linux中常用的50个命令

    1. [命令]:cat [功能说明]: concatenate files and print on the standard output #连接文件并打印到标准输出,有标准输出的都可以用重定向定向 ...

  3. over(partition by)开窗函数的使用

    开窗函数是分析函数中的一种,开窗函数与聚合函数的区别是:开窗函数是用于计算基于组的某种聚合值且每个的组的聚合计算结果可以有多行,而聚合函数每个组的聚合计算结果只有一个.使用开窗函数可以在没有group ...

  4. Linux基础命令---tail显示文本

    tail 显示文本文件尾部的部分内容,默认显示最后10行. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法         ...

  5. JDBC-day01

    #JDBC Java DataBase Connectivity Java数据库连接 JDBC提供了一套和数据库交互 ###为什么要使用JDBC 因为Java语言在工作中有可能会有需求去访问各种数据库 ...

  6. vue组件通信之任意级组件之间的通信

    <div id="app"> <comp1></comp1> <comp2></comp2> </div> ...

  7. AtCoder Beginner Contest 044 B - 美しい文字列 / Beautiful Strings

    Time limit : 2sec / Memory limit : 256MB Score : 200 points Problem Statement Let w be a string cons ...

  8. 使用github管理Eclipse分布式项目开发

    使用github管理Eclipse分布式项目开发 老关我在前面的博文(github管理iOS分布式项目开发)中介绍了github管理iOS分布式开发,今天老关将向大家介绍使用github管 理Ecli ...

  9. Win10+Ubuntu16.04双系统安装过程中遇到的一些问题及解决办法

    前两天闲来无聊重装了系统,装的是win10 64的系统,后来心血来潮索性再装Ubuntu,搞成win10+Ubuntu双系统. win10系统的镜像文件可以通过MSDN网站获取,MSDN是一个很可靠的 ...

  10. IE haslayout的属性及其值

    haslayout是IE 渲染引擎的一个内部组成部分.在IE 中,一个元素要么自己对自身的内容进行计算大小和组织,要么依赖于父元素来计算尺寸和组织内容.为了调节这两个不同的概念,渲染引擎采用了hasl ...