日常工作中经常需要将后台的数据导出成excel  格式,这里通过调用微软提供的类库来生成excel 文件。

具体是引用 了Microsoft.Office.Interop.Excel.dll 类库文件来生成excel 文件,这样使用起来比较方便,这是我的实例代码:

 public static void ExportExcel() {

            // excel 文件的数据源
DataSet ds = CommonDB.executeQuery(CommandType.StoredProcedure, "GetAllProducts"); try
{
Application statusExcel = new Microsoft.Office.Interop.Excel.Application();
Workbook statusWorkbook = statusExcel.Application.Workbooks.Add(true);
Worksheet wsStatusSheet = (Worksheet)statusWorkbook.Worksheets.Add(statusWorkbook.Sheets[1], Type.Missing, Type.Missing, Type.Missing);
((Worksheet)statusWorkbook.Sheets["Sheet1"]).Delete();
wsStatusSheet.Name = "Status11"; int excelCurrRow = 1;
wsStatusSheet.Cells[excelCurrRow, 1] = "Name";
wsStatusSheet.Cells[excelCurrRow, 2] = "Category";
wsStatusSheet.Cells[excelCurrRow, 3] = "Price";
wsStatusSheet.Cells[excelCurrRow, 4] = "Description"; Range firstHeaderRow = wsStatusSheet.get_Range("A1:D1", System.Type.Missing);
firstHeaderRow.Font.Bold = true;
firstHeaderRow.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.ColorTranslator.FromHtml("#808080"));
firstHeaderRow.Font.Color = 2;
firstHeaderRow.Font.Name = "Calibri";
firstHeaderRow.EntireRow.RowHeight = 30;
firstHeaderRow.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter; Range statusSheetFirstColumn = wsStatusSheet.get_Range("A:A", System.Type.Missing);
statusSheetFirstColumn.EntireColumn.ColumnWidth = 36.43; Range statusSheetSecondColumn = wsStatusSheet.get_Range("B:B", System.Type.Missing);
statusSheetSecondColumn.EntireColumn.ColumnWidth = 15.71; Range statusSheetThirdColumn = wsStatusSheet.get_Range("C:C", System.Type.Missing);
statusSheetThirdColumn.EntireColumn.ColumnWidth = 15.43; Range statusSheetFourthColumn = wsStatusSheet.get_Range("D:D", System.Type.Missing);
statusSheetFourthColumn.EntireColumn.ColumnWidth = 17.86; excelCurrRow++; for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
for (int j = 1; j <= 4; j++)
{
wsStatusSheet.Cells[excelCurrRow, j] = ds.Tables[0].Rows[i][j - 1].ToString();
}
excelCurrRow++;
} // 指定生成的文件名和路径
string statusFilePath = ConfigurationManager.AppSettings["statusExcelPath"];
string fileName = "Result_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
fileName = statusFilePath + fileName;
if (File.Exists(fileName))
{
File.Delete(fileName);
} object missing = System.Reflection.Missing.Value; // 保存excel 文件
statusWorkbook.SaveAs(fileName, XlFileFormat.xlOpenXMLWorkbook, missing, missing, false, false, XlSaveAsAccessMode.xlNoChange,
XlSaveConflictResolution.xlUserResolution, true, missing, missing, missing); }
catch (Exception ex) { }
}

如下是生成的excel 文件截图:

使用Microsoft.Office.Interop.Excel.dll 文件来生成excel 文件的更多相关文章

  1. C# Microsoft.Office.Interop.Owc11 导出excel文件

    C# Microsoft.Office.Interop.Owc11 导出excel文件 1.新建项SupremeWindowsForms窗体应用项目(项目平台设置称X86) 注意:因为大多数第三方写的 ...

  2. VS编程中找不到Microsoft.Office.Core、Microsoft.Office.Interop.Word和VBIDE

    在使用vs2005. vs2008. vs2010 制作包含 word等office的应用程序时,有时找不到对Microsoft.Office.Core. Microsoft.Office.Inter ...

  3. c#操作excel方式三:使用Microsoft.Office.Interop.Excel.dll读取Excel文件

    1.引用Microsoft.Office.Interop.Excel.dll 2.引用命名空间.使用别名 using System.Reflection; using Excel = Microsof ...

  4. Excel操作 Microsoft.Office.Interop.Excel.dll的使用

    ----转载: http://www.cnblogs.com/lanjun/archive/2012/06/17/2552920.html 先说说题外话,前段时间近一个月,我一直在做单据导入功能,其中 ...

  5. NPOI写Excel,Microsoft.Office.Interop.excel.dll 转换Excel为PDF

    首先要引用NPOI动态库和Microsoft.Office.Interop.excel.dll (Microsoft.Office.Interop.excel.dll 下载链接 ,下载以后解压文件,把 ...

  6. Visual Studio 进行Excel相关开发,Microsoft.Office.Interop.Excel.dll库

    1. Interop.Excel.dll 的查找 本文中将 Microsoft.Office.Interop.Excel.dll库简称为Interop.Excel.dll库 其实在使用Visual S ...

  7. C# 使用自带Microsoft.Office.Interop.Excel简单操作Excel文件

    项目添加应用 Microsoft.Office.Interop.Excel.dll 文件 引用命名空间: using Excel = Microsoft.Office.Interop.Excel; 简 ...

  8. Microsoft.Office.Interop.Excel操作Excel文件时出现的问题及解决方案

    问题描述: Microsoft.Office.Interop.Excel.Worksheet 打不开文件 Microsoft Office Excel 不能访问文件"a.xls". ...

  9. C# Microsoft.Office.Interop.Excel.ApplicationClass 加载类型库/DLL 时出错

    问题  无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Intero ...

随机推荐

  1. javascript 禁用 右键 按键 禁用开发者工具

    var h = window.innerHeight,w=window.innerWidth; //禁用右键 (防止右键查看源代码) window.oncontextmenu=function(){ ...

  2. 大数字加法(hduoj)

    Problem Description I have a very simple problem for you. Given two integers A and B, your job is to ...

  3. Problem: Time(一道水却有意思的题

    Problem Description Digital clock use 4 digits to express time, each digit is described by 3*3 chara ...

  4. Python3.x - 字符串

    Python3 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号( ' 或 " )来创建字符串. var1 = 'hello world' var2 = "he ...

  5. 2018-软工机试-A-西班牙馅饼

    A. 西班牙馅饼 单点时限: 1.0 sec 内存限制: 256 MB 港岛妹妹,你献给我的西班牙馅饼 甜蜜地融化了我,天空之城在哭泣 港岛妹妹,我们曾拥有的甜蜜的爱情 疯狂地撕裂了我,天空之城在哭泣 ...

  6. 微信自定义菜单errcode(40016)

    最近在做微信开发,通过微信API创建自定义菜单时候出现:{"errcode":40016,"errmsg":"invalid button size ...

  7. pagerank算法在数学模型中的运用(有向无环图中节点排序)

    一.模型介绍 pagerank算法主要是根据网页中被链接数用来给网页进行重要性排名. 1.1模型解释 模型核心: a. 如果多个网页指向某个网页A,则网页A的排名较高. b. 如果排名高A的网页指向某 ...

  8. 从码云上下载react项目并配置成可运行状态

    (第一次写,如有不足之处,欢迎指出) 一.下载项目: 1.首先保证安装了git, 2.然后在本地想要存放项目位置打开git(Git Bash Here),再复制码云中如图所示的地址: 3.在git中输 ...

  9. C++中const的用法

    1.const修饰普通变量和指针 (1).const修饰普通变量 其写法有2种:a.const type value;   b.type const value; 这两种写法本质上是一样的.其含义是: ...

  10. 使用JBolt新建Maven版工程步骤

    一.打开新建对话框 在左侧右键new中可以找到JFinal创建工程的菜单 JBoltHome页面也有快捷按钮用来弹出创建工程对话框. 二.填写Maven和其他信息配置 填写工程name 主包名 下面有 ...