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. SQL小结

    1. 一般而言,除非你确实需要表中的每一列,否则最好别使用*通配符.虽然使用通配符让你自己省事,不用明确列出所需列,但检索不需要的列通常会降低检索和应用程序的性能. 2. DISTINCT关键字作用于 ...

  2. Linux基础命令---显示主机名hostname

    hostname hostname指令用于设置或者显示系统主机名,没有任何参数就会返回gethostname()函数的返回值.使用hostname指令之后,主机名会立马被修改,但是重启系统之后就失效了 ...

  3. 深入理解Node.js基于事件驱动的回调

    回调和异步调用的关系 首先明确一点,回调并非是异步调用,回调是一种解决异步函数执行结果的处理方法.在异步调用,如果我们希望将执行的结果返回并且处理时,可以通过回调的方法解决.为了能够更好的区分回调和异 ...

  4. Linux文件管理和编辑常用命令

    Linux文件管理和编辑常用命令 mkdir 命令 功能说明 mkdir 命令用于创建一个目录,mkdir是make directory的缩写 格式: mkdir [选项] 目录名 mkdir 命令的 ...

  5. 常用linux命令行

    1.ls命令 ls -a 列出目录所有文件,包含以.开始的隐藏文件 ls -A 列出除.及..的其它文件 ls -r 反序排列 ls -t 以文件修改时间排序 ls -S 以文件大小排序 ls -h ...

  6. 数据分析之pandas01

    Series 一.Series Series是一种类似于一维数组的对象,有两部分组成:     .values:一组数据(ndarray类型)     .index: 相关的数据索引标签 二.seri ...

  7. QRegExp 正则表达式详解

    引言 正则表达式(regular expression)就是用一个“字符串”来描述一个特征,然后去验证另一个“字符串”是否符合这个特征.比如 表达式“ab+” 描述的特征是“一个 'a' 和 任意个 ...

  8. MyEclipse/Eclipse快捷键总结

    MyEclipse/Eclipse快捷键 查找某个方法被谁调用:选中方法名,ctrl+shift+g 通过文件名称查找类或文件:ctrl+shift+r(Open Resource)

  9. i2c调试碰到的问题

    i2c eeprom i2cget两次结果不一致 i2cset没成功. device里只看到50,却冒出了51地址. i2ctools是针对8bit地址的,而我们的eeprom都是用16bit add ...

  10. Django基础(10): URL重定向的HttpResponseDirect, redirect和reverse的用法详解

    利用django开发web应用, 我们经常需要进行URL重定向,有时候还需要给URL传递额外的参数.比如用户添加文章完成后需要转到文章列表或某篇文章详情.因此熟练掌握HttpResponseDirec ...