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[]; Cells cells = worksheet.Cells;
Aspose.Cells.Range range = cells.CreateRange("A1", "G19");
//打开模板文件,得到WorkBook对象
int allpagecount = ; 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 / + (vlis.Count % == ? : ); //当前卷的总页数
for (int j = ; j <= pagecount; j++)
{
int startrow = allpagecount * ;
Aspose.Cells.Range range2 = cells.CreateRange(startrow, , startrow+, );
range2.Copy(range);
range2.RowHeight = ; worksheet.Cells[startrow + , ].PutValue(vol.VolumesName);
worksheet.Cells[startrow + , ].PutValue(vol.VolumesCode);
worksheet.Cells[startrow + , ].PutValue(vol.RetentionPeriod);
cells.SetRowHeight(startrow + , );
cells.SetRowHeight(startrow + , ); worksheet.Cells.ClearContents(startrow + , , startrow + , );//删除起始单元格到结束单元格数据
int pagerow = (j == pagecount && vlis.Count % != ) ? vlis.Count % : ;//当前页行数
object[,] arr = new object[pagerow, ];
for (int i = ; i < pagerow; i++)
{
int index = (j - ) * + i;//当前行在卷内的序号
worksheet.Cells[startrow + + i, ].PutValue(vlis[index].Sort);
worksheet.Cells[startrow + + i, ].PutValue(vlis[index].FileCode);
worksheet.Cells[startrow + + i, ].PutValue(vlis[index].Responsible);
worksheet.Cells[startrow + + i, ].PutValue(vlis[index].FileName);
worksheet.Cells[startrow + + i, ].PutValue(vlis[index].FileDate);
worksheet.Cells[startrow + + i, ].PutValue(vlis[index].PageNum);
worksheet.Cells[startrow + + i, ].PutValue(vlis[index].Remark);
}
allpagecount++;
}
}
System.IO.MemoryStream ms = workbook.SaveToStream();//生成数据流 //转换成流字节,输出浏览器下载
byte[] ret = ms.GetBuffer();
result.Data = ret;
result.Message = "目录.xls";
result.DataCount = ;
//关闭流
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;
}
上一篇:python数据分析入门学习笔记


下一篇:PHP扩展安装mcrypt 提示没有可用包(No package php-mcrypt available)