0

好吧,这肯定以前没有被问过。

我有一个返回 IQueryable 的方法,客户端要求是一个特定示例,我们应该将其导出到 excel 中而不在 gridview 中显示结果。

任何想法?

string strSql = BuildQuery();
            try
            {
                var list = RequestBaseBL.GetRequestByCustomQuery(strSql, DdlRequestType.SelectedValue).ToList();
4

1 回答 1

3

要在 excel 中导出,请看这里:

http://epplus.codeplex.com/releases/view/42439

然后您可以创建自己的类来为 IEnumerable 生成 excel 导出

这里有一些提示(这些是更大对象的一部分,所以只是提示,没有任何编译)可能来自我的一个项目:

 public interface IExcelReporting : IServiceObject
    {
        ColumnsExcel Columns { get; }

        void SetDatasource(IEnumerable datasource);
        void SetHeaderLabelMerge(int columnMerge);
        void AddHeader(string label, string value);
        Byte[] ExportToExcel(string title, string author, DateTime date);
    }

实施示例:

  public Byte[] ExportToExcel(string title, string author, DateTime date)
    {
        ExcelPackage package = new ExcelPackage();

        ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(title);

if (_Columns != null)
            {

                Dictionary<int, Func<object, string>> internalMappingGetter = new Dictionary<int, Func<object, string>>();

                //create the Header of the body
                foreach (ColumnExcel entity in _Columns)
                {
                    worksheet.Cells[RowIndex, ColumnIndex].Value = entity.HeaderName;
                    worksheet.Cells[RowIndex, ColumnIndex].Style.WrapText = true;
                    BorderCell(worksheet.Cells[RowIndex, ColumnIndex]);
                    worksheet.Column(ColumnIndex).Width = entity.Width;
                    ColumnIndex++;
                }

                RowIndex++;

                if (_DataSource != null)
                {
                    foreach (Object o in _DataSource)
                    {
                        ColumnIndex = 1;
                        foreach (ColumnExcel column in _Columns)
                        {
                            column.Apply(worksheet.Cells[RowIndex, ColumnIndex], o);
                            worksheet.Column(ColumnIndex).BestFit = true;
                            worksheet.Cells[RowIndex, ColumnIndex].Style.WrapText = true;
                            //BorderCell(worksheet.Cells[RowIndex, ColumnIndex]);
                            ColumnIndex++;
                        }
                        RowIndex++;
                    }
                }

和列类:

public class ColumnsExcel : IEnumerable
    {
        List<ColumnExcel> _Columns;

        public ColumnsExcel()
        {
            _Columns = new List<ColumnExcel>();
        }


        public void AddInt(Func<object,int> getValue, string headerName, int width, string format)
        {
            ColumnExcel entity = new ColumnExcelInt(headerName, width, format, getValue);
            _Columns.Add(entity);
        }

        public void AddString(Func<object, string> getValue, string headerName, int width )
        {
            ColumnExcel entity = new ColumnExcelstring(headerName, width, getValue);
            _Columns.Add(entity);
        }

        public void AddDateTime(Func<object, DateTime?> getValue, string headerName, int width, string format)
        {
            ColumnExcel entity = new ColumnExcelDateTime(headerName, width, format, getValue);
            _Columns.Add(entity);
        }

        public void AddDecimal(Func<object, decimal> getValue, string headerName, int width, string format)
        {
            ColumnExcel entity = new ColumnExcelDecimal(headerName, width, format, getValue);
            _Columns.Add(entity);
        }

        public IEnumerator GetEnumerator()
        {
            return _Columns.GetEnumerator();
        }
    }

及其用途:

 //prepare the Ienumerable<MyObject>
 var interventi = GetInterventoSchedeConsuntivi()

 //prepare the report
 IExcelReporting report = ReportingFactory.GetInstance();
 report.SetDatasource(interventi);
 report.AddHeader("Lotto:", lotto);

 report.Columns.AddString((object v) => ((InterventoSchedeConsuntiviView)v).Lotto, "Lotto", 20);

using (System.IO.Stream s = File.Create(filepath))
                    {
                        byte[] csv = report.ExportToExcel(("titleFile", string.Empty, Servizi.DataOra.Now);
                        s.Write(csv, 0, csv.Length);
                    }
于 2012-06-01T08:43:59.323 回答