C# Aspose填充excel模板数据,下载

 public Result<object> GetAExcelFieItem(string[] aid, int kind, string contype) {
            string fileUrl = AppDomain.CurrentDomain.BaseDirectory + "\\Template\\FileItemData.xls";
            Result<object> result = new Result<object>(); 
            try
            {
                List<Volumes> listvol = Item.Where(w => aid.Any(a => a == w.ID)).OrderBy(o => o.Sort).ToList(); //获取数
                WorkbookDesigner designer = new WorkbookDesigner();
                Workbook workbook = new Workbook(fileUrl);
                Worksheet worksheet = workbook.Worksheets[0];

                Cells cells = worksheet.Cells;
                Aspose.Cells.Range range = cells.CreateRange("A1", "G19");
                //打开模板文件,得到WorkBook对象  
                int allpagecount = 0; 

                listvol = listvol == null ? new List<Volumes>() : listvol;
                foreach (var vol in listvol)
                {
                    var vlis = List.Where(p=> p.VolumesID == vol.ID).OrderBy(p=> p.Sort).ToList();
                    int pagecount = vlis.Count / 15 + (vlis.Count % 15 == 0 ? 0 : 1); //当前卷的总页数 
                    for (int j = 1; j <= pagecount; j++)
                    {
                        int startrow = allpagecount * 19;  
                        Aspose.Cells.Range range2 = cells.CreateRange(startrow, 0, startrow+19, 7); 
                        range2.Copy(range);
                        range2.RowHeight = 40;
                        
                        worksheet.Cells[startrow + 1, 2].PutValue(vol.VolumesName);
                        worksheet.Cells[startrow + 1, 5].PutValue(vol.VolumesCode);
                        worksheet.Cells[startrow + 2, 5].PutValue(vol.RetentionPeriod);
                        cells.SetRowHeight(startrow + 1, 31);
                        cells.SetRowHeight(startrow + 2, 31); 

                        worksheet.Cells.ClearContents(startrow + 4, 0, startrow + 19, 7);//删除起始单元格到结束单元格数据
                        int pagerow = (j == pagecount && vlis.Count % 15 != 0) ? vlis.Count % 15 : 15;//当前页行数
                        object[,] arr = new object[pagerow, 7];
                        for (int i = 0; i < pagerow; i++)
                        {
                            int index = (j - 1) * 15 + i;//当前行在卷内的序号 
                            worksheet.Cells[startrow + 4 + i, 0].PutValue(vlis[index].Sort);
                            worksheet.Cells[startrow + 4 + i, 1].PutValue(vlis[index].FileCode);
                            worksheet.Cells[startrow + 4 + i, 2].PutValue(vlis[index].Responsible);
                            worksheet.Cells[startrow + 4 + i, 3].PutValue(vlis[index].FileName);
                            worksheet.Cells[startrow + 4 + i, 4].PutValue(vlis[index].FileDate);
                            worksheet.Cells[startrow + 4 + i, 5].PutValue(vlis[index].PageNum);
                            worksheet.Cells[startrow + 4 + i, 6].PutValue(vlis[index].Remark);
                        } 
                        allpagecount++; 
                    }
                }
                System.IO.MemoryStream ms = workbook.SaveToStream();//生成数据流
               
               //转换成流字节,输出浏览器下载 
                byte[] ret = ms.GetBuffer();
                result.Data = ret;
                result.Message = "目录.xls"; 
                result.DataCount = 1;
                //关闭流
                ms.Close();
                GC.Collect(); 
            }
            catch (Exception e)
            {
                result.Flag = false;
                GC.Collect();
                if (e.InnerException != null)
                    result.Message = e.InnerException.Message;
                else
                    result.Message = e.Message;
            }
            return result;
        }

 

posted @ 2017-11-17 16:46  荆棘中的百合花  阅读(1447)  评论(0编辑  收藏  举报