使用Microsoft.Office.Interop.Excel.dll 文件来生成excel 文件

时间:2022-12-27 22:51:58

日常工作中经常需要将后台的数据导出成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 文件

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

  1. C&num; Microsoft&period;Office&period;Interop&period;Owc11 导出excel文件

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

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

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

  3. c&num;操作excel方式三&colon;使用Microsoft&period;Office&period;Interop&period;Excel&period;dll读取Excel文件

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

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

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

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

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

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

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

  7. C&num; 使用自带Microsoft&period;Office&period;Interop&period;Excel简单操作Excel文件

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

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

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

  9. C&num; Microsoft&period;Office&period;Interop&period;Excel&period;ApplicationClass 加载类型库&sol;DLL 时出错

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

随机推荐

  1. layer——源码学习

    一.根据源码的学习 发现创建弹窗:使用了一些div来组成 zindex 和 index 是自动生成. zindex 表示生成的层次关系 index 用来表示各个层的id 默认class名 h = [& ...

  2. Javascript初学篇章&lowbar;7(DOM)

    DOM 文档对象模型DOM (document object model) 文档对象模型,它定义了操作文档对象的接口.DOM 把一份html文档表示为一棵家谱树,使用parent(父), child( ...

  3. SQL Server查询数据库中所有的表名及行数

    SELECT a.name, b.rows FROM sysobjects AS a INNER JOIN sysindexes AS b ON a.id = b.id WHERE (a.type = ...

  4. hdu 4714 Tree2cycle dp

    用树形dp做的,dp[t][i]表示t及其孩子入度都已经小于等于2并且t这个节点的入度等于i的最优解. 那么转移什么的自己想想就能明白了. 关键在于这个题目会暴栈,所以我用了一次bfs搜索出节点的顺序 ...

  5. centos7下安装docker(12docker网络)

    docker 网络荣覆盖范围可分为:单个host上的容器网络和跨多个host的网络 docker 安装时会自动在host上创建三个网络,我们可以使用docker network ls查看到 1.non ...

  6. HTML 引入Css样式

  7. 【转】Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)

    [转]Python之xml文档及配置文件处理(ElementTree模块.ConfigParser模块) 本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 ...

  8. Nginx配置示例

    server {listen 6080;server_name local.boheadmin; location / {proxy_pass http://127.0.0.1:8087;} loca ...

  9. java输出自身源代码

    如何通过运行程序输出程序源码? 下面是JAVA实现 public class Quine { public static void main(String[] args) { char q = 34; ...

  10. 4&period;keras实现--&gt&semi;生成式深度学习之用GAN生成图像

    生成式对抗网络(GAN,generative adversarial network)由Goodfellow等人于2014年提出,它可以替代VAE来学习图像的潜在空间.它能够迫使生成图像与真实图像在统 ...