Excel导出

Excel导出的意义

因为在项目中有些报表数据客户需要导出功能,那么导出为Excel就十分有必要了,可是有些客户的机器上并没有安装Excel或者安装的版本参差不一。
那么我们在这样的情况下应该应该怎么做呢?最简单的方法就是引用Excel相关的Com组件就行了,可是这种情况只能在安装了Excel的用户适用,
对于没有安装Excel的用户不能强制用户安装吧。那么我们只能将眼光瞄向第三方的类库了,能用的Excel操作导出类库大致有这么三个
  1. NPOI
  2. ExcelRepor
  3. aspose.cells

    好的,废话不多说,接下来就将这几种导出方法一一道来:首先我们模拟一个数据源

         private DataTable dt = new DataTable();

         /// <summary>
/// 装载数据
/// </summary>
private void Inidata()
{
dt.TableName = "student";
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Team", typeof(string));
DataRow dr = dt.NewRow();
DataRow dr1 = dt.NewRow();
dr["Name"] = "科比";
dr["Team"] = "湖人";
dt.Rows.Add(dr);
dr1["Name"] = "詹姆斯";
dr1["Team"] = "骑士";
dt.Rows.Add(dr1);
list.Add(new Student { Name = "科比", Team = "湖人" });
list.Add(new Student { Name = "詹姆斯", Team = "骑士" });
}

使用NPOI导出

NPOI 是 POI 项目的 .NET 版本。具体的信息请自行百度。下边是我们公司用的一个NPOI到处的Helper中的片段
  public class NPOIExportExclHelper
{
/// <summary>
/// 创建一个Excel
/// Yakecan
/// </summary>
/// <returns>返回一个空表格</returns>
public HSSFWorkbook InitializeWorkBook()
{
HSSFWorkbook workBook = new HSSFWorkbook();
DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
SummaryInformation si = PropertySetFactory.CreateSummaryInformation(); dsi.Company = "河南xx软件科技有限公司";
dsi.Manager = "Office Word 2003/2007"; si.Author = "www.henanluheng.com";
si.Subject = "信息导出";
si.Title = "系统报表"; workBook.DocumentSummaryInformation = dsi;
workBook.SummaryInformation = si; return workBook;
} /// <summary>
/// 把指定的DataTable导出Excel
/// Yakecan
/// </summary>
/// <param name="dt">数据源</param>
/// <param name="path">导出的路径(包含文件的名称及后缀名)</param>
/// <param name="tittle">Sheet的名称</param>
public void Export(DataTable dt, string path, string tittle)
{
HSSFWorkbook workbook = InitializeWorkBook();
ISheet sheet1 = workbook.CreateSheet(tittle); IRow titleRow = sheet1.CreateRow();
titleRow.Height = (short) * ; ICellStyle titleStyle = workbook.CreateCellStyle();
titleStyle.Alignment = HorizontalAlignment.Center;
titleStyle.VerticalAlignment = VerticalAlignment.Center;
IFont font = workbook.CreateFont();
font.FontName = "宋体";
font.FontHeightInPoints = (short);
titleStyle.SetFont(font); NPOI.SS.Util.CellRangeAddress region = new NPOI.SS.Util.CellRangeAddress(, , , dt.Columns.Count);
sheet1.AddMergedRegion(region); // 添加合并区域 ICell titleCell = titleRow.CreateCell();
titleCell.CellStyle = titleStyle;
titleCell.SetCellValue(tittle); IRow headerRow = sheet1.CreateRow();
ICellStyle headerStyle = workbook.CreateCellStyle();
headerStyle.Alignment = HorizontalAlignment.Center;
headerStyle.VerticalAlignment = VerticalAlignment.Center;
headerStyle.BorderBottom = BorderStyle.Thin;
headerStyle.BorderLeft = BorderStyle.Thin;
headerStyle.BorderRight = BorderStyle.Thin;
headerStyle.BorderTop = BorderStyle.Thin;
IFont titleFont = workbook.CreateFont();
titleFont.FontHeightInPoints = (short);
titleFont.FontName = "宋体";
headerStyle.SetFont(titleFont); headerRow.CreateCell().SetCellValue("序号");
headerRow.GetCell().CellStyle = headerStyle; for (int i = ; i < dt.Columns.Count; i++)
{
headerRow.CreateCell(i + ).SetCellValue(dt.Columns[i].ColumnName);
headerRow.GetCell(i + ).CellStyle = headerStyle;
sheet1.SetColumnWidth(i, * );
} ICellStyle bodyStyle = workbook.CreateCellStyle();
bodyStyle.BorderBottom = BorderStyle.Thin;
bodyStyle.BorderLeft = BorderStyle.Thin;
bodyStyle.BorderRight = BorderStyle.Thin;
bodyStyle.BorderTop = BorderStyle.Thin;
for (int r = ; r < dt.Rows.Count; r++)
{
IRow bodyRow = sheet1.CreateRow(r + );
bodyRow.CreateCell().SetCellValue(r + );
bodyRow.GetCell().CellStyle = bodyStyle;
bodyRow.GetCell().CellStyle.Alignment = HorizontalAlignment.Center; for (int c = ; c < dt.Columns.Count; c++)
{
bodyRow.CreateCell(c + ).SetCellValue(dt.Rows[r][c].ToString());
bodyRow.GetCell(c + ).CellStyle = bodyStyle;
}
} sheet1.CreateFreezePane(, ); FileStream fs = new FileStream(path, FileMode.Create);
workbook.Write(fs);
fs.Flush();
fs.Position = ;
sheet1 = null;
headerRow = null;
workbook = null;
//OutPutExcelStreamOnClient(ms, xlsName);
fs.Dispose();
} }

好了,接下来让我们来使用一下这个导出的功能,导出代码如下,这些没什么技术含量,F5启动程序查看导出的效果

  SaveFileDialog savedialog = ShowExportExcelDoalog();
savedialog.FileName = "这是我的测试数据";
if (savedialog.ShowDialog() == DialogResult.OK)
{
NPOIExportExclHelper excelHelper = new NPOIExportExclHelper();
excelHelper.Export(dt, savedialog.FileName, "这是我的测试数据");
}

OK,这是我们导出的效果图,自动为我们加上了序号这一列,不过整体来说导出的功能还是没有任何问题。不过还是有一点瑕疵,数据的列名还是英文,解决方法也很简单只要我们设置数据源的dt的列头名设置为中国文就行了,dt.Columns["Name"].ColumnName = "姓名"; 设置完成再导出就没问题了。对于我们直男程序员来说这样大致差不多也就完成了任务,可是这样的效果对于真正的生产环境来说还是不行的,因为有些客户还要求导出的Excel要有格式,比如标题高亮啊、数据行每行颜色的高亮。这些怎么办呢,这就用到接下来的方法了。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAm0AAACtCAIAAABtHt90AAAbk0lEQVR4nO2d74/dVHrH79/Civd5ZW1uk7CUpIGW0LKQmWGZVQh1b9UVu2hh0umLrjqqtiyb7TaOGKnVokpEIH6FXJjQOChNBBNIMpswSTZACLmBYZgACeFHkXZfRCJyX9y599rH5xwf+9r3OcfP96NH7OSOr/21x/t8fGxf38ZTJ3/Rrcdf+skXX33LqqIo2n/wSBRF5ElQqJEVdnuGhT96pdVg7tG58DD2LRSrwm7PsPBHr7QS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4lAp4lArOfcQGkJ8Wzvnzff+o132MUeKZ9fCoXcCjVHDuIzaA/LRwzp/ve9Pw/aP2A49SwbmP2ADy08I5f67vTct+HCA8Sg48SgXnPmIDyE8L5/x5nlM/Pru394jdqTY8ainwKBWc+4gNID8tnPPn+r6X3u1F7WkP10dtBR6lgnMfsQHkp4Vz/nzj0d53pS0G4xlDUniUCniUCs59xAaQnxbO+YtdH4VH7QUepYJzH7EB5KeFc/58n3uJndfNuOcIHqUCHqWCcx+xAeSnhXP+gs9hwH1G1gKPUsG5j9gA8tPCOT+eC1g34FEqOPcRG0B+Wjjnh0frBjxKBec+YgPITwvn/PBo3YBHqeDcR2wA+WnhnB/f4123wvd4o1Ao1CgL49G6gfEoFRcZH4/bAPLTwjk/PFo34FEqOPcRG0B+Wjjnh0frBjxKBec+YgPITwvn/PmerxtH/1Xe8CgV8CgVnPuIDSA/LZzzFxqP4nlGFgOPUsG5j9gA8tPCOf//AwV1jwKqJA9xAAAAAElFTkSuQmCC" alt="" />


使用ExcelReport和模板导出

使用模板导出Excel可以实现Excel的数据和样式的分离,虽然NPOI也可以导出的时候设置样式但是代码比较繁琐。

ExcelReport是博客园的一个大神韩兆新捣鼓出来的一个小工具。在QQ群中作者对于使用者遇到的问题和意见还是反馈的很积极的,这点要给作者点个赞的。关于这个控件的具体详细方法还请大家参考作者的一系列博客,下面只是给你导出的最基本的代码。

首先是我们的模板格式

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdMAAACICAIAAAAzopZ6AAAOwElEQVR4nO2d7VMV1x3H73/Q/0HH1+XVTmrRxJoZfRGTvoiapGZ8SML0QeK00+krMnY6xBjSTGtf2E6YztjxKaaCgkWQwqigQgSEoCICeouggggCk7xgRub0xcKy7J6z9+ze3XP2/Pb7nc/cuew99+7Db8+Hcw93ubljNz/xcPjsr2dm5+XoPlpeUVZeUVZ+sn12fmZ2/n7dkbKlJRUH6/Izs/Pt1Ut3XE9ZajwzO99efaRmeHl55aWa6oqjHd1H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ImxdRExjKrMRgXjXfSaH938IDAIAW0jXmRTRmiOhIxNxQrQj2i6VttgHRGKr9wdxQrQj2i8G8iBOq/cHcUK0I9ovBvIgTqv3B3FCtCPaLwbyIE6r9wdxQrQj2i8Vr3uL/Yw6iMVT7g7mhWhHsF4vRvEVew4ZoD9X+YG6oVgT7xWBexAnV/mBuqFYE+8XiMq/tXJjX6FDtD+aGakWwXyyu/5iDeV4CodofzA3VimC/WLz/JRLmNTpU+4O5oVoR7BeLxbz+wLwmhmp/MDdUK4L9YvhUGeKEan8wN1Qrgv1iMC/ihGp/MDdUK4L9YjF+tiHs11LAvGkL1f5gbqhWBPvFcPUw4oRqfzA3VCuC/WIwL+KEan8wN1Qrgv1i+E4KAABQD8a8yFKGiI5EzA3VimC/GGYbECdU+4O5oVoR7BeDeREnVPuDuaFaEewXY+z/hxgoQ9OylLIAAAAASUVORK5CYII=" alt="" />

导出代码如下

 SaveFileDialog saveFileDlg = new SaveFileDialog();
saveFileDlg.Filter = "Excel 2003文件|*.xls|Excel 2007文件|*.xlsx"; if (DialogResult.OK.Equals(saveFileDlg.ShowDialog()))
{
//实例化一个参数容器,并加载模板填充规则文件 ParameterCollection collection = new ParameterCollection(); collection.Load(@"Templete\excelreport.xml");
//实例化一个元素格式化器列表 List<ElementFormatter> formatters = new List<ElementFormatter>(); formatters.Add(new CellFormatter(collection["Sheet1", "time"], "第一次")); //添加一个单元格格式化器
//添加一个Table格式化器
formatters.Add(new TableFormatter<Student>(collection["Sheet1", "Name"].X, list,
new TableColumnInfo<Student>(collection["Sheet1", "Name"].Y, t => t.Name),
new TableColumnInfo<Student>(collection["Sheet1", "Team"].Y, t => t.Team))
); //导出文件到本地
ExportHelper.ExportToLocal(@"Templete\excelreport.xls", saveFileDlg.FileName,
new SheetFormatterContainer("Sheet1", formatters));

使用aspose.cells和模板导出

Aspose是一个非常强大的“收费控件”,我们要用到的是aspose.cells,这个控件的使用不需要引用其他别的控件,其次代码也非常简单。具体的使用方法参考Aspose.Cells 根据Excel模板导出数据统计

模板如下

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZ8AAACICAIAAAC3JHFUAAAO3klEQVR4nO2d228Vxx3H9z/o/wDiuedplFKDQokEDyHpQ4AkTcQlSdULCWpV9amoVZUQctKol5dWQZWoQBBSMBgKRq5R4AQwwVwcA4ZwyamxAzbmEqrkAQk0fXBZr3cuO7tndmbnt9+vPrLO2Z3dmT0z82HOlWjLqQ1bTm3YtOtnd+//t1Zwzvf1Huace28JAM6o1bCPYrvdvvugVsTd7L0lADijVsO+1nbrOdhfk24GYJpaDfsZu01M3a8Vjx8/7jnY//jxY+8tAcAZtRr2M3b7avJereCc7z3Yzzn33hIAnFGrYT9jt/f/9FcAACDDjN1OfHYGAADIMGO3C5evAQAAGWbs1h67BQAAZJix24dbtoMasnnLjs1bdnhvBgDWmWU3jtQjDx8+PHLs5NaPug8d/vTs8OWzw5fbYxMEwBhGkoHdapcb4ze37ez+9yfHr4/e9O4j2A0pL7BbvXJj/ObWj7qHLnzh3UQFGBg819t3GHZDDAO71SgPHz7c/s+9wyPXvHuqACtffKmrq6trwYK1r7/hy27Rk5RaSxmJEvHdFneB3WqUI8dOHm4NePdUAX73+7cZY4Nnzy9fsXLdW+s92q3U88e1lFcR7BZeIiGqvb5a6D137t7bsXt/8rW2oQtf9B89sbvn4O6eg/1HT1T56eqiRYuWPfd8ZrFcY7jAeHA2fvzaTZxNgU4cInbjQrfFd1Xb65ZTZ4aSC7fhkau9fZ907+uN6e37ZHjkqneRpXj5R6+wJ5k/f74tu6mGh+FRJZXv/MDMww3tproRVmC3umTvgb7kJz9ODJ471H8kxYnBc951JvKbDb9ljG1qfmBx7Qa7OWiG99C0m6pXQu8tab4vi1hsx66ekav/iUUwMHjm+MnBFAODZ7y7TGTlyhcZYyNffNkemzh5eujAof722MRnZz7vO3y0Q7tpXsFQvcQhPZVYWHoSaRn9IdLzx7cN6828BFXMr7dYY8oOKbvpO9XL4+smmWrjnG/b2X35+o1YB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a9IkTTHpHIgLWLQbesQw5u1PboHdgrRbsdfdVHMpkmVWc7XPC6S79FsMVwRie9zYjQtzI7nF0G6qx9PEbugRk/ZrHmHznuKwm+34eVchNSLNu1NfMnPmiFtM5pJJXc7spr+tmkuqKlJ2k57BsJ2Ge8PtEVVhk/bAbnW0GzcYtbOaqx1bmr3SWlT/8pusCAznkslkll54pt2k2zPtltol2k3feE07VVVorktavrI9Yn64eAN2C8Zu3ManeaMn4VnjaVZztc8LVIdIh69YdeZ2aUnxrqpe6SVIi4nnlx4ivSGeU/UQRbJ3FdAjmS03v+S8PcU9WcBBQrJbMfA900o1PsL3TKvU+OmWxL9J5bs5lgO7IU4T4ffdKhMOu5UQ17/N6/3nOkHMvLlzQEUQu8P78CCA6/9Xwb2/EVUirN0qE461WwmB3WodL2MOkQZ2sx7YrdaB3aoWqj0CuyGuQ3UuhRuqPQK7Ia5DdS6FG6o9EqTdGGOwW7ihOpfCDdUeCc9u099VgN3CDdW5FG6o9gjshrgO1bkUbqj2SGB2m/Ya7BZ0qM6lcEO1R0KyWyw12C3oUJ1L4YZqjwRjt6TRYLegQ3UuhRuqPRKS3cTAbiGG6lwKN1R7JBi7qdZxsFtwoTqXwg3VHoHdENehOpfCDdUeCcxueZ+Wwm4VDNW5FG6o9khgdisA7Fa1UJ1L4YZqj8BuiOtQnUvhhmqPeLZbz4E+B3j/uU4AQE3A2q3WuUJ0pRBuqPaIl+uC3WodqnMp3FDtEdgNcR2qcyncUO0R2A1xHapzKdxQ7REv1/U/+xdEw00RqywAAAAASUVORK5CYII=" alt="" />

 WorkbookDesigner designer = new WorkbookDesigner();
//Server.MapPath("./")
string path = System.AppDomain.CurrentDomain.BaseDirectory + "/Templete/aspose.xls";
designer.Open(path);
designer.SetDataSource(dt);
designer.Process();
//Save the excel file
string fileToSave = System.AppDomain.CurrentDomain.BaseDirectory + "Templete/JH_ManageExcel.xls";
if (File.Exists(fileToSave))
{
File.Delete(fileToSave);
} designer.Save(fileToSave, FileFormatType.Excel2003);
//打开Excel文件
Process.Start(fileToSave);

总结


本文只是简单的介绍几种Excel的导出方法,并没有具体的深入研究。这三种方式各有各的特点,第一种方法比较通用,适合那些需要多种导出且对导出的样式没有要求的项目,后边两种适合那些对导出数据有样式需求的项目。第二种方式所用的类库开源、免费但是代码比较繁琐,且还需要生成Xml的配置文件使起来还是比较麻烦,第三种使用起来最好、最简便,不过需要收费。如果用在商业项目中还是需要斟酌的。

C#导出Excel那些事的更多相关文章

  1. Java利用POI导入导出Excel中的数据

         首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据.下面POI包的下载地 ...

  2. shopnc 导出Excel数据问题实例 && ajax 获取当前值并传递

    任务:从商家中心导出数据,各个商品所属情况. 商品导出到Excel文件功能 /导出exel 功能make-in-lemon public function createExcelOp(){ $mode ...

  3. C#中缓存的使用 ajax请求基于restFul的WebApi(post、get、delete、put) 让 .NET 更方便的导入导出 Excel .net core api +swagger(一个简单的入门demo 使用codefirst+mysql) C# 位运算详解 c# 交错数组 c# 数组协变 C# 添加Excel表单控件(Form Controls) C#串口通信程序

    C#中缓存的使用   缓存的概念及优缺点在这里就不多做介绍,主要介绍一下使用的方法. 1.在ASP.NET中页面缓存的使用方法简单,只需要在aspx页的顶部加上一句声明即可:  <%@ Outp ...

  4. html table表格导出excel的方法 html5 table导出Excel HTML用JS导出Excel的五种方法 html中table导出Excel 前端开发 将table内容导出到excel HTML table导出到Excel中的解决办法 js实现table导出Excel,保留table样式

    先上代码   <script type="text/javascript" language="javascript">   var idTmr; ...

  5. Java无模板导出Excel,Apache-POI插件实现

    开发环境 jdk 1.8 Maven 3.6 Tomcat 8.5 SpringBoot 2.1.4.RELEASE Apache-POI 3.6 Idea 注意: 我是在现有的基于SpringBoo ...

  6. POI通用导出Excel数据(包括样式设计)

    前言 前一段时间我写过通用的导入Excel,前几天也写了导出pdf格式的,还有我之前搞得导出Word,我在之前的博客也都介绍了导出和导入是一个道理,无非是一个获取一个是赋值.昨天有一位同仁看了我的Ex ...

  7. react学习之js-xlsx导入和导出excel表格

    前记:最近真的挺忙的,一件事接着一件,都忘了我的React项目,尽管这是一个没写概率没写离散的夜晚,我决定还是先做做我的React 好了,进入正题 项目需求,需要导入和导出表单,发现前端已经强大到无所 ...

  8. JAVA POI XSSFWorkbook导出扩展名为xlsx的Excel,附带weblogic 项目导出Excel文件错误的解决方案

    现在很多系统都有导出excel的功能,总结一下自己之前写的,希望能帮到其他人,这里我用的是XSSFWorkbook,我们项目在winsang 用的Tomcat,LInux上用的weblogic服务器, ...

  9. C#使用Aspose.Cells导出Excel简单实现

    首先,需要添加引用Aspose.Cells.dll,官网下载地址:http://downloads.aspose.com/cells/net 将DataTable导出Xlsx格式的文件下载(网页输出) ...

随机推荐

  1. Android Studio2.x版本无法自动关联源码的解决方法

    Android Studio2.x版本无法自动关联源码的解决方法 在学习android开发过程中,对于一个不熟悉的类,阅读源码是一个很好的学习方式,使用andorid studio开发工具的SDK M ...

  2. CGFloat Float 互转

    直接上代码吧 var positionX:CGFloat = 10 var positionY:CGFloat = 20 var tmpX:Float = 30 var tmpY:Float = 40 ...

  3. redis配置文件redis.conf中文版(基于2.4)

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/99.html?1455869981 代码如下: # Redis示例配置文件 ...

  4. javascript_basic_03之函数、循环、数组

    1.函数:函数(Function),方法(Method),过程(Procedure): 2.默认为假的情况: ①if(0){}:②if(0.0){}:③if(null){}:④if("&qu ...

  5. png图片制作任意颜色的小图标

    本内容只要是对张鑫旭PNG格式小图标的CSS任意颜色赋色技术的这篇文章进行详细说明. HTML: <i class="icon"><i class="i ...

  6. 一道 google曾出过的笔试题:编程实现对数学一元多项式的相加和相乘操作(1)

    数学中一元n次多项式可表示成如下的形式:  Pn(x)=p0+p1x+p2x^2+…+pnx^n     (最多有 n+1 项,n +1 个系数唯一确定她)      (1)请设计一套接口用以表示和操 ...

  7. Ruby FFI 入门教程

    FFI是一个可以让用户使用Ruby调用C代码的gem.如果你需要执行一些系统底层调用,或者做一些高性能运算的话,FFI是一个很不错的选择. 1. 安装 执行gem install ffi即可.非常标准 ...

  8. Pillow实现图片对比

    在编写Web自动化测试用例的时候,如何写断言使新手不解,严格意义上来讲,没有断言的自动化脚本不能叫测试用例.就像功能测试一样,当测试人员做了一些操作之后必然会判断实际结果是否等于预期结果,只不过,这个 ...

  9. Nutch源码阅读进程2---Generate

    继之前仓促走完nutch的第一个流程Inject后,再次起航,Debug模式走起,进入第二个预热阶段Generate~~~   上期回顾:Inject主要是将爬取列表中的url转换为指定格式<T ...

  10. Bounce.js – 快速创建漂亮的 CSS3 动画效果

    Bounce.js 是一个用于制作漂亮的 CSS3 关键帧动画的 JavaScript 库,使用其特有的方式生成的动画效果.只需添加一个组件,选择预设,然后你就可以得到一个短网址或者导出为 CSS 代 ...