Ejemplo n.º 1
0
        public static bool SaveItemListViewToCsv(
            ExchangeService oExchangeService,
            ListView oListView,
            string sFilePath,
            List <AdditionalPropertyDefinition> oAdditionalPropertyDefinitions,
            List <ExtendedPropertyDefinition> oExtendedPropertyDefinitions,
            CsvExportOptions oCsvExportOptions
            )
        {
            bool bRet = false;

            string sHeader = string.Empty;
            string sLine   = string.Empty;

            int iFolderPathColumn = 14;  // Folder Path

            PropertySet oExtendedPropSet = new PropertySet(BasePropertySet.IdOnly);

            if (oExtendedPropertyDefinitions != null)
            {
                foreach (ExtendedPropertyDefinition oEPD in oExtendedPropertyDefinitions)
                {
                    oExtendedPropSet.Add(oEPD);
                }
            }

            StreamWriter w = File.AppendText(sFilePath);

            char[]        TrimChars    = { ',', ' ' };
            StringBuilder SbHeader     = new StringBuilder();
            int           iHeaderCount = 0;

            // Build header part for listview
            foreach (ColumnHeader oCH in oListView.Columns)
            {
                iHeaderCount++;
                // Exclusions
                if (oCsvExportOptions._CsvExportGridExclusions != Exports.CsvExportGridExclusions.ExportAll)
                {
                    if (oCsvExportOptions._CsvExportGridExclusions == Exports.CsvExportGridExclusions.ExcludeAllInGridExceptFilePath)
                    {
                        if (iFolderPathColumn == iHeaderCount)
                        {
                            SbHeader.Append(oCH.Text);
                            SbHeader.Append(",");
                        }
                    }
                }
                else
                {
                    SbHeader.Append(oCH.Text);
                    SbHeader.Append(",");
                }
            }
            sHeader = SbHeader.ToString();
            sHeader = sHeader.TrimEnd(TrimChars);
            // Add headers for custom properties.
            if (oAdditionalPropertyDefinitions != null)
            {
                sHeader += "," + AdditionalProperties.GetExtendedPropertyHeadersAsCsvContent(oAdditionalPropertyDefinitions);
                sHeader  = sHeader.TrimEnd(TrimChars);
            }

            w.WriteLine(sHeader);


            ItemId oItemId        = null;
            string sExtendedValue = string.Empty;

            int iCount = 0;



            string s = string.Empty;

            foreach (ListViewItem oListViewItem in oListView.SelectedItems)
            {
                iCount++;

                StringBuilder SbLine = new StringBuilder();

                ItemTag oCalendarItemTag = (ItemTag)oListViewItem.Tag;
                oItemId = oCalendarItemTag.Id;
                byte[] oFromBytes;


                if (oListViewItem.Selected == true)
                {
                    int iColumnCount = 0;
                    foreach (ListViewItem.ListViewSubItem o in oListViewItem.SubItems)
                    {
                        iColumnCount++;

                        s = (o.Text);

                        // clean or encode strings to prevent issus with usage in a CSV? ----------
                        if (oCsvExportOptions._CsvStringHandling != CsvStringHandling.None)
                        {
                            s = AdditionalProperties.DoStringHandling(s, oCsvExportOptions._CsvStringHandling);
                        }


                        if (oCsvExportOptions.HexEncodeBinaryData == true)
                        {
                            if (oListViewItem.Tag != null)
                            {
                                if (oListViewItem.Tag.ToString() == "Binary")
                                {
                                    oFromBytes = System.Convert.FromBase64String(s); // Base64 to byte array.
                                    s          = StringHelper.HexStringFromByteArray(oFromBytes, false);
                                }
                            }
                        }

                        //// If its base64 encoded then convert it to hex encoded.
                        //if (oCsvExportOptions.HexEncodeBinaryData == true)
                        //{
                        //    //bColumnIsByteArray = StringHelper.IsBase64Encoded(s);
                        //    if (StringHelper.IsBase64Encoded(s) == true)
                        //    {
                        //        oFromBytes = System.Convert.FromBase64String(s); // Base64 to byte array.
                        //        s = StringHelper.HexStringFromByteArray(oFromBytes, false);
                        //    }
                        //}

                        // Exclusions --------------------------------------------
                        if (oCsvExportOptions._CsvExportGridExclusions != Exports.CsvExportGridExclusions.ExportAll)
                        {
                            if (oCsvExportOptions._CsvExportGridExclusions == Exports.CsvExportGridExclusions.ExcludeAllInGridExceptFilePath)
                            {
                                if (iFolderPathColumn == iColumnCount)
                                {
                                    SbLine.Append(s);
                                    SbLine.Append(",");
                                }
                            }
                        }
                        else
                        {
                            SbLine.Append(s);
                            SbLine.Append(",");
                        }
                    }
                }

                sLine = SbLine.ToString();
                sLine = sLine.TrimEnd(TrimChars);

                //  Add Additional Properties ----------------------------------------------


                StringBuilder oStringBuilder = new StringBuilder();

                if (oExtendedPropertyDefinitions != null)
                {
                    string sExt = AdditionalProperties.GetExtendedPropertiesForItemAsCsvContent(
                        oExchangeService,
                        oItemId,
                        oExtendedPropertyDefinitions,
                        oCsvExportOptions
                        );

                    sLine += "," + sExt;

                    //sLine = SbLine.ToString();
                    sLine = sLine.TrimEnd(TrimChars);
                }

                w.WriteLine(sLine);

                bRet = true;
            }

            w.Close();

            return(bRet);
        }