C# Microsoft.Office.Interop.Owc11 导出excel文件
C# Microsoft.Office.Interop.Owc11 导出excel文件
1、新建项SupremeWindowsForms窗体应用项目(项目平台设置称X86)
注意:因为大多数第三方写的COM控件,只支持32位的系统,因此在VS中找到引用控件所在的项目SupremeWindowsForms设置一下,SupremeWindowsForms项目--〉属性--〉生成--〉常规---〉目标平台---〉选择X86即可
否则会出现如下问题:
检索 COM 类工厂中 CLSID 为 {XXXX-XXXX-XXX-XXXXX-XXX} 的组件时失败,原因是出现以下错误: 80040154。
如以上问题出现,多半是因为COM控件未在目标机器上面注册造成的
解决方法:Resvr32 .net中引用控件的名称,如果注册成功,问题不在出现
但是如果是在x64位的系统中,即使控件注册成功,错误依照提示,是因为大多数第三方写的COM控件,只支持32位的系统,
在VS中找到引用控件所在的项目--〉属性--〉生成--〉常规---〉目标平台---〉选择X86即可解决
2、右键添加Microsoft.Office.Interop.Owc11.dll和OWC11.dll组件,下载地址https://download.csdn.net/download/LongtengGensSupreme/12037912
3、添加方法ExportExcel,
代码如下:
按钮事件方法:
private void btnTestchartPrint_Click(object sender, EventArgs e)
{
//测试 Microsoft.Office.Interop.Owc11 SpreadsheetClass
ExportExcel();
}
ExportExcel方法:
public void ExportExcel()
{
int colNum = ;
int rowNum = ;
SpreadsheetClass xlsheet = null;
try
{
xlsheet = new SpreadsheetClass();
}
catch
{
MessageBox.Show("测试Owc11 SpreadsheetClass出错", "测试Microsoft.Office.Interop.Owc11", MessageBoxButtons.OK);
try
{
FileInfo fi = new FileInfo(Path.Combine(Application.StartupPath, "OWC11.dll"));
if (fi.Exists)
{
Process p = new Process();
p.StartInfo = new ProcessStartInfo("regsvr32", "\"" + fi.FullName + "\"");
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
p.WaitForExit();
}
}
catch
{
}
xlsheet = new SpreadsheetClass();
}
//xlsheet.Sheets.Add(null, xlsheet.Sheets.Count, null, null);
//Worksheet ws = xlsheet.Sheets[0] as Worksheet;
//ws.Name = "hello";
//xlsheet.ActiveSheet.Name = "";
//合并单元格
xlsheet.get_Range(xlsheet.Cells[, ], xlsheet.Cells[, colNum]).set_MergeCells(true);
xlsheet.ActiveSheet.Cells[, ] = "测试Microsoft.Office.Interop.Owc11";
//字体加粗
xlsheet.get_Range(xlsheet.Cells[, ], xlsheet.Cells[, colNum]).Font.set_Bold(true);
//单元格文本水平居中对齐
xlsheet.get_Range(xlsheet.Cells[, ], xlsheet.Cells[, colNum]).set_HorizontalAlignment(XlHAlign.xlHAlignCenter);
//设置字体大小
xlsheet.get_Range(xlsheet.Cells[, ], xlsheet.Cells[, colNum]).Font.set_Size();
//设置列宽
xlsheet.get_Range(xlsheet.Cells[, ], xlsheet.Cells[, ]).set_ColumnWidth();
//画边框线
xlsheet.get_Range(xlsheet.Cells[, ], xlsheet.Cells[rowNum, ]).Borders.set_LineStyle(XlLineStyle.xlDashDotDot);
int x = , y = ;
//写入数据 (这里可根据需要由ds生成)
for (int row = ; row < ; row++) //注意写入数据时,必须从第一行开始写excel里没有第"0"行
{
for (int i = ; i < ; i++)
{
xlsheet.ActiveSheet.Cells[row, i + ] = 123.456;
}
}
try
{
//将数字格式化为金额(要格式化的单元格内的值必须为数值型)
xlsheet.get_Range(xlsheet.Cells[, ], xlsheet.Cells[, ]).set_NumberFormat("¥#,##0.00");
xlsheet.Export(Path.Combine(Application.StartupPath, "testowc11.xls"), SheetExportActionEnum.ssExportActionNone, SheetExportFormat.ssExportXMLSpreadsheet);
MessageBox.Show("export ok");
Process.Start(Path.Combine(Application.StartupPath, "testowc11.xls"));
}
catch
{
MessageBox.Show("导出Owc11 SpreadsheetClass出错", "测试Microsoft.Office.Interop.Owc11", MessageBoxButtons.OK);
}
}
4、测试结果如下:

注意事项问题原文链接:https://blog.csdn.net/Fibona/article/details/5949180
C# Microsoft.Office.Interop.Owc11 导出excel文件的更多相关文章
- AxMicrosoft.Office.Interop.Owc11.AxSpreadsheet控件在C#中的引用
这几天要是用AxMicrosoft.Office.Interop.Owc11.AxSpreadsheet控件做查询,发现一系列问题,一点点记录下来吧,以备后查: 第一.相关属性:http://www. ...
- 使用Microsoft.Office.Interop.Excel.dll 文件来生成excel 文件
日常工作中经常需要将后台的数据导出成excel 格式,这里通过调用微软提供的类库来生成excel 文件. 具体是引用 了Microsoft.Office.Interop.Excel.dll 类库文件 ...
- Microsoft.Office.Interop.Excel操作Excel文件时出现的问题及解决方案
问题描述: Microsoft.Office.Interop.Excel.Worksheet 打不开文件 Microsoft Office Excel 不能访问文件"a.xls". ...
- c#操作excel方式三:使用Microsoft.Office.Interop.Excel.dll读取Excel文件
1.引用Microsoft.Office.Interop.Excel.dll 2.引用命名空间.使用别名 using System.Reflection; using Excel = Microsof ...
- C# 使用自带Microsoft.Office.Interop.Excel简单操作Excel文件
项目添加应用 Microsoft.Office.Interop.Excel.dll 文件 引用命名空间: using Excel = Microsoft.Office.Interop.Excel; 简 ...
- NPOI写Excel,Microsoft.Office.Interop.excel.dll 转换Excel为PDF
首先要引用NPOI动态库和Microsoft.Office.Interop.excel.dll (Microsoft.Office.Interop.excel.dll 下载链接 ,下载以后解压文件,把 ...
- Microsoft.Office.Interop.Excel的用法以及利用Microsoft.Office.Interop.Excel将web页面转成PDF
1.常见用法 using Microsoft.Office.Interop.Excel; 1)新建一个Excel ApplicationClass ExcelApp = New A ...
- 引用Microsoft.Office.Interop.Excel出现的问题
引用Microsoft.Office.Interop.Excel出现的问题 转自:http://www.hccar.com/Content,2008,6,11,75.aspx,作者:方继祥 操作背 ...
- 利用Microsoft.Office.Interop.Excel 将web页面转成PDF
网上有很多将Web页面转成PDF的方法,还有许多收费的第三方插件.其实利用Office 自带的将EXCEL发布成PDF的功能就可以实现,如果你的需求没有多复杂,可以采用笔者的方法. 首先将web页面h ...
随机推荐
- 模块化规范:CMD和AMD的区别
https://www.zhihu.com/question/20351507/answer/14859415 AMD(异步模块定义) 是 RequireJS 在推广过程中对模块定义的规范化产出.CM ...
- Fiddler抓包工具简介
1.Fiddler工具: 个人认为Fiddler实际上是一款在浏览器与服务器之间设置代理,对两者之间的通信会话(数据包)进行抓取和处理的一款工具: Fiddler是一个http协议调试代理工具,它能够 ...
- Android:异步处理之Handler+Thread的应用
担心原文消失,做此记录,感谢 https://www.cnblogs.com/net168/p/4075126.html 前言 很久很久以前就听说了,每一个android的应用程序都会分别运行在一个独 ...
- 如何将Android的AOSP仓库放置到自己的gitlab服务器上?
平台 Ubuntu 18.04 GitLab Community Edition 11.11.0 参考 https://source.android.google.cn/ 概述 git ...
- reset.css文件下载及剖析
@charset "utf-8"; /* http://meyerweb.com/eric/tools/css/reset/ v2.0-modified | 20110126 Li ...
- Linux 下 svn 多个项目多用户分配
安装步骤如下: 1.yum install subversion 2.输入rpm -ql subversion查看安装位置,如下图: 输入 svn –help可以查看svn的使用方法 需求 开发服务器 ...
- 细说Unicode(一) Unicode初认识
https://segmentfault.com/a/1190000007992346 细说Unicode(一) Unicode初认识 网站开发中经常会被乱码问题困扰.知道文件编码错误会导致乱码,但对 ...
- MySQL的简单概念及软件安装
数据库的简介 一.数据库的基本概念:数据.数据库.数据库管理系统.数据库系统 数据:数据(Data)是用来记录信息的可识别符号,是信息的具体表现形式. 数据库:(1)数据库(Database,DB)是 ...
- Json提取器(Json Extractor)
Variable names:保存的变量名,后面使用${Variable names}引用 JSON Path expressions:调试通过的json path表达式 Match Numbers ...
- 探索Windows 10的CFG机制
转载https://www.anquanke.com/post/id/85493 0x00 前言 随着操作系统开发人员一直在增强漏洞利用的缓解措施,微软在Windows 10和Windows 8.1 ...