Exemplo n.º 1
0
        public bool Export(List <ContactJson> contacts)
        {
            try
            {
                var dir_path = GetDirectory();

                using (ExcelExport eX = new ExcelExport())
                {
                    eX.AddSheet(export_excel_sheet_name, contacts.Select(q => new
                    {
                        Vid                 = q.Vid,
                        Firstname           = q.Properties.firstname.value,
                        Lastname            = q.Properties.lastname.value,
                        Lastmodifieddate    = q.Properties.lastmodifieddate.value,
                        Associatedcompanyid = q.Properties.associatedcompanyid.value,
                        Name                = q.Associated_company.Properties.name.value,
                        City                = q.Associated_company.Properties.city.value,
                        Website             = q.Associated_company.Properties.website.value,
                        Phone               = q.Associated_company.Properties.phone.value,
                        State               = q.Associated_company.Properties.state.value,
                        Zip                 = q.Associated_company.Properties.zip.value
                    }));

                    eX.ExportTo(Path.Combine(dir_path, string.Format("{0} {1}.{2}", DateTime.Now.Ticks, export_excel_sheet_name, export_excel_file_format)));

                    return(true);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Couldn't export data to Excel file. See inner excpetion."), ex);
            }
        }
Exemplo n.º 2
0
        public void ToExcelfile(object fileOut, DataTable datos)
        {
            if (File.Exists(fileOut.ToString()))
            {
                File.Delete(fileOut.ToString());
            }
            ExcelExport excel = new ExcelExport();

            excel.AddSheet(datos);
            excel.ExportTo(fileOut.ToString());
        }
Exemplo n.º 3
0
 ///<summary>Adds a collection of strongly-typed objects to be exported.</summary>
 ///<param sheetName="sheetName">The sheetName of the sheet to generate.</param>
 ///<param sheetName="items">The rows to export to the sheet.</param>
 ///<returns>This instance, to allow chaining.kds</returns>
 public ExcelExport AddSheet <TRow>(string sheetName, IEnumerable <TRow> items)
 {
     exporter.AddSheet(sheetName, items);
     return(this);
 }
Exemplo n.º 4
0
        /// <summary> 读取数据源 保存为可转换的临时文件
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        private string SaveTempFile(string filename)
        {
            if (!File.Exists(filename))
            {
                return(null);
            }

            try
            {
                using (var stream = File.Open(filename, FileMode.Open, FileAccess.Read))
                {
                    string fileType = System.IO.Path.GetExtension(filename);

                    //1. Reading from a binary Excel file ('97-2003 format; *.xls)
                    IExcelDataReader excelReader = null;

                    if (Equals(fileType, ".xls"))
                    {
                        excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
                    }
                    else //2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
                    {
                        excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                    }

                    var dataset = excelReader.AsDataSet();

                    var excelexport = new ExcelExport();

                    if (dataset.Tables.Count == 0)
                    {
                        return(null);
                    }

                    var table = dataset.Tables[0];

                    var tempTable = new DataTable(table.TableName);

                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        if (i == 0 || i == 2) //名称描述
                        {
                            continue;
                        }

                        if (_version == 2 && i == 2)//类型描述
                        {
                            continue;
                        }

                        try
                        {
                            if (i == 1)
                            {
                                for (int j = 0; j < table.Rows[1].ItemArray.Count(); j++)
                                {
                                    tempTable.Columns.Add(table.Rows[1].ItemArray[j].ToString());
                                }
                            }
                            else
                            {
                                var newrow = tempTable.NewRow();
                                newrow.ItemArray = table.Rows[i].ItemArray;

                                tempTable.Rows.Add(newrow);
                            }
                        }
                        catch (Exception exception)
                        {
                            if (Logger.IsErrorEnabled)
                            {
                                Logger.Error(string.Format("{0} - {1}", exception.Message, table.Rows[i]));
                            }
                        }
                    }

                    excelexport.AddSheet(tempTable);

                    var fileName = System.IO.Path.GetFileName(filename);

                    var tempFileName = string.Format("{0}\\{1}-{2}.xls", _tempDir, fileName.Substring(0, fileName.IndexOf('.')), DateTime.Now.Ticks);
                    excelexport.ExportTo(tempFileName);
                    return(tempFileName);
                }
            }
            catch (Exception exception)
            {
                if (Logger.IsErrorEnabled)
                {
                    Logger.Error(filename, exception);
                }
            }

            return(null);
        }