使用Aspose.Cells实现简单的Excle导出

            //创建新的EXCELSheet
            Workbook BuildReport_WorkBook = new Workbook();
            Worksheet BuildReport_WorkSheet = BuildReport_WorkBook.Worksheets[0];

            Cells BuildReportCells = BuildReport_WorkSheet.Cells;

            BuildReportCells[0, 0].PutValue("用户类型");
            BuildReportCells[0, 1].PutValue("用户姓名");
            BuildReportCells[0, 2].PutValue("单位电话");
            BuildReportCells[0, 3].PutValue("单位地址");

            //读取数据,导入到Cells
            for (int i = 0; i < ListSystemUserInfo.Count; i++)
            {
                BuildReportCells[1 + i, 0].PutValue(UserType.Find(int.Parse(ListSystemUserInfo[i].UserType.ToString())).DispString.ToString());
                BuildReportCells[1 + i, 1].PutValue(ListSystemUserInfo[i].UserName.ToString());
                BuildReportCells[1 + i, 2].PutValue(ListSystemUserInfo[i].Phone.ToString());
                BuildReportCells[1 + i, 3].PutValue(ListSystemUserInfo[i].Address.ToString());
            }

            BuildReport_WorkSheet.Name = "通讯录";
            BuildReport_WorkSheet.AutoFitColumns();
            string SaveFileName = Server.MapPath("~/AttachInfo/" + BuildReport_WorkSheet.Name + ".xls");

            //保存Excel
            BuildReport_WorkBook.Save(SaveFileName, FileFormatType.Excel2003);//输出

            FileInfo file = new FileInfo(SaveFileName);
            FileStream fileStream = file.OpenRead();
            byte[] Buffer = new byte[(int)fileStream.Length];
            fileStream.Read(Buffer, 0, (int)fileStream.Length);
            fileStream.Close();

            Response.Clear();
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(DateTime.Now.ToString("yyyyMMdd") + ".xls"));
            Response.ContentType = "application/octet-stream";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.BinaryWrite(Buffer);
            Response.Flush();

            file.Delete();

            Response.Clear();
            Response.End();

posted @ 2011-06-17 17:06  WCJ  阅读(559)  评论(0编辑  收藏  举报