Exemple #1
0
        /// <summary>
        /// Export data into CSV format
        /// </summary>
        public CFile Export(FileSystem fs, ExportRow.ExportRowList rows)
        {
            int          i;
            MemoryStream csvstream = new MemoryStream();
            StreamWriter csvwriter = new StreamWriter(csvstream);

            //Write the data out in CSV style rows
            foreach (ExportRow row in rows)
            {
                for (i = 0; i < row.Fields.Count; i++)
                {
                    csvwriter.Write(row.Fields[i]);
                    if (i < row.Fields.Count - 1)
                    {
                        csvwriter.Write(",");
                    }
                }
                csvwriter.WriteLine();
            }
            csvwriter.Flush();

            //Commit to a temp file within the FS
            //Create a temp file
            Guid  guid    = Guid.NewGuid();
            CFile expfile = fs.CreateFile(@"c:\system\export\" + guid.ToString(), false,
                                          new CFilePermission.FilePermissionList());

            expfile.Name = expfile.ID + ".csv";
            fs.UpdateFileInfo(expfile, false);

            //Commit the data
            csvstream.Seek(0, SeekOrigin.Begin);
            expfile.RawData = Globals.ReadStream(csvstream, (int)csvstream.Length);
            fs.Edit(expfile);
            fs.Save(expfile);

            csvwriter.Close();

            return(expfile);
        }
        private void cmdExport_Click(object sender, System.EventArgs e)
        {
            CFile export;
            ExportRow.ExportRowList rows = new ExportRow.ExportRowList();

            int courseID = GetCourseID();
            Users userda = new Users(Globals.CurrentIdentity);
            User.UserList users = GetUserSet(GetSectionID(), GetCourseID());

            //Add rows
            rows.Add(userda.GetCourseExportHeading(courseID));
            foreach (User user in users)
                rows.Add(userda.GetCourseExport(user.UserName, courseID));

            //Do export
            try {
                IExporter exporter =
                    (IExporter) ExporterFactory.GetInstance().CreateItem(ddlExportFormat.SelectedItem.Value);
                export = exporter.Export(new FileSystem(Globals.CurrentIdentity), rows);
            } catch (CustomException er) {
                PageExportError(er.Message);
                return;
            }

            //Setup d/l link
            lnkExport.Attributes.Clear();
            lnkExport.Attributes.Add("onClick",
                @"window.open('Controls/Filesys/dlfile.aspx?FileID=" + export.ID +
                @"', '"+export.ID+@"', 'width=770, height=580')");
            lnkExport.Visible = true;
        }