使用Aspose.Cells组件生成Excel文件实例
生成带表头的Excel文件,格式如下显示。
当然更复杂的一些也可以通过合并单元格的方法publicvoidMerge(intfirstRow,intfirstColumn,inttotalRows,inttotalColumns)来实现。
实现方式:
1.首先,需要添加对"Aspose.Cells.dll"的引用。
2.实现代码如下:
//新建工作簿
Workbookworkbook=newWorkbook();//工作簿
Worksheetsheet=workbook.Worksheets[0];//工作表
Cellscells=sheet.Cells;//单元格
Stylestyle=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.MemoryStreamms=workbook.SaveToStream();//生成数据流
byte[]bt=ms.ToArray();
workbook.Save(@"E:\test.xls");//保存到硬盘
}
3.生成好的Excel可以保存到磁盘,也可以在web页面上通过流的方式来下载。
//下载
System.IO.MemoryStreamms=workbook.SaveToStream();//生成数据流
byte[]bt=ms.ToArray();
stringfileName="标准化工作意见建议汇总表"+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();
相关文章
- Excel如何统计重复次数
- 导出的Excel名字乱码_恢复的excel文件乱码
- Excel连接字符串「建议收藏」
- python运算符优先级_excel运算符优先级最高的
- 前端必读:如何在 JavaScript 中使用SpreadJS导入和导出 Excel 文件
- Python自动化办公--Pandas玩转Excel【一】
- Python操作Excel的一些基本程序代码
- Java读取Excel文件详解编程语言
- java处理Excel文件—excel文件的创建,删除,写入,读取详解编程语言
- Java动态生成条形码并将条形码插入进excel中详解编程语言
- 操作excel展示Linux下Poi妙用:操作Excel(linux下poi)
- 如何使用Excel连接MySQL数据库(excel连接mysql数据库)
- MySQL数据导出Excel快速教程(mysql转excel)
- MySQL读取Excel文件的简单方法(mysql读取excel)
- 轻松操作:oracle快速导入Excel数据(oracle导入excel数据)
- MSSQL导入Excel数据的精彩实战(mssql读excel)
- 如何将Excel数据导入MySQL数据库学习MySQL中Excel的基本用法(mysql中excel)
- asp.net操作excel的实现代码
- php读取EXCEL文件phpexcelreader读取excel文件
- Python读写Excel文件的实例
- js导入导出excel(实例代码)
- OpenXml读写Excel实例代码
- java使用poi读取excel内容方法实例
- asp中把数据导出为excel的2种方法