利用Aspose.Cells 組件導出數據到excel

利用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;
        }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章