मैं दो वर्गों में मेरा उत्तर को अलग कर रहा हूँ: पहले एक कैसे एन्कोडिंग, हेडर के साथ, csv में कुछ सामान्य आइटम सूची निर्यात करने के लिए है - (यह केवल निर्दिष्ट हेडर के लिए csv डेटा का निर्माण होगा, और ध्यान नहीं देगा अनियंत्रित गुण)।
public string ExportCsv<T>(IEnumerable<T> items, Dictionary<string, string> headers)
{
string result;
using (TextWriter textWriter = new StreamWriter(myStream, myEncoding))
{
result = this.WriteDataAsCsvWriter<T>(items, textWriter, headers);
}
return result;
}
private string WriteDataAsCsvWriter<T>(IEnumerable<T> items, TextWriter textWriter, Dictionary<string, string> headers)
{
//Add null validation
////print the columns headers
StringBuilder sb = new StringBuilder();
//Headers
foreach (KeyValuePair<string, string> kvp in headers)
{
sb.Append(ToCsv(kvp.Value));
sb.Append(",");
}
sb.Remove(sb.Length - 1, 1);//the last ','
sb.Append(Environment.NewLine);
//the values
foreach (var item in items)
{
try
{
Dictionary<string, string> values = GetPropertiesValues(item, headers);
foreach (var value in values)
{
sb.Append(ToCsv(value.Value));
sb.Append(",");
}
sb.Remove(sb.Length - 1, 1);//the last ','
sb.Append(Environment.NewLine);
}
catch (Exception e1)
{
//do something
}
}
textWriter.Write(sb.ToString());
return sb.ToString();
}
//Help function that encode text to csv:
public static string ToCsv(string input)
{
if (input != null)
{
input = input.Replace("\r\n", string.Empty)
.Replace("\r", string.Empty)
.Replace("\n", string.Empty);
if (input.Contains("\""))
{
input = input.Replace("\"", "\"\"");
}
input = "\"" + input + "\"";
}
return input;
}
यह सबसे महत्वपूर्ण कार्य है, इसके गुण निकालने (लगभग) किसी भी सामान्य वर्ग के बाहर महत्व देता है। GetPropertiesValues
के लिए
private Dictionary<string, string> GetPropertiesValues(object item, Dictionary<string, string> headers)
{
Dictionary<string, string> values = new Dictionary<string, string>();
if (item == null)
{
return values;
}
//We need to make sure each value is coordinated with the headers, empty string
foreach (var key in headers.Keys)
{
values[key] = String.Empty;
}
Type t = item.GetType();
PropertyInfo[] propertiesInfo = t.GetProperties();
foreach (PropertyInfo propertiyInfo in propertiesInfo)
{
//it not complex: string, int, bool, Enum
if ((propertiyInfo.PropertyType.Module.ScopeName == "CommonLanguageRuntimeLibrary") || propertiyInfo.PropertyType.IsEnum)
{
if (headers.ContainsKey(propertiyInfo.Name))
{
var value = propertiyInfo.GetValue(item, null);
if (value != null)
{
values[propertiyInfo.Name] = value.ToString();
}
}
}
else//It's complex property
{
if (propertiyInfo.GetIndexParameters().Length == 0)
{
Dictionary<string, string> lst = GetPropertiesValues(propertiyInfo.GetValue(item, null), headers);
foreach (var value in lst)
{
if (!string.IsNullOrEmpty(value.Value))
{
values[value.Key] = value.Value;
}
}
}
}
}
return values;
}
उदाहरण:
public MyClass
{
public string Name {get; set;}
public MyEnum Type {get; set;}
public MyClass2 Child {get; set;}
}
public MyClass2
{
public int Age {get; set;}
public DateTime MyDate {get; set;}
}
MyClass myClass = new MyClass()
{
Name = "Bruce",
Type = MyEnum.Sometype,
Child = new MyClass2()
{
Age = 18,
MyDate = DateTime.Now()
}
};
Dictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Name", "CustomCaption_Name");
headers.Add("Type", "CustomCaption_Type");
headers.Add("Age", "CustomCaption_Age");
GetPropertiesValues(myClass, headers)); // OUTPUT: {{"Name","Bruce"},{"Type","Sometype"},{"Age","18"}}
तो .. बस इसे लिखने? यह एक क्रमबद्ध प्रक्रिया है, लेकिन एक्सएमएल या बाइनरी के बजाय यह सीएसवी है। आपकी क्या समस्या हैं? –
(ए) कक्षा के लिए एक उदाहरण दें, और (बी) आप जिस सीएसवी की अपेक्षा करते हैं उसके लिए एक उदाहरण दें, और आपको बहुत से जवाब मिलेगा कि कैसे (ए) से (बी) तक जाना है। –
रेशमी: मैं इसे लिख सकता हूं, लेकिन मुझे कुछ जेनेरिक चाहिए, क्योंकि जब मैं कक्षाओं में गुण जोड़ रहा हूं तो मुझे सीएसवी के बारे में चिंता करने की ज़रूरत नहीं है .. डॉक्टर: उपयोगकर्ता (नाम, उपनाम, आयु ...) जैसे सरल डेटा वर्ग । मैं कुछ ऐसा सोच रहा था जैसे ";" स्टार्टर्स के लिए हेडर समेत सीमित। सरल कक्षाओं के लिए – no9