示例#1
0
        /// <summary>
        /// CsvContentsの情報をCSVとしてテキストファイルに出力します
        /// </summary>
        /// <param name="outputPath"> 出力先テキストのPath </param>
        /// <param name="contents"> CsvContents </param>
        /// <example></example>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <exception cref="FormatException"></exception>
        /// <exception cref="NotSupportedException"></exception>
        /// <exception cref="UnauthorizedAccessException"></exception>
        /// <exception cref="DirectoryNotFoundException"></exception>
        /// <exception cref="PathTooLongException"></exception>
        /// <exception cref="ObjectDisposedException"></exception>
        /// <exception cref="IOException"></exception>
        /// <exception cref="RegexMatchTimeoutException"></exception>
        /// <exception cref="SecurityException"></exception>
        public void Write(string outputPath, CsvContents contents)
        {
            if (contents.Contents.Count == 0)
            {
                return;
            }

            string path = DirectoryOperation(outputPath);

            using (var sw = new StreamWriter(path, false, CodePage))
            {
                foreach (string str in GetCsvString(contents))
                {
                    sw.WriteLine(str);
                }
            }
        }
示例#2
0
        /// <summary>
        /// オブジェクトから文字列としてのCSVデータを取得します
        /// </summary>
        /// <param name="contents"> CsvContents </param>
        /// <returns> CSVデータ(Iterator) </returns>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <exception cref="RegexMatchTimeoutException"></exception>
        public IEnumerable <string> GetCsvString(CsvContents contents)
        {
            // ヘッダーを出力する場合
            if (HasHeader)
            {
                string header = string.Empty;

                for (int i = 0; i < contents.Headers.Count; i++)
                {
                    header += contents.Headers[i] + Delimiter.ToString();
                }

                yield return(header.Remove(header.Length - 1, 1));
            }

            // Contentsから行ごとのデータを返す
            foreach (var ctn in contents.Contents)
            {
                yield return(GetLineData(ctn));
            }
        }