zoukankan      html  css  js  c++  java
  • 从数据库中导出excel报表

    通常需要将后台数据库中的数据集或者是其他列表等导出excel 报表,这里主要引用了Apose.cells dll 类库,

    (1)直接上主要代码:

      protected void txtExport_Click(object sender, EventArgs e)         {            

                  try             {               

                     // 获取测试商品报表               

                    IList<ProductEntity> pList = ProductBLL.getProductList();

                    // 导出到Excel中                

                   Workbook workbook = new Workbook();               

                   Worksheet sheet = workbook.Worksheets[0];            

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

                    for (int i = 0; i < 3; i++)        {      

                            cells.SetColumnWidth(i, 20);     

                                                               }

                    cells[1, 0].PutValue("商品序号");        

                    cells[1, 1].PutValue("商品名称");        

                    cells[1, 2].PutValue("描述");

                    for (int i = 0; i < pList.Count; i++)                 {         

                            var p = pList[i];                  

                          cells[2 + i, 0].PutValue(p.Number);         

                          cells[2 + i, 1].PutValue(p.Name);            

                         cells[2 + i, 2].PutValue(p.Description);

                        }              

              string fileName = "商品列表报表" + DateTime.Now.ToString("yyyy.MM.dd.HH.mm.ss.ms") + ".xls";             

             string saveFileName = HttpContext.Current.Server.MapPath(@"ExportFile" + fileName);

                    workbook.Save(saveFileName);

                    Label1.Text = "导出报表成功!";        

             }

                catch (Exception ex)             {            

                 Label1.Text = "导出报表失败,原因:" + ex.Message;             }

            }

    (2)获取ProductList 的方法getProductList():

            public static IList<ProductEntity> getProductList(){                   

                       IList<ProductEntity> productList = new List<ProductEntity>();

                      DataTable dt = ProductDAL.getProductList();

                       if(dt!=null && dt.Rows.Count>0)           

                          {

                           productList =DatatableToObject.ConvertToList<ProductEntity>(dt);            

                           }

                         return productList;

            }

    (3) 顺便介绍比较实用用的DataToObject.ConvertToList<T>() 方法,可以直接将从数据库中读取的DataTable 转化成列表List<T>,

     最近才在上面写文章,不会自动对齐格式哈。

        public class DatatableToObject     {       

              public static T ConvertToObject<T>(DataRow row) where T : new()         {

                             System.Object obj = new T();         

                            if (row != null)             {    

                             DataTable dataTable = row.Table;           

                             GetObject(dataTable.Columns, row, obj);            

                            }

                     if (obj != null && obj is T)             {      

                                return (T)obj;            

                              }           

                    else             {

                           return default(T);            

                                      }       

                      }

            private static void GetObject(DataColumnCollection cols, DataRow dr, System.Object obj)       

                   {             Type type = obj.GetType();

                                PropertyInfo[] pros = type.GetProperties();   

                                foreach (PropertyInfo pro in pros)             {

                                if (cols.Contains(pro.Name))           

                                    {

                                 if ((pro.PropertyType).Name.ToLower() == "string")         

                                       pro.SetValue(obj, dr[pro.Name] == DBNull.Value ? "" : dr[pro.Name].ToString(), null);          

                                 else                      

                                       pro.SetValue(obj, dr[pro.Name] == DBNull.Value ? null : dr[pro.Name], null);              

                                      }

                               }      

                      }

            public static List<T> ConvertToList<T>(DataTable dataTable) where T : new()     

                          {       List<T> list = new List<T>();            

                                 foreach (DataRow row in dataTable.Rows)             {    

                                        T obj = ConvertToObject<T>(row);         

                                        list.Add(obj);             }         

                                 return list;      

                             }    

                     }

    (4) 部分运行截图:

      

     

  • 相关阅读:
    TCP和UDP的最完整的区别
    kafka重置到最新offset偏移量
    MYSQL中IN,INSTR,FIND_IN_SET函数效率比较
    本地不安装ORACLE,用PLSQL访问远程数据库
    MySQL中的DEFINER与SQL SECURITY
    Centos6.8 安装tomcat8.5.11
    动态代理模式
    linux下mysql允许远程连接
    全面理解Java中的String数据类型
    Spring中获取web项目的根目录
  • 原文地址:https://www.cnblogs.com/Fluent-1202/p/6230610.html
Copyright © 2011-2022 走看看