參考:// Create a new file. using (FileStream fs = new FileStream("test.txt", FileMode.Create)) { // Create a writer and specify the encoding. // The default (UTF-8) supports special Unicode characters, // but encodes all standard characters in the same way as // ASCII encoding. using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8)) { // Write a decimal, string, and char. w.WriteLine(124.23M); w.WriteLine("Test string"); w.WriteLine('!'); } } Console.WriteLine("Press Enter to read the information."); Console.ReadLine(); // Open the file in read-only mode. using (FileStream fs = new FileStream("test.txt", FileMode.Open)) { using (StreamReader r = new StreamReader(fs, Encoding.UTF8)) { // Read the data and convert it to the appropriate data type. Console.WriteLine(Decimal.Parse(r.ReadLine())); Console.WriteLine(r.ReadLine()); Console.WriteLine(Char.Parse(r.ReadLine())); } }
System.IO.StreamReader sr = new System.IO.StreamReader(FilePath, System.Text.Encoding.Default); while (sr.Peek() >= 0) { string str = sr.ReadLine(); Console.WriteLine(str); if (str.Contains("─")) continue; string[] ss = str.Split('|'); List<string> datalist = new List<string>(); for (int i = 0; i < ss.Length; i++) { if (ss[i].Trim().Length == 0) continue; datalist.Add(ss[i]); } foreach (string s in datalist) Console.WriteLine(s); if (datalist.Count == 1) dgvResult.Rows[dgvResult.Rows.Count - 1].Cells[1].Value += datalist[0]; if (datalist.Count == 2) dgvResult.Rows.Add(datalist[0], datalist[1]); if (datalist.Count == 4) { dgvResult.Rows.Add(datalist[0], datalist[1]); dgvResult.Rows.Add(datalist[2], datalist[3]); } 如果需要源程序,就发邮件给我吧,[email protected]
这是谁写进txt?读就要谁去写读代码.哈哈
string tempString = string.Empty; string All = string.Empty; using (StreamReader sr = new StreamReader(filepath,System.Text.Encoding.Default)) { while (sr.Peek() >= 0) { All = tempString = sr.ReadLine(); if (tempString == "【基本资料】") { for (int i = 0; i < 50; i++) { tempString += sr.ReadLine().ToString(); }
this.txtResult.Text = Encode(tempString); } if (tempString == "【股本历次变更状况】") { for (int j = 0; j < 6; j++) { All += sr.ReadLine().ToString(); }
using (FileStream fs = new FileStream("test.txt", FileMode.Create))
{
// Create a writer and specify the encoding.
// The default (UTF-8) supports special Unicode characters,
// but encodes all standard characters in the same way as
// ASCII encoding.
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
// Write a decimal, string, and char.
w.WriteLine(124.23M);
w.WriteLine("Test string");
w.WriteLine('!');
}
}
Console.WriteLine("Press Enter to read the information.");
Console.ReadLine();
// Open the file in read-only mode.
using (FileStream fs = new FileStream("test.txt", FileMode.Open))
{
using (StreamReader r = new StreamReader(fs, Encoding.UTF8))
{
// Read the data and convert it to the appropriate data type.
Console.WriteLine(Decimal.Parse(r.ReadLine()));
Console.WriteLine(r.ReadLine());
Console.WriteLine(Char.Parse(r.ReadLine()));
}
}
System.IO.StreamReader sr = new System.IO.StreamReader(FilePath, System.Text.Encoding.Default); while (sr.Peek() >= 0)
{
string str = sr.ReadLine(); Console.WriteLine(str); if (str.Contains("─"))
continue; string[] ss = str.Split('|');
List<string> datalist = new List<string>();
for (int i = 0; i < ss.Length; i++)
{
if (ss[i].Trim().Length == 0)
continue; datalist.Add(ss[i]);
} foreach (string s in datalist)
Console.WriteLine(s); if (datalist.Count == 1)
dgvResult.Rows[dgvResult.Rows.Count - 1].Cells[1].Value += datalist[0]; if (datalist.Count == 2)
dgvResult.Rows.Add(datalist[0], datalist[1]); if (datalist.Count == 4)
{
dgvResult.Rows.Add(datalist[0], datalist[1]);
dgvResult.Rows.Add(datalist[2], datalist[3]);
}
如果需要源程序,就发邮件给我吧,[email protected]
string tempString = string.Empty;
string All = string.Empty;
using (StreamReader sr = new StreamReader(filepath,System.Text.Encoding.Default))
{
while (sr.Peek() >= 0)
{
All = tempString = sr.ReadLine();
if (tempString == "【基本资料】")
{
for (int i = 0; i < 50; i++)
{
tempString += sr.ReadLine().ToString();
}
this.txtResult.Text = Encode(tempString);
}
if (tempString == "【股本历次变更状况】")
{
for (int j = 0; j < 6; j++)
{
All += sr.ReadLine().ToString();
}
this.txtResult.Text += Encode(All);
}
}
}
public string Encode(string str)
{
if (str.Length > 0)
{
str = str.Replace("|", "");
str = str.Replace("─", "");
str = str.Replace("┤", "");
str = str.Replace("├", "");
str = str.Replace("┼", "");
str = str.Replace("┴", "");
str = str.Replace("└", "");
str = str.Replace("┬", "");
str = str.Replace("┌", "");
str = str.Replace("┐", "");
return str;
}
else
{
return str;
}
}
先用回车行分隔开,放在一个数组然后循环输出stra[i]里面的数据,然后再把stra[i]里面的数据通过substring 或split提取出来因为没有规律,只能自己慢慢分析