Aspose.Cells Excel导出导入

DLL下载地址  http://download.csdn.net/download/u011155153/9957024   

/// <summary>

        /// 将list 导出excel
        /// </summary>
        /// <typeparam name="T">model</typeparam>
        /// <param name="list">需要导出的list数据集</param>
        /// <param name="sheetName">标题</param>
        /// <param name="cols">需要导出的字段</param>
        /// <returns>excel保存的路径</returns>
        public string ExportExecl<T>(IList<T> list, string sheetName, List<Col> cols) where T : class, new()
        {
            string rptPath = PathExt.ConvertAbsolutePath("Report/WaterReport.xls");
            try
            {
                #region Aspose  创建工作簿
                /*创建工作簿*/
                Workbook workbook = new Workbook(rptPath);
                Worksheet sheet = workbook.Worksheets[0]; //工作表 
                Cells cells = sheet.Cells;//单元格
                //标题
                Style title = workbook.Styles[workbook.Styles.Add()];
                title.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;//字体居中
                //  cells.Merge(0, 0, 1, cols.Count);//合并单元格     前两个为单元格坐标 第三个参数为合并多少行,第四个参数合并多少列  
                title.Font.Size = 25;
                title.Font.IsBold = true;//粗体   
                // 表头
                Style header = workbook.Styles[workbook.Styles.Add()];
                header.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;//字体居中
                header.Font.Size = 14;
                header.Font.IsBold = true;//粗体   
                header.ForegroundColor = System.Drawing.Color.FromArgb(204, 204, 204);//设置背景色 可以参考颜色代码对照表  
                header.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;//应用边界线 左边界线  
                header.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; //应用边界线 右边界线  
                header.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;//应用边界线 上边界线  
                header.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;//应用边界线 下边界线  
                //边框
                Style border = workbook.Styles[workbook.Styles.Add()];
                border.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;//字体居中
                border.IsTextWrapped = true;//单元格内容自动换行  
                border.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;//应用边界线 左边界线  
                border.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; //应用边界线 右边界线  
                border.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;//应用边界线 上边界线  
                border.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;//应用边界线 下边界线  
                #endregion


                #region Aspose  样式设置事例


                //Aspose.Cells.Style style1 = workbook.Styles[workbook.Styles.Add()];
                style1.Pattern = Aspose.Cells.BackgroundType.Solid;//单元格的线:实线  
                //style1.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;//字体居中  
                //style1.Font.Name = "宋体";//文字字体  
                //style1.Font.Size = 25;//文字大小  
                //style1.IsTextWrapped = true;//单元格内容自动换行  
                style1.ForegroundColor = System.Drawing.Color.FromArgb(50, 205, 50);//设置背景色 可以参考颜色代码对照表  
                //style1.Font.IsBold = true;//粗体  
                style1.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;//应用边界线 左边界线  
                style1.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; //应用边界线 右边界线  
                style1.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;//应用边界线 上边界线  
                style1.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;//应用边界线 下边界线  
                //cells[row, 0].PutValue(sheetName);
                //cells[row, 0].SetStyle(title);
                //cells.SetColumnWidth(0, 30);//设置列宽  
                //cells.Merge(0, 0, 1, cols.Count);//合并单元格     前两个为单元格坐标 第三个参数为合并多少行,第四个参数合并多少列
                //cells.SetRowHeight(0, 38);//设置行高   
                #endregion


                int row = 0;
                cells[row, 0].PutValue(sheetName);
                cells[row, 0].SetStyle(title);
                cells.SetColumnWidth(0, 30);//设置列宽  
                //生成列名
                row++;
                for (int i = 0; i < cols.Count; i++)
                {
                    cells[row, i].PutValue(cols[i].Name);
                    cells[row, i].SetStyle(header);
                }
                //生成数据  
                if (list != null && list.Count > 0)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        row++;
                        for (int j = 0; j < cols.Count; j++)

                        {

    cells[row, j].PutValue(typeof(T).GetProperty(Code).GetValue(list[i]));

                            cells[row, j].SetStyle(border);
                        }
                    }
                }


                #region 验证是否存在文件夹
                string path = "/Upload/";
                string rootPath = PathExt.ConvertAbsolutePath(path);
                if (!System.IO.Directory.Exists(rootPath))
                {
                    Directory.CreateDirectory(rootPath);
                } 
                #endregion


                string FileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
                string savePath = PathExt.GetTempFile(rootPath, FileName);
                workbook.Save(savePath);
                return "Upload/" + FileName;
            }
            catch (Exception e)
            {
                return " IListToExcel: " + e.Message;
            }
        }




        /// <summary>
        /// 导入excel 返回DataTable
        /// </summary>
        /// <param name="strFileName">excel文件地址</param>
        /// <returns>DataTable</returns>
        public System.Data.DataTable ReadExcel(String strFileName)
        {
            Workbook book = new Workbook();
            book.Open(strFileName);
            Worksheet sheet = book.Worksheets[0];
            Cells cells = sheet.Cells;
            return cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true);

        }

public class Col
    { /// <summary>
        /// 中文名(显示名称)
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 英文(数据库查询字段)
        /// </summary>
        public string ColName { get; set; }
    }

   List<Col> list = new List<Col>() {
                new Col() { Name = "popedomId", ColName = "popedomId" },
                new Col() { Name = "workIngId", ColName = "workIngId" },
                new Col() { Name = "workingSeatId", ColName = "workingSeatId" },
                new Col() { Name = "questionTypeId", ColName = "questionTypeId" },
                new Col() { Name = "questionId", ColName = "questionId" },
                new Col() { Name = "shiftId", ColName = "shiftId" },
                new Col() { Name = "classId", ColName = "classId" },
                new Col() { Name = "solutionId", ColName = "solutionId" },
                new Col() { Name = "userId", ColName = "userId" } 
            };

 public string AA()
        {
            AsposeExportExcel excel = new AsposeExportExcel();
            string url = excel.ExportExecl<VW_WorkNote>("需要导出的List", "sheetName", list);
            return url;
        }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值