首先,需要引用excel的库:

Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

Workbooks books = excel.Workbooks;
Workbook xlbook = books.Add(true);
Worksheet xlsheet = (Worksheet)xlbook.ActiveSheet;

xlsheet.Name = "DataEdit";
var fileName = "test.xlsx";

var rownum = 10; //excel行数
var colnum =10; //excel列数
Array arr = Array.CreateInstance(typeof(string), rownum, colnum);//先将属性数据缓存到Array数组中

int i = 0;
foreach (var proInfo in listProperties)
{
  arr.SetValue(proInfo, 0, i);
  for (var index = 0; index < listProperties.Count; index++)
  {
    string xsheetValue = "";
    if (listProperties[index].ContainsKey(proInfo))
    {
      xsheetValue = listProperties[index][proInfo];
    }
    arr.SetValue(xsheetValue, index + 1, i);
  }
  i++;
}

//一次性写入到excel的sheet中,减少对excel文件的I/O操作次数,提高效率
Range range = xlsheet.Range[xlsheet.Cells[1, 1], xlsheet.Cells[rownum, colnum]];
range.Value2 = arr;

//excel表头添加背景色
Range rangeHeader = xlsheet.Range[xlsheet.Cells[1, 1], xlsheet.Cells[1, colnum]];
rangeHeader.Interior.Color = System.Drawing.Color.GreenYellow;

excel.Columns.AutoFit();
xlbook.Saved = true;
xlbook.SaveCopyAs("D:\\" + fileName);

excel.Quit();

C#高效率导出Excel的更多相关文章

  1. 从DataTable高效率导出数据到Excel

    首先从数据库读取数据到DataTable,这我就不提了,大家都明白.下面直接介绍如何从DataTable高效率导出数据到Excel中的方法,代码如下: using Microsoft.Office.I ...

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

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

  3. 利用poi导出Excel

    import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.r ...

  4. [django]数据导出excel升级强化版(很强大!)

    不多说了,原理采用xlwt导出excel文件,所谓的强化版指的是实现在网页上选择一定条件导出对应的数据 之前我的博文出过这类文章,但只是实现导出数据,这次左思右想,再加上网上的搜索,终于找出方法实现条 ...

  5. NPOI导出Excel

    using System;using System.Collections.Generic;using System.Linq;using System.Text;#region NPOIusing ...

  6. ASP.NET Core 导入导出Excel xlsx 文件

    ASP.NET Core 使用EPPlus.Core导入导出Excel xlsx 文件,EPPlus.Core支持Excel 2007/2010 xlsx文件导入导出,可以运行在Windows, Li ...

  7. asp.net DataTable导出Excel 自定义列名

    1.添加引用NPOI.dll 2.cs文件头部添加 using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System.IO; 3.代码如 ...

  8. Aspose.Cells导出Excel(1)

    利用Aspose.Cells导出excel 注意的问题 1.DataTable的处理 2.进行编码,便于中文名文件下载 3.别忘了Aspose.Cells.dll(可以自己在网上搜索) public ...

  9. 前端导出Excel兼容写法

    今天整理出在Web前端导出Excel的写法,写了一个工具类,对各个浏览器进行了兼容. 首先,导出的数据来源可能有两种: 1. 页面的HTML内容(一般是table) 2. 纯数据 PS:不同的数据源, ...

随机推荐

  1. Chrome 开发者工具使用技巧

    最近我花了较多的时间使用 Chrome 的开发者工具.我发现了很多之前没用过的好功能 (或还不是特别需要用到的功能,例如 blackboxing 和 asynchronous stacktraces) ...

  2. ORA-01489: result of string concatenation is too long

    ORA-01489: result of string concatenation is too long Cause: String concatenation result is more tha ...

  3. android手势事件 快速移动 长按触摸屏 按下触摸屏,并拖动

    /* 用户按下触摸屏.快速移动后松开 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float vel ...

  4. HTML5 <script>元素async,defer异步加载

    原文地址:HTML5′s async Script Attribute原文日期: 2010年09月22日翻译日期: 2013年08月22日 (译者注: 异步加载,可以理解为无阻塞并发处理.) (译者再 ...

  5. poj 1222 EXTENDED LIGHTS OUT(位运算+枚举)

    http://poj.org/problem?id=1222 题意:给一个确定的5*6放入矩阵.每一个格子都有一个开关和一盏灯,0表示灯没亮,1表示灯亮着.让你输出一个5*6的矩阵ans[i][j], ...

  6. iOS开发——UI高级OC篇&自定义控件之调整按钮中子控件(图片和文字)的位置

    自定义控件之调整按钮中子控件(图片和文字)的位置 其实还有一种是在storyBoard中实现的,只需要设置对应空间的左右间距: 这里实现前面两种自定义的方式 一:imageRectForContent ...

  7. 文件写入文件分布式系统(asp.net C#)

    ) { try { System.Drawing.Image img = System.Drawing.Image.FromStream(fileData.InputStream); &&am ...

  8. php 日期

    获取两个日期之间的间隔天列表: $begin = new DateTime( '2012-08-0' );$end = new DateTime( '2012-08-31' );$end = $end ...

  9. 小凡的Linux主机与时间服务器同步记录

    小凡的Linux主机与时间服务器同步记录 导读 我们新安装的Linux主机,如果没有做与互联网服务器时间同步的处理的话,当我们使用date命令的时候,我们就看不到当前的时间,只能看到过去的时间.在我们 ...

  10. iOS企业应用Profile制作流程

    第一步:企业版iDP申请完成以后,访问iOS Dev Center:https://developer.apple.com/devcenter/ios/index.action 第二步:点击Log I ...