C#+Aspose.Cells 导出Excel及设置样式 (Webform/Winform)
在项目中用到,特此记录下来,Aspose.Cells 不依赖机器装没有装EXCEL都可以导出,很方便。具体可以参考其他
http://www.aspose.com/docs/display/cellsnet/Importing+Data+to+Worksheets#ImportingDatatoWorksheets-array
Webfrom版本:
效果如下:
- protected void btnAsnExport_ServerClick(object sender, EventArgs e)
- {
- var getAsnData = SearchDataClass.GetAsnSearchData(txtAsnNo.Value,
- hfCustomerID.Value, txtTimeSelect.Value,txtSku.Value,txtSkuContent.Value);
- //设置导出excel列的标题
- ArrayList ColTitle = new ArrayList()
- { "ASN编号", "SKU", "产品描述", "预期数量", "收货数量",
- "单位","收货库位","收货时间","所属客户","ASN状态","ASN创建时间" };
- //string[] strTitle = new string[] { "ASNNo", "SKU", "SKUDescrC", "ExpectedQty", "ReceivedQty", "UOM",
- "ReceivingLocation", "ReceivedTime", "CustomerID", "CodeName_C" };
- if (getAsnData.ToList().Count > 0)
- {
- Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
- //创建一个sheet
- Aspose.Cells.Worksheet sheet = workbook.Worksheets[0];
- //为单元格添加样式
- Aspose.Cells.Style style = workbook.Styles[workbook.Styles.Add()];
- style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;
- 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; //应用边界线 下边界线
- //给各列的标题行PutValue赋值
- int currow = 0;
- byte curcol = 0;
- //sheet.Cells.ImportCustomObjects((System.Collections.ICollection)getAsnData,
- //strTitle, true, 0, 0, getAsnData.Count, true, "yyyy/MM/dd HH:mm", false);
- sheet.Cells.ImportCustomObjects((System.Collections.ICollection)getAsnData,
- null, true, 0, 0, getAsnData.Count, true, "yyyy/MM/dd HH:mm", false);
- // 设置内容样式
- for (int i = 0; i < getAsnData.ToList().Count; i++)
- {
- for (int j = 0; j < 11; j++)
- {
- sheet.Cells[i + 1, j].Style = style;
- sheet.Cells[i + 1, 2].Style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Left;
- sheet.Cells[i + 1, 7].Style.Custom = "yyyy/MM/dd HH:mm";
- sheet.Cells[i + 1, 10].Style.Custom = "yyyy/MM/dd HH:mm";
- }
- }
- // 设置标题样式及背景色
- foreach (string s in ColTitle)
- {
- sheet.Cells[currow, curcol].PutValue(s);
- style.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0);
- style.Pattern = Aspose.Cells.BackgroundType.Solid;
- style.Font.IsBold = true;
- sheet.Cells[currow, curcol].Style = style;
- curcol++;
- }
- Aspose.Cells.Cells cells = sheet.Cells;
- //设置标题行高
- cells.SetRowHeight(0, 30);
- //让各列自适应宽度
- sheet.AutoFitColumns();
- //生成数据流
- System.IO.MemoryStream ms = workbook.SaveToStream();
- byte[] bt = ms.ToArray();
- //客户端保存的文件名
- string fileName = "入库查询数据导出" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
- //以字符流的形式下载文件
- Response.ContentType = "application/vnd.ms-excel";
- //通知浏览器下载文件而不是打开
- Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
- Response.BinaryWrite(bt);
- Response.Flush();
- Response.End();
- }
- }
Winform版本:
效果图如下:
- public void ExportExcelWithAspose(MDataTable dt, string fileName)
- {
- string saveFileName = "";
- SaveFileDialog saveDialog = new SaveFileDialog();
- saveDialog.DefaultExt = "xls";
- saveDialog.Filter = "Excel文件|*.xls";
- saveDialog.FileName = fileName;
- saveDialog.ShowDialog();
- saveFileName = saveDialog.FileName;
- if (saveFileName.IndexOf(":") < 0) return; //被点了取消
- Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
- if (xlApp == null)
- {
- MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");
- return;
- }
- try
- {
- Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
- Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
- Aspose.Cells.Worksheet cellSheet = workbook.Worksheets[0];
- Aspose.Cells.Cells cells = cellSheet.Cells ;//单元格
- //为单元格添加样式
- Aspose.Cells.Style style = workbook.Styles[workbook.Styles.Add()];
- //设置居中
- style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;
- //行索引
- int rowIndex = 0;
- //列索引
- int colIndex = 0;
- //列总数
- int colCount = dt.Columns.Count;
- //总行数
- int rowCount = dt.Rows.Count;
- rowIndex++;
- for (int i = 0; i < rowCount; i++)
- {
- colIndex = 0;
- for (int j = 0; j < colCount; j++)
- {
- if (j == 5) { cellSheet.Cells[rowIndex, colIndex].PutValue(Convert.ToDateTime (dt.Rows[i][j].Value).ToString("yyyy/MM/dd HH:mm:ss")); }
- else { cellSheet.Cells[rowIndex, colIndex].PutValue(dt.Rows[i][j].Value); }
- 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; //应用边界线 下边界线
- cellSheet.Cells[rowIndex, colIndex].Style = style;
- colIndex++;
- }
- rowIndex++;
- }
- //清除内容时行列索引值为0
- rowIndex = 0; colIndex = 0;
- //列名的处理
- for (int i = 0; i < colCount; i++)
- {
- cellSheet.Cells[rowIndex, colIndex].PutValue(dt.Columns[i].ColumnName);
- //设置背景颜色
- style.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0);
- style.Pattern = Aspose.Cells.BackgroundType.Solid;
- style.Font.IsBold = true;
- style.IsTextWrapped = true;
- cells.SetRowHeight(0, 38);//设置行高
- cellSheet.Cells[rowIndex, colIndex].Style = style;
- colIndex++;
- }
- cellSheet.AutoFitColumns();
- workbook.Save(Path.GetFullPath(saveFileName));
- xlApp.Quit();
- GC.Collect();//强行销毁
- MessageBox.Show("文件: " + fileName + ".xls 保存成功", "信息提示", MessageBoxButtons.OK,MessageBoxIcon.Information);
- }
- catch (Exception ex)
- {
- MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
- }
- }
C#+Aspose.Cells 导出Excel及设置样式 (Webform/Winform)的更多相关文章
- Aspose.Cells导出Excel(1)
利用Aspose.Cells导出excel 注意的问题 1.DataTable的处理 2.进行编码,便于中文名文件下载 3.别忘了Aspose.Cells.dll(可以自己在网上搜索) public ...
- C#使用Aspose.Cells导出Excel简单实现
首先,需要添加引用Aspose.Cells.dll,官网下载地址:http://downloads.aspose.com/cells/net 将DataTable导出Xlsx格式的文件下载(网页输出) ...
- Aspose.Cells导出Excel(2)
DataTable dtTitle = ds.Tables[]; DataTable dtDetail = ds.Tables[]; int columns = dtTitle.Columns.Cou ...
- aspose.Cells 导出Excel
aspose aspse.Cells可以操作Excel,且不依赖于系统环境. 使用模板,通过绑定输出数据源 这种适合于对格式没有特别要求的,直接绑定数据源即可.和数据绑定控件差不多. Workbook ...
- C# 使用Aspose.Cells 导出Excel
今天在工作中碰到同事用了一种新型的方式导入excel,在此做个学习记录. 插件:Aspose.Cells 第一步:准备好导出的模板,例子: C#代码: #region 验证数据 if (model = ...
- Aspose.Cells 导出 excel
Aspose.Cells.Workbook book = new Aspose.Cells.Workbook(); Aspose.Cells.Worksheet sheet = book.Worksh ...
- aspose.cell 给excel表格设置样式
方法1: Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式 styleTitle.HorizontalAlignment ...
- 使用Aspose.Cells读取Excel
最新更新请访问: http://denghejun.github.io Aspose.Cells读取Excel非常方便,以下是一个简单的实现读取和导出Excel的操作类: 以下是Aspose.Ce ...
- 使用aspose.cell导出excel需要注意什么?
1.如果导出的数据源是汇总出来的,最好方法是将数据源放到缓存里面,当基本数据源变化的时候,在改变数据2.使用模板导出EXCEL,这样很多样式可以在模板文件里面直接设置,例如:默认打开页签,让列头固定3 ...
随机推荐
- sencha touch 百度地图扩展(2014-6-24)(废弃 仅参考)
扩展代码如下: Ext.define('ux.BMap', { alternateClassName: 'bMap', extend: 'Ext.Container', xtype: 'bMap', ...
- mysql innodb存储引擎优化
innodb_data_home_dir 这是InnoDB表的目录共用设置.如果没有在 my.cnf 进行设置,InnoDB 将使用mysql的datadir目录为缺省目录.如果设定一个空字串,可以i ...
- 5-4 import,export属性
一.默认 export default 匿名的方法 这种导出的方式不需要知道变量的名字, 相当于是匿名的, 直接把开发的接口给export:如果一个js模块文件就只有一个功能, 那么就可以使用expo ...
- C语言qsort用法
一.对int类型数组排序 int num[100]; Sample: int cmp ( const void *a , const void *b ) { return *(int *)a - *( ...
- Java虚拟机九 java.lang.String在虚拟机中的实现
在Java中,Java的设计者对String对象进行了大量的优化,主要有三个特点: 1.不变性: 不变性是指String对象一旦生成,则不能再对它进行改变.String的这个特点可以泛化成不变(imm ...
- python中filter(),map()和reduce()的用法及区别
先看filter()方法 print(list(filter(lambda n : n % 2 == 1, range(20))))# 结果 [1, 3, 5, 7, 9, 11, 13, 15, 1 ...
- js深浅copy
...点copy是浅拷贝var obj1 = [1,{a: 1}];//var obj2 = Object.assign( {}, obj1);//浅copy//var obj2 = JSON.par ...
- ActiveMQ延迟消息配置
ActiveMQ使用延迟消息,需要在activemq.xml配置文件中添加这项: schedulerSupport="true" <broker xmlns="ht ...
- 电话、地址、Email等常用正则表达式
正则表达式用于字符串处理.表单验证等场合,实用高效.现将一些常用的表达式收集于此,以备不时之需. 匹配中文字符的正则表达式: [\u4e00-\u9fa5]评注:匹配中文还真是个头疼的事,有了这个表达 ...
- Linux下 磁盘扩容的两种方式
Hadoop扩容 概述 Hadoop存储容量或计算能力不能满足日益增长的需求时,就需要扩容. 扩容有两个方案: 1) 增加磁盘 2) 增加节点 方案一:扩大虚拟磁盘 扩大容量 将虚拟的Linux关闭, ...