您的位置:首页 > 编程语言 > ASP

利用Aspose.Cells 组件导出数据到excel

2015-10-10 12:38 831 查看
利用Aspose.Cells 导出数据到excel 。需要引入Aspose.Cells.dll;

具体使用方法贴代码:

private string ExportProjectReportExcel(List<ProjectReportDetail> prdlist, string ProjectReportName)
{
//新建工作簿
Workbook wb = new Workbook();
//新建样式
Style style = wb.Styles[wb.Styles.Add()];
//设置单元格水平居中对齐和垂直居中对齐
style.HorizontalAlignment = TextAlignmentType.Center;
//新建工作表
Worksheet ws = wb.Worksheets[0];
//ws.ClearComments();
//添加标题内容
ws.Cells[0, 0].PutValue("项目团队");
ws.Cells[0, 1].PutValue("任务名称");
ws.Cells[0, 2].PutValue("人员分工");
ws.Cells[0, 3].PutValue("开始时间");
ws.Cells[0, 4].PutValue("结束时间");
ws.Cells[0, 5].PutValue("总数量");
ws.Cells[0, 6].PutValue("累计完成数量");
ws.Cells[0, 7].PutValue("本周完成数量");
ws.Cells[0, 8].PutValue("进度");

//添加表数据
for (int i = 0; i < prdlist.Count(); i++)
{
ws.Cells[i + 1, 0].PutValue(prdlist[i].DepartmentName);
ws.Cells[i + 1, 1].PutValue(prdlist[i].TaskName);
ws.Cells[i + 1, 2].PutValue(prdlist[i].Member);
ws.Cells[i + 1, 3].PutValue(Convert.ToDateTime(prdlist[i].BeginTime).ToString("yyyy/MM/dd"));
ws.Cells[i + 1, 4].PutValue(Convert.ToDateTime(prdlist[i].EndTime).ToString("yyyy/MM/dd"));
ws.Cells[i + 1, 5].PutValue(prdlist[i].Number);
ws.Cells[i + 1, 6].PutValue(prdlist[i].TotalNum);
ws.Cells[i + 1, 7].PutValue(prdlist[i].WeeklyNum);
ws.Cells[i + 1, 8].PutValue(prdlist[i].Progress);

}
//设置所有列为自适应列宽
ws.AutoFitColumns();

//设置文件打开和保存路径
//
// string downfile = "Resource/Excel/" + ProjectReportName + ".xls";
string downfile = "Resource/Excel/项目周报.xls";
string filePath = System.IO.Path.Combine(Server.MapPath("/"), downfile);
if (System.IO.File.Exists(filePath))
System.IO.File.Delete(filePath);
FileStream fs = System.IO.File.Create(filePath);
fs.Close();
wb.Save(filePath);
return downfile;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: