Ejemplo n.º 1
0
        public static byte[] ExportDataRSS(string rss, ExportToFormat exportFormat,
                                           params KeyValuePair <string, object>[] additionalData)
        {
            var json = GetDataFromRSS(rss);

            return(ExportDataJson(json, exportFormat, additionalData));
        }
Ejemplo n.º 2
0
        public static byte[] ExportDataCsv(string[] csvWithHeader, ExportToFormat exportFormat,
                                           params KeyValuePair <string, object>[] additionalData)
        {
            var props = csvWithHeader[0].Split(new string[] { "," }, StringSplitOptions.None);

            if (props.Contains(""))
            {
                throw new ArgumentException("header contains empty string");
            }

            var type     = GenerateTypeFromProperties(props);
            var assembly = type.Assembly;

            //in order to be found from Razor export
            Assembly.LoadFile(assembly.Location);
            var listType = typeof(List <>).MakeGenericType(type);

            dynamic list = Activator.CreateInstance(listType);

            for (int i = 1; i < csvWithHeader.Length; i++)
            {
                var item       = csvWithHeader[i];
                var propsValue = item.Split(new string[] { "," }, StringSplitOptions.None).ToList();
                propsValue.Add("fake");
                dynamic obj = assembly.CreateInstance(type.FullName, true, BindingFlags.Public | BindingFlags.Instance,
                                                      null,
                                                      propsValue.ToArray(), null,
                                                      null);
                list.Add(obj);
            }
            return(ExportDataWithType(list as IEnumerable, exportFormat, type, additionalData));
        }
Ejemplo n.º 3
0
        public static byte[] ExportOpmlRSS(string dataOPML, ExportToFormat exportFormat,
                                           params KeyValuePair <string, object>[] additionalData)
        {
            if (exportFormat != ExportToFormat.Excel2007)
            {
                throw new ArgumentException("ready just for Excel 2007");
            }

            XDocument rssDoc;

            if (Uri.IsWellFormedUriString(dataOPML, UriKind.Absolute))
            {
                rssDoc = XDocument.Load(dataOPML);
            }
            else
            {
                rssDoc = XDocument.Parse(dataOPML);
            }

            var dataNodes = rssDoc.XPathSelectElements("//outline[@type='rss']").ToArray();
            var count     = dataNodes.Length;
            var listData  = new List <IList>(count);

            for (int i = 0; i < count; i++)
            {
                var rss       = dataNodes[i].Attribute("xmlUrl").Value;
                var jsonArray = ExportFactory.GetDataFromRSS(rss);
                listData.Add(FromJson(jsonArray));
            }
            var export = new ExportExcel2007 <Tuple <string> >();
            var data   = export.ExportMultipleSheets(listData.ToArray(), additionalData);

            return(data);
        }
Ejemplo n.º 4
0
        public static string GetFileExtension(ExportToFormat e)
        {
            switch (e)
            {
            case ExportToFormat.Word2003XML:
                return(".doc");

            case ExportToFormat.Excel2003XML:
                return(".xls");

            case ExportToFormat.CSV:
                return(".csv");

            case ExportToFormat.HTML:
                return(".html");

            case ExportToFormat.XML:
                return(".xml");

            case ExportToFormat.itextSharpXML:
                return(".xml");

            case ExportToFormat.PDFtextSharpXML:
                return(".pdf");

            case ExportToFormat.Word2007:
                return(".docx");

            case ExportToFormat.Excel2007:
                return(".xlsx");

            default:
                throw new ArgumentException("not found : " + (object)e);
            }
        }
Ejemplo n.º 5
0
        private void exportTables(DatatableEx root, ExportToFormat format)
        {
            TraceLog.Information("Exporting tables to {format}", format.ToString());

            var fileExtension = ".xlsx";

            switch (format)
            {
            case ExportToFormat.Word2007:
                fileExtension = ".docx";
                break;

            case ExportToFormat.HTML:
                fileExtension = ".html";
                break;

            case ExportToFormat.PDFtextSharpXML:
                fileExtension = ".pdf";
                break;
            }
            var fileName = Path.Combine(ExportPath, root.Root.Table.TableName + fileExtension);

            root.Root.Table.ExportTo(format, fileName);
            foreach (var t in root.Children)
            {
                exportTables(t, format);
            }
        }
Ejemplo n.º 6
0
        public static byte[] ExportDataJson(string jsonArray, ExportToFormat exportFormat,
                                            params KeyValuePair <string, object>[] additionalData)
        {
            var list = FromJson(jsonArray);
            var type = list[0].GetType();

            return(ExportDataWithType(list, exportFormat, type, additionalData));
        }
Ejemplo n.º 7
0
        public static byte[] ExportDataFromDataTable(DataTable data, ExportToFormat exportFormat,
                                                     params KeyValuePair <string, object>[] additionalData)
        {
            var list = IEnumerableFromDataTable(data);
            var type = list[0].GetType();

            return(ExportDataWithType(list as IEnumerable, exportFormat, type, additionalData));
        }
Ejemplo n.º 8
0
        public static byte[] ExportDataIDataReader(IDataReader dr, ExportToFormat exportFormat,
                                                   params KeyValuePair <string, object>[] additionalData)
        {
            var dt = new DataTable();

            dt.Load(dr);
            return(ExportDataFromDataTable(dt, exportFormat, additionalData));
        }
Ejemplo n.º 9
0
        public static byte[] ExportDataSet(DataSet ds, ExportToFormat exportFormat,
                                           params KeyValuePair <string, object>[] additionalData)
        {
            if (exportFormat != ExportToFormat.Excel2007)
            {
                throw new ArgumentException("ready just for Excel 2007");
            }

            var export = new ExportExcel2007 <Tuple <string> >();

            return(export.ExportMultipleSheets(ds));
        }
Ejemplo n.º 10
0
        public static byte[] ExportDataWithType(IEnumerable data, ExportToFormat exportFormat, Type type,
                                                params KeyValuePair <string, object>[] additionalData)

        {
            var exportType = typeof(Export <>).MakeGenericType(type);

            switch (exportFormat)
            {
            case ExportToFormat.Word2003XML:

                exportType = typeof(ExportWord2003 <>).MakeGenericType(type);
                break;

            case ExportToFormat.Excel2003XML:
                exportType = typeof(ExportExcel2003 <>).MakeGenericType(type);
                break;

            case ExportToFormat.HTML:
                exportType = typeof(ExportHtml <>).MakeGenericType(type);
                break;

            case ExportToFormat.PDFiTextSharp4:
                exportType = typeof(ExportPdfiTextSharp4 <>).MakeGenericType(type);
                break;

            case ExportToFormat.Word2007:
                exportType = typeof(ExportWord2007 <>).MakeGenericType(type);
                break;

            case ExportToFormat.Excel2007:
                exportType = typeof(ExportExcel2007 <>).MakeGenericType(type);
                break;

            case ExportToFormat.ODT:
                exportType = typeof(ExportODT <>).MakeGenericType(type);
                break;

            case ExportToFormat.ODS:
                exportType = typeof(ExportODS <>).MakeGenericType(type);
                break;

            default:
                //throw new ArgumentOutOfRangeException(nameof(exportFormat), exportFormat, null);
                throw new ArgumentOutOfRangeException("exportFormat", exportFormat, null);
            }

            dynamic export = Activator.CreateInstance(exportType);
            dynamic data1  = data;

            return(export.ExportResult(data1, additionalData));
        }
Ejemplo n.º 11
0
 public IHttpActionResult ExportFromJSON([FromUri] ExportToFormat id, [FromBody] ExportData dataJson)
 {
     try
     {
         var bytes = ExportFactory.ExportDataJson(dataJson.data.Trim(), id);
         var key   = Guid.NewGuid().ToString();
         MemoryCache.Default.Add(key, bytes, DateTimeOffset.Now.AddMinutes(10));
         return(Ok(key));
     }
     catch (Exception ex)
     {
         return(BadRequest(ex.Message + "=>" + ex.StackTrace));
     }
 }
Ejemplo n.º 12
0
        public static byte[] ExportData <T>(List <T> data, ExportToFormat exportFormat,
                                            params KeyValuePair <string, object>[] additionalData)
            where T : class
        {
            Export <T> export;

            switch (exportFormat)
            {
            case ExportToFormat.Word2003XML:
                export = new ExportWord2003 <T>();
                break;

            case ExportToFormat.Excel2003XML:
                export = new ExportExcel2003 <T>();
                break;

            case ExportToFormat.HTML:
                export = new ExportHtml <T>();
                break;

            case ExportToFormat.PDFiTextSharp4:
                export = new ExportPdfiTextSharp4 <T>();
                break;

            case ExportToFormat.Word2007:
                export = new ExportWord2007 <T>();
                break;

            case ExportToFormat.Excel2007:
                export = new ExportExcel2007 <T>();
                break;

            case ExportToFormat.ODT:
                export = new ExportODT <T>();
                break;

            case ExportToFormat.ODS:
                export = new ExportODS <T>();
                break;

            default:
                //throw new ArgumentOutOfRangeException(nameof(exportFormat), exportFormat, null);
                throw new ArgumentOutOfRangeException("exportFormat", exportFormat, null);
            }
            return(export.ExportResult(data, additionalData));
        }
Ejemplo n.º 13
0
        public IHttpActionResult ExportFromCSV(
            ExportToFormat id, [FromBody] ExportData dataCSV)
        {
            try
            {
                var dataArray
                    = dataCSV.data.Trim()
                      .Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

                if (dataArray.Length == 1)
                {
                    dataArray = dataCSV.data.Trim().Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
                }

                var bytes = ExportFactory.ExportDataCsv(dataArray, id);
                var key   = Guid.NewGuid().ToString();
                MemoryCache.Default.Add(key, bytes, DateTimeOffset.Now.AddMinutes(10));
                return(Ok(key));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message + "=>" + ex.StackTrace));
            }
        }
Ejemplo n.º 14
0
        public IHttpActionResult ExportFromCSV(
            ExportToFormat id, [FromBody]ExportData dataCSV)
        {
            try
            {
                var dataArray
                    = dataCSV.data.Trim()
                        .Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);

                if (dataArray.Length == 1)
                {
                    dataArray=dataCSV.data.Trim().Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
                }

                var bytes = ExportFactory.ExportDataCsv(dataArray, id);
                var key = Guid.NewGuid().ToString();
                MemoryCache.Default.Add(key, bytes, DateTimeOffset.Now.AddMinutes(10));
                return Ok(key);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.StackTrace);
            }
        }
Ejemplo n.º 15
0
        public static byte[] ExportDataDictionary(Dictionary <string, object>[] values, ExportToFormat exportFormat,
                                                  params KeyValuePair <string, object>[] additionalData)
        {
            var props    = values[0].Keys.ToArray();
            var type     = GenerateTypeFromProperties(props);
            var assembly = type.Assembly;

            //in order to be found from Razor export
            Assembly.LoadFile(assembly.Location);
            var listType = typeof(List <>).MakeGenericType(type);

            dynamic list = Activator.CreateInstance(listType);

            for (int i = 0; i < values.Length; i++)
            {
                var item       = values[i];
                var propsValue = item.Values.ToList();
                propsValue.Add("fake");
                dynamic obj = assembly.CreateInstance(type.FullName, true, BindingFlags.Public | BindingFlags.Instance,
                                                      null,
                                                      propsValue.ToArray(), null,
                                                      null);
                list.Add(obj);
            }
            return(ExportDataWithType(list as IEnumerable, exportFormat, type, additionalData));
        }
Ejemplo n.º 16
0
        protected void ExportTo(IEnumerable col, Type type, ExportToFormat exp, string fileName)
        {
            var str1 = TemplateFileName;

            if (string.IsNullOrWhiteSpace(str1))
            {
                str1 = type.Name + "_" + exp + ".cshtml";
            }
            if (!File.Exists(Path.Combine(PathTemplateFolder, str1)))
            {
                switch (exp)
                {
                case ExportToFormat.Word2003XML:
                    WriteWord2003TemplateForIEnumerable(str1, type);
                    break;

                case ExportToFormat.Excel2003XML:
                    WriteExcel2003TemplateForIEnumerable(str1, type);
                    break;

                case ExportToFormat.CSV:
                    WriteCSVTemplateForIEnumerable(str1, type);
                    break;

                case ExportToFormat.HTML:
                    WriteHtmlTemplateForIEnumerable(str1, type);
                    break;

                case ExportToFormat.XML:
                    WriteXMLTemplateForIEnumerable(str1, type);
                    break;

                case ExportToFormat.itextSharpXML:
                case ExportToFormat.PDFtextSharpXML:
                    WriteitextSharpXMLTemplateForIEnumerable(str1, type);
                    break;

                case ExportToFormat.Word2007:
                    WriteWord2007TemplateForIEnumerable(str1, type);
                    break;

                case ExportToFormat.Excel2007:
                    WriteExcel2007TemplateForIEnumerable(str1, type);
                    break;

                default:
                    throw new NotImplementedException();
                }
            }

            var list = CreateInstance(type);

            foreach (var obj in col)
            {
                list.Add(obj);
            }

            var folderHostContainer = CreateRazorFolderHostContainer(type, PathTemplateFolder);
            var str2 = folderHostContainer.RenderTemplate(str1, list);

            if (!string.IsNullOrWhiteSpace(folderHostContainer.ErrorMessage))
            {
                throw new Exception(folderHostContainer.ErrorMessage);
            }

            switch (exp)
            {
            case ExportToFormat.PDFtextSharpXML:
                CreateFilePDF(str2, fileName);
                break;

            case ExportToFormat.Word2007:
                CreateWord2007(str2, fileName);
                break;

            case ExportToFormat.Excel2007:
                CreateExcel2007(str2, fileName);
                break;

            default:
                File.WriteAllText(fileName, str2);
                break;
            }
        }
 public void ExportTo(IEnumerable <T> col, ExportToFormat exp, string fileName)
 {
     ExportTo(col, typeof(T), exp, fileName);
 }
Ejemplo n.º 18
0
        public void ExportTo(DataTable dt, ExportToFormat exp, string fileName)
        {
            if (string.IsNullOrEmpty(dt.TableName))
            {
                throw new ArgumentException("Please provide a table name for datatable ");
            }

            var tableName = dt.TableName;
            var path1     = Path.GetDirectoryName(fileName);

            if (string.IsNullOrEmpty(path1))
            {
                path1 = Environment.CurrentDirectory;
            }

            var  path = Path.Combine(path1, tableName + ".dll");
            Type type;

            if (!File.Exists(path))
            {
                var provider = CodeDomProvider.CreateProvider("CSharp");
                var options  = new CompilerParameters();
                options.ReferencedAssemblies.Add("System.dll");
                options.ReferencedAssemblies.Add("System.Data.dll");
                options.ReferencedAssemblies.Add("System.XML.dll");
                options.GenerateExecutable = false;
                options.GenerateInMemory   = false;
                options.OutputAssembly     = path;

                var stringBuilder = new StringBuilder();
                stringBuilder.Append("using System;");
                stringBuilder.Append("using System.Data;");
                stringBuilder.Append("using System.Collections.Generic;");
                stringBuilder.AppendFormat("namespace Export{0} {{", tableName);
                stringBuilder.AppendFormat("public class {0} {{", tableName);
                foreach (var obj in dt.Columns)
                {
                    var dataColumn = obj as DataColumn;
                    stringBuilder.AppendFormat("public {0} {1} {{get;set;}}", dataColumn.DataType.FullName, dataColumn.ColumnName);
                }

                stringBuilder.AppendFormat("public static {0} From(DataRow dr) {{", tableName);
                stringBuilder.AppendFormat("{0} ret=new {0}();", tableName);
                foreach (var obj in dt.Columns)
                {
                    var dataColumn = obj as DataColumn;
                    stringBuilder.AppendFormat("ret.{0}=  (dr[{1}] == null || dr[{1}] == DBNull.Value)?default({2}):({2}) dr[{1}];", dataColumn.ColumnName, dataColumn.Ordinal, dataColumn.DataType.FullName);
                }

                stringBuilder.Append("return ret;");
                stringBuilder.Append("}");
                stringBuilder.AppendFormat("public static List<{0}> FromDataTable(DataTable dt) {{", tableName);
                stringBuilder.AppendFormat("List<{0}> ret=new List<{0}>();", tableName);
                stringBuilder.Append("foreach(var row in dt.Rows){");
                stringBuilder.Append("ret.Add(From(row as DataRow));");
                stringBuilder.Append("}");
                stringBuilder.Append("return ret;");
                stringBuilder.Append("}");
                stringBuilder.Append("}");
                stringBuilder.Append("}");

                var compilerResults = provider.CompileAssemblyFromSource(options, stringBuilder.ToString());
                if (compilerResults.Errors.Count > 0)
                {
                    throw new ArgumentException(compilerResults.Errors[0].ErrorText);
                }
                type = compilerResults.CompiledAssembly.GetType(string.Format("Export{0}.{0}", tableName), true, false);
            }
            else
            {
                type = Assembly.LoadFile(path).GetType(string.Format("Export{0}.{0}", tableName), true, false);
            }

            ExportTo(type.GetMethod("FromDataTable", BindingFlags.Static | BindingFlags.Public).Invoke(null, new object[] { dt }) as IEnumerable, type, exp, fileName);
        }