|
本帖最后由 huhangfei 于 2013-8-23 17:41 编辑
传入的集合,还有每一列的title不能灵活多变,太死。。。。怎么改?- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using org.in2bits.MyXls;
- namespace TXManager.Controllers
- {
- public class ExcelResult<T> : ActionResult where T : OutputExcel
- {
- public ExcelResult(IList<T> entity, string fileName, string wSheet)
- {
- this.Entity = entity;
- DateTime time = DateTime.Now;
- this.FileName = string.Format("{0}_{1}_{2}_{3}_{4}",
- fileName, time.Month, time.Day, time.Hour, time.Minute);
- this.WSheet = wSheet;
- }
- public ExcelResult(IList<T> entity)
- {
- this.Entity = entity;
- DateTime time = DateTime.Now;
- this.FileName = string.Format("{0}_{1}_{2}_{3}_{4}",
- this.FileName, time.Month, time.Day, time.Hour, time.Minute);
- }
- public IList<T> Entity
- {
- get;
- set;
- }
- public string FileName
- {
- get;
- set;
- }
- /// <summary>
- /// 需要生成EXCEL模板 WorkSheet1:约看中 WorkSheet2:
- /// </summary>
- public string WSheet
- {
- get;
- set;
- }
- public override void ExecuteResult(ControllerContext context)
- {
- if (Entity == null)
- {
- new EmptyResult().ExecuteResult(context);
- return;
- }
- SetResponse(context);
- }
- /// <summary>
- /// 设置并向客户端发送请求响应。
- /// </summary>
- /// <param name="context"></param>
- private void SetResponse(ControllerContext context)
- {
- XlsDocument xls = new XlsDocument();//创建空xls文档
- xls.FileName = FileName;
- Worksheet sheet = xls.Workbook.Worksheets.AddNamed(this.FileName);
- //设置文档列属性
- ColumnInfo cinfo = new ColumnInfo(xls, sheet);//设置xls文档的指定工作页的列属性
- cinfo.Collapsed = true;
- //设置列的范围 如 0列-10列
- cinfo.ColumnIndexStart = 0;//列开始
- cinfo.ColumnIndexEnd = 10;//列结束
- cinfo.Collapsed = true;
- cinfo.Width = 85 * 65;//列宽度
- sheet.AddColumnInfo(cinfo);
- //设置文档列属性结束
- ConvertEntity(sheet);
- xls.Send();
- }
- /// <summary>
- /// 把泛型集合转换成组合Excel表格的字符串。
- /// </summary>
- /// <returns></returns>
- private void ConvertEntity(Worksheet sheet)
- {
- Cells cells = sheet.Cells; //获得指定工作页列集合
- AddCells(cells);
- AddTableBody(cells);
- }
- public void AddCells(Cells cell)
- {
- switch (this.WSheet)
- {
- case "WorkSheet1":
- cell.Add(1, 1, "房源编号");
- cell.Add(1, 2, "小区名");
- cell.Add(1, 3, "所在城市");
- cell.Add(1, 4, "详细地址");
- cell.Add(1, 5, "支付方式");
- cell.Add(1, 6, "房东报价");
- cell.Add(1, 7, "出价时间");
- cell.Add(1, 8, "真实姓名");
- cell.Add(1, 9, "手机号码");
- cell.Add(1, 10, "出价次数");
- cell.Add(1, 11, "出价金额");
- cell.Add(1, 12, "");
- cell.Add(1, 13, "");
- cell.Add(1, 14, "");
- break;
- case "WorkSheet2":
- cell.Add(1, 1, "");
- cell.Add(1, 2, "");
- cell.Add(1, 3, "");
- cell.Add(1, 4, "");
- cell.Add(1, 5, "");
- cell.Add(1, 6, "");
- cell.Add(1, 7, "");
- cell.Add(1, 8, "");
- cell.Add(1, 9, "");
- cell.Add(1, 10, "");
- cell.Add(1, 11, "");
- cell.Add(1, 12, "");
- cell.Add(1, 13, "");
- cell.Add(1, 14, "");
- break;
- default:
- break;
- }
- }
- /// <summary>
- /// 根据IList泛型集合中的每项的属性值来组合Excel表格。
- /// </summary>
- /// <param name="sb"></param>
- private void AddTableBody(Cells cells)
- {
- if (Entity == null || Entity.Count <= 0)
- {
- return;
- }
- int tmepCell = 1;
- foreach (var item in Entity)
- {
- tmepCell++;
- cells.Add(tmepCell, 1, item.CellText1);
- cells.Add(tmepCell, 2, item.CellText2);
- cells.Add(tmepCell, 3, item.CellText3);
- cells.Add(tmepCell, 4, item.CellText4);
- cells.Add(tmepCell, 5, item.CellText5);
- cells.Add(tmepCell, 6, item.CellText6);
- cells.Add(tmepCell, 7, item.CellText7);
- cells.Add(tmepCell, 8, item.CellText8);
- cells.Add(tmepCell, 9, item.CellText9);
- cells.Add(tmepCell, 10, item.CellText10);
- cells.Add(tmepCell, 11, item.CellText11);
- cells.Add(tmepCell, 12, item.CellText12);
- cells.Add(tmepCell, 13, item.CellText13);
- cells.Add(tmepCell, 14, item.CellText14);
- }
- }
- }
- }
- //-----------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace TXManager.Controllers
- {
- public class OutputExcel
- {
- private string _CellText1 = string.Empty;
- public string CellText1
- {
- get { return _CellText1; }
- set { _CellText1 = value; }
- }
- private string _CellText2 = string.Empty;
- public string CellText2
- {
- get { return _CellText2; }
- set { _CellText2 = value; }
- }
- private string _CellText3 = string.Empty;
- public string CellText3
- {
- get { return _CellText3; }
- set { _CellText3 = value; }
- }
- private string _CellText4 = string.Empty;
- public string CellText4
- {
- get { return _CellText4; }
- set { _CellText4 = value; }
- }
- private string _CellText5 = string.Empty;
- public string CellText5
- {
- get { return _CellText5; }
- set { _CellText5 = value; }
- }
- private string _CellText6 = string.Empty;
- public string CellText6
- {
- get { return _CellText6; }
- set { _CellText6 = value; }
- }
- private string _CellText7 = string.Empty;
- public string CellText7
- {
- get { return _CellText7; }
- set { _CellText7 = value; }
- }
- private string _CellText8 = string.Empty;
- public string CellText8
- {
- get { return _CellText8; }
- set { _CellText8 = value; }
- }
- private string _CellText9 = string.Empty;
- public string CellText9
- {
- get { return _CellText9; }
- set { _CellText9 = value; }
- }
- private string _CellText10 = string.Empty;
- public string CellText10
- {
- get { return _CellText10; }
- set { _CellText10 = value; }
- }
- private string _CellText11 = string.Empty;
- public string CellText11
- {
- get { return _CellText11; }
- set { _CellText11 = value; }
- }
- private string _CellText12 = string.Empty;
- public string CellText12
- {
- get { return _CellText12; }
- set { _CellText12 = value; }
- }
- private string _CellText13 = string.Empty;
- public string CellText13
- {
- get { return _CellText13; }
- set { _CellText13 = value; }
- }
- private string _CellText14 = string.Empty;
- public string CellText14
- {
- get { return _CellText14; }
- set { _CellText14 = value; }
- }
- }
- }
- //----------------------------------
- public ActionResult ExportExcel(SearchAgentComPany search)
- {
- var list = _agentcpbll.SelectComPanylistByPage(search, 1, 10);
- //
- return new ExcelResult<OutputExcel>(list.ConvertAll(it => new OutputExcel() {
- CellText1 = it.Id.ToString(),
- CellText2 = it.Name,
- CellText3 = it.Name,
- CellText4 = it.Address,
- CellText5 = it.Name,
- CellText6 = it.Name,
- CellText7 = it.Name,
- CellText8 = it.Name,
- CellText9 = it.Name,
- CellText10 = it.Name,
- CellText11 = it.CreateTime.ToString()
- }), "约看中", "WorkSheet1");
- }
复制代码 |
|