Пример #1
0
        private void ExportDataAsCsv(ViewPage page, DbDataReader reader, StreamWriter writer)
        {
            bool firstField = true;

            for (int i = 0; (i < page.Fields.Count); i++)
            {
                DataField field = page.Fields[i];
                if (!(field.Hidden) && (field.Type != "DataView"))
                {
                    if (firstField)
                    {
                        firstField = false;
                    }
                    else
                    {
                        writer.Write(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator);
                    }
                    if (!(String.IsNullOrEmpty(field.AliasName)))
                    {
                        field = page.FindField(field.AliasName);
                    }
                    writer.Write("\"{0}\"", field.Label.Replace("\"", "\"\""));
                }
                field.NormalizeDataFormatString();
            }
            writer.WriteLine();
            while (reader.Read())
            {
                firstField = true;
                for (int j = 0; (j < page.Fields.Count); j++)
                {
                    DataField field = page.Fields[j];
                    if (!(field.Hidden) && (field.Type != "DataView"))
                    {
                        if (firstField)
                        {
                            firstField = false;
                        }
                        else
                        {
                            writer.Write(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator);
                        }
                        if (!(String.IsNullOrEmpty(field.AliasName)))
                        {
                            field = page.FindField(field.AliasName);
                        }
                        string text = String.Empty;
                        object v    = reader[field.Name];
                        if (!(DBNull.Value.Equals(v)))
                        {
                            if (!(String.IsNullOrEmpty(field.DataFormatString)))
                            {
                                text = String.Format(field.DataFormatString, v);
                            }
                            else
                            {
                                text = Convert.ToString(v);
                            }
                            writer.Write("\"{0}\"", text.Replace("\"", "\"\""));
                        }
                        else
                        {
                            writer.Write("\"\"");
                        }
                    }
                }
                writer.WriteLine();
            }
        }
Пример #2
0
        private void ExportDataAsRss(ViewPage page, DbDataReader reader, StreamWriter writer)
        {
            string            appPath  = Regex.Replace(HttpContext.Current.Request.Url.AbsoluteUri, "^(.+)Export.ashx.+$", "$1", RegexOptions.IgnoreCase);
            XmlWriterSettings settings = new XmlWriterSettings();

            settings.CloseOutput = false;
            XmlWriter output = XmlWriter.Create(writer, settings);

            output.WriteStartDocument();
            output.WriteStartElement("rss");
            output.WriteAttributeString("version", "2.0");
            output.WriteStartElement("channel");
            output.WriteElementString("title", ((string)(_view.Evaluate("string(concat(/c:dataController/@label, \' | \',  @label))", Resolver))));
            output.WriteElementString("lastBuildDate", DateTime.Now.ToString("r"));
            output.WriteElementString("language", System.Threading.Thread.CurrentThread.CurrentCulture.Name.ToLower());
            int rowCount = 0;

            while ((rowCount < MaximumRssItems) && reader.Read())
            {
                output.WriteStartElement("item");
                bool          hasTitle   = false;
                bool          hasPubDate = false;
                StringBuilder desc       = new StringBuilder();
                for (int i = 0; (i < page.Fields.Count); i++)
                {
                    DataField field = page.Fields[i];
                    if (!(field.Hidden) && field.Type != "DataView")
                    {
                        if (rowCount == 0)
                        {
                            field.NormalizeDataFormatString();
                        }
                        if (!(String.IsNullOrEmpty(field.AliasName)))
                        {
                            field = page.FindField(field.AliasName);
                        }
                        string text = String.Empty;
                        object v    = reader[field.Name];
                        if (!(DBNull.Value.Equals(v)))
                        {
                            if (!(String.IsNullOrEmpty(field.DataFormatString)))
                            {
                                text = String.Format(field.DataFormatString, v);
                            }
                            else
                            {
                                text = Convert.ToString(v);
                            }
                        }
                        if (!(hasPubDate) && (field.Type == "DateTime"))
                        {
                            hasPubDate = true;
                            if (!(String.IsNullOrEmpty(text)))
                            {
                                output.WriteElementString("pubDate", ((DateTime)(reader[field.Name])).ToString("r"));
                            }
                        }
                        if (!(hasTitle))
                        {
                            hasTitle = true;
                            output.WriteElementString("title", text);
                            StringBuilder link = new StringBuilder();
                            link.Append(_config.Evaluate("string(/c:dataController/@name)"));
                            foreach (DataField pkf in page.Fields)
                            {
                                if (pkf.IsPrimaryKey)
                                {
                                    link.Append(String.Format("&{0}={1}", pkf.Name, reader[pkf.Name]));
                                }
                            }
                            string itemGuid = String.Format("{0}Details.aspx?l={1}", appPath, HttpUtility.UrlEncode(Convert.ToBase64String(Encoding.Default.GetBytes(link.ToString()))));
                            output.WriteElementString("link", itemGuid);
                            output.WriteElementString("guid", itemGuid);
                        }
                        else
                        if (!(String.IsNullOrEmpty(field.OnDemandHandler)) && (field.OnDemandStyle == OnDemandDisplayStyle.Thumbnail))
                        {
                            if (text.Equals("1"))
                            {
                                desc.AppendFormat("{0}:<br /><img src=\"{1}Blob.ashx?{2}=t", HttpUtility.HtmlEncode(field.Label), appPath, field.OnDemandHandler);
                                foreach (DataField f in page.Fields)
                                {
                                    if (f.IsPrimaryKey)
                                    {
                                        desc.Append("|");
                                        desc.Append(reader[f.Name]);
                                    }
                                }
                                desc.Append("\" style=\"width:92px;height:71px;\"/><br />");
                            }
                        }
                        else
                        {
                            desc.AppendFormat("{0}: {1}<br />", HttpUtility.HtmlEncode(field.Label), HttpUtility.HtmlEncode(text));
                        }
                    }
                }
                output.WriteStartElement("description");
                output.WriteCData(String.Format("<span style=\\\"font-size:small;\\\">{0}</span>", desc.ToString()));
                output.WriteEndElement();
                output.WriteEndElement();
                rowCount++;
            }
            output.WriteEndElement();
            output.WriteEndElement();
            output.WriteEndDocument();
            output.Close();
        }