
2019-04-23转载文章 21
C# winform导出excel可以使用 Microsoft.Office.Interop.Excel.dll或者Aspose.Cells.dll以及其他方法。Microsoft.Office.Interop.Excel.dll导出速度慢,不适用于数据量大情况。Aspose.Cells.dll到处速度很快。由于Aspose.Cells.dll本身收费,所以需要加载破解证书。
oleaut32.dll ..... windows 要用它执行ole (对象链接和嵌入) 操作. ole 允许将程序创建的对象嵌入到另一个程序的文档或对象中. 例如. 将一个 excel 表格嵌入到 word 文档中. windows 应用程序要经常用到ole, 因此一般你是无法将其删除的。在rtf文档中可以嵌入图像等文件,rtf是word为了与其他字处理软件兼容而能够保存的文档格式,类似 doc格式(word文档)的文件,有很好的兼容性。随着建站技术的不断升级,技术能实现的网站功能也越来越多,可以说只要市场有所需求,那么都会被挖掘并形成一个新的功能,但是当一个企业选择建网站时,并不能说看到哪些功能当下应用最广泛,用户需求更大,就硬是给网站加上,到头来会形成这样一种情况:功能复杂,操作繁琐却不利于用户体验以及网站优化。
C#中winform使用spose.Cells导出excel的方法:
1.下载aspose.Cells.dll以及破解证书:下载地址
2.引用右键添加引用,点击浏览,找到下载的dll文件(最好复制到工程目录),选择Aspose.Cells引用


2、然后打开购买下载得到的扩展面板,找到:“deliciousretouch加强中文版”文件夹,然后复制这个文件夹,粘贴到刚才打开的ps面板目录:d:\program files\adobe\adobe photoshop cc 2015.5\required\cep\extensions(ps安装在哪个盘就打开相应的盘符,我这里是装在d盘,所以打开的是d盘,如果你的在c盘,那就是c盘)。破解过程,打开安装方法图按照要求在“js0group.dll”文件上鼠标右键打开快捷菜单选择“复制(c)”,在桌面上找到“catiav5r20”图标并右键快捷菜单选择“属性(r)”。就下来设置用户目录和临时文件夹hkey_current_user\software\microsoft\windows\currentversion\explorer\shell folders\ *这个自由发挥,建议把cache 和 cookies设置到rramdisk中<3>安装 wimtool 将program files做成wim镜像放到d:盘复制wimtool.exe ,wimgapi.dll,fltlib.dll,pecmd.exe(wincmd)到c:\windows\system32为防止某些锤子软件自动安装到 c:\ program fileshkey_local_machine\software\microsoft\windows\currentversion\commonfilesdir = d:\program files\common filesprogramfilesdir = d:\program files (其实这样设置几乎不起作用,汗`)设置hkey_local_machine\software\microsoft\windows\currentversion\runwimtool=wimtool.exe /mountrw d:\*.wim 1“c:\program files”(注意引号)。


4.
添加using

using Aspose.Cells;
新建DataTable
DataTable dt1 = new DataTable();
初始化表头:

dt1.Columns.Add(new DataColumn("表头1", typeof(string)));
dt1.Columns.Add(new DataColumn("表头2", typeof(string)));
dt1.Columns.Add(new DataColumn("表头3", typeof(string)));
dt1.Columns.Add(new DataColumn("表头4", typeof(string)));
添加数据(可以放到循环体)
DataRow rowData = dt1.NewRow(); rowData["表头1"] = "1" rowData["表头2"] = "2"; rowData["表头3"] = "3"; rowData["表头4"] = "4"; dt1.Rows.Add(rowData);//新增一行数据
将DataTabel写入excel

ExportExcelWithAspose(dt1, "D:\\设备数据.xlsx");
函数实现:
public static bool ExportExcelWithAspose(System.Data.DataTable data, string filepath)
{
try
{
if (data == null)
{
MessageBox.Show("数据为空");
return false;
}
Aspose.Cells.License li = new Aspose.Cells.License();
li.SetLicense("ASPOSE/License.lic");//破解证书
Workbook book = new Workbook(); //创建工作簿
Worksheet sheet = book.Worksheets[0]; //创建工作表
Cells cells = sheet.Cells; //单元格
//创建样式
Aspose.Cells.Style style = book.Styles[book.Styles.Add()];
style.Borders[Aspose.Cells.BorderType.LeftBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 左边界线
style.Borders[Aspose.Cells.BorderType.RightBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 右边界线
style.Borders[Aspose.Cells.BorderType.TopBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 上边界线
style.Borders[Aspose.Cells.BorderType.BottomBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 下边界线
style.HorizontalAlignment = TextAlignmentType.Center; //单元格内容的水平对齐方式文字居中
style.Font.Name = "宋体"; //字体
//style1.Font.IsBold = true; //设置粗体
style.Font.Size = 11; //设置字体大小
//style.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0); //背景色
//style.Pattern = Aspose.Cells.BackgroundType.Solid;
int Colnum = data.Columns.Count;//表格列数
int Rownum = data.Rows.Count;//表格行数
//生成行 列名行
for (int i = 0; i < Colnum; i++)
{
cells[0, i].PutValue(data.Columns[i].ColumnName); //添加表头
cells[0, i].SetStyle(style); //添加样式
}
//生成数据行
for (int i = 0; i < Rownum; i++)
{
for (int k = 0; k < Colnum; k++)
{
cells[1 + i, k].PutValue(data.Rows[i][k].ToString()); //添加数据
cells[1 + i, k].SetStyle(style); //添加样式
}
}
sheet.AutoFitColumns(); //自适应宽
book.Save(filepath); //保存
MessageBox.Show("Excel成功保存到D盘!!!");
GC.Collect();
}
catch (Exception e)
{
return false;
}
return true;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
原文地址:https://www.jb51.net/article/152729.htm
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-97031-1.html
这样的事件最好不好在发生了