如何使用Aspose.Cells控件生成Excel文件

作者:控件中国网   出处:控件中国网   2015-05-26 21:52:58   阅读:4

 Aspose.Cells是一款功能强大的Excel文档处理和转换控件,开发人员和客户电脑无需安装Microsoft Excel也能在应用程序中实现类似Excel的强大数据管理功能,支持所有Excel格式类型的操作,在没有Microsoft Excel的环境下,用户也可为其应用程序嵌入类似Excel的强大数据管理功能。Aspose.Cells可以对每一个具体的数据,表格和格式进行管理,在各个层面导入图像,应用复杂的计算公式,并将应用程序中的表格保存为各种格式等。

Aspose Cells 最新试用版下载地址:http://www.componentcn.com/kongjianchanpin/yonghujiemian/biaogekongjian/2014-09-16/174.html 

控件中国网是Aspose公司在中国的授权销售商,能够提供优惠的Aspose购买价格,同时也能够保障购买客户得到完善的技术支持服务,详情请拨打咨询电话:023-6787 0900联系控件中国网产品经理进一步了解该产品。

如何使用Aspose.Cells控件生成Excel文件呢?

这篇文章主要介绍了使用Aspose.Cells组件生成Excel文件的方法,大家参考使用吧

生成带表头的Excel文件,格式如下显示。

 

1.jpg

 

当然更复杂的一些也可以通过 合并单元格的方法 public void Merge(int firstRow, int firstColumn, int totalRows, int totalColumns)来实现。

实现方式:

 

1. 首先,需要添加对"Aspose.Cells.dll"的引用。

2. 实现代码如下:

 

代码如下:

 

//新建工作簿 

            Workbook workbook = new Workbook(); //工作簿 

            Worksheet sheet = workbook.Worksheets[0]; //工作表 

            Cells cells = sheet.Cells;//单元格

 

            Style style = workbook.Styles[workbook.Styles.Add()];//新增样式

            #region 表头

            //标题 

            style.HorizontalAlignment = TextAlignmentType.Center;//文字居中  

            style.Font.Name = "宋体";//文字字体 

            style.Font.Size = 18;//文字大小  

            style.Font.IsBold = true;//粗体

            cells.Merge(0, 0, 1, 12);               //合并单元格 

            cells[0, 0].PutValue("标准化工作意见建议汇总表");   //填写内容 

            cells[0, 0].SetStyle(style);            //给单元格关联样式  

            cells.SetRowHeight(0, 28);              //设置行高 

 

            //发布时间

            style.HorizontalAlignment = TextAlignmentType.Left;

            style.Font.Size = 11;

            style.Font.IsBold = false;

            cells.Merge(1, 0, 1, 7);

            cells[1, 0].PutValue(String.Format("发布起止时间:{0}至{1}",DateTime.Now.AddDays(-1).ToString("yyyy年MM月dd日"),DateTime.Now.ToString("yyyy年MM月dd日")));

            cells[1, 0].SetStyle(style);

            cells.SetRowHeight(1, 20);

            //统计时间

            style.HorizontalAlignment = TextAlignmentType.Right;

            style.Font.Size = 11;

            style.Font.IsBold = false;

            cells.Merge(1, 7, 1, 5);

            cells[1, 7].PutValue(String.Format("统计时间:{0}", DateTime.Now.ToString("yyyy年MM月dd日")));

            cells[1, 7].SetStyle(style);

            cells.SetRowHeight(1, 20);

            #endregion

            #region 表格

            #region 表格标题行

            //序号

            style.HorizontalAlignment = TextAlignmentType.Center;

            cells[2, 0].PutValue("序号");

            cells[2, 0].SetStyle(style);

            cells.SetRowHeight(2, 20);

            cells.SetColumnWidthPixel(0, 38);

            //建议时间

            cells[2, 1].PutValue("建议时间");

            cells[2, 1].SetStyle(style); 

            cells.SetColumnWidthPixel(1, 77);

            //建议部门

            cells[2, 2].PutValue("建议部门");

            cells[2, 2].SetStyle(style);

            cells.SetColumnWidthPixel(2, 107);

            //建 议 人

            cells[2, 3].PutValue("建 议 人");

            cells[2, 3].SetStyle(style);

            cells.SetColumnWidthPixel(3, 69);

            //类   别

            cells[2, 4].PutValue("类   别");

            cells[2, 4].SetStyle(style);

            cells.SetColumnWidthPixel(4, 71);

            //业务种类

            cells[2, 5].PutValue("业务种类");

            cells[2, 5].SetStyle(style);

            cells.SetColumnWidthPixel(5, 71);

            //标准名称

            cells[2, 6].PutValue("标准名称");

            cells[2, 6].SetStyle(style);

            cells.SetColumnWidthPixel(6, 114);

            //标准章、条编号

            cells[2, 7].PutValue("标准章、条编号");

            cells[2, 7].SetStyle(style);

            cells.SetColumnWidthPixel(7, 104);

            //意见建议

            cells[2, 8].PutValue("意见建议");

            cells[2, 8].SetStyle(style);

            cells.SetColumnWidthPixel(8, 255);

            //处理部门

            cells[2, 9].PutValue("处理部门");

            cells[2, 9].SetStyle(style);

            cells.SetColumnWidthPixel(9, 72);

            //处理进度

            cells[2, 10].PutValue("处理进度");

            cells[2, 10].SetStyle(style);

            cells.SetColumnWidthPixel(10, 72);

            //备注

            cells[2, 11].PutValue("备注");

            cells[2, 11].SetStyle(style);

            cells.SetColumnWidthPixel(11, 255);

            #endregion

            #endregion

 

            System.IO.MemoryStream ms = workbook.SaveToStream();//生成数据流 

            byte[] bt = ms.ToArray();

            workbook.Save(@"E:\test.xls");//保存到硬盘 

 

3. 生成好的Excel可以保存到磁盘,也可以在web页面上通过流的方式来下载。

 

 

 

代码如下:

 

//下载

            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();

 

控件中国网是Aspose公司在中国的授权销售商,能够提供优惠的Aspose购买价格,同时也能够保障购买客户得到完善的技术支持服务,详情请拨打咨询电话:023-6787 0900联系控件中国网产品经理进一步了解该产品。

Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat