1、創建自定義類文件
StringList.cs
內容:
class StringList
{
private int m_Capacity;
private string[] m_Strings;
private int m_Size;
/// <summary>
/// 數據個數屬性
/// </summary>
public int Count
{
get
{
return m_Size;
}
}
/// <summary>
///內存大小屬性
/// </summary>
public int Capacity
{
get
{
return m_Capacity;
}
set
{
if (m_Strings == null)
{
return;
}
if (value != m_Strings.Length)
{
if (value < this.m_Size)
{
throw new ArgumentOutOfRangeException();
}
if (value > 0)
{
string[] objArray1 = new string[value];
if (this.m_Size > 0)
{
Array.Copy(this.m_Strings, 0, objArray1, 0, this.m_Size);
}
this.m_Strings = objArray1;
}
else
{
this.m_Strings = new string[0x10];
}
}
}
}
public string Text
{
get
{
return this.ToString();
}
}
/// <summary>
/// 構造函數
/// </summary>
public StringList()
: this(10)
{
}
/// <summary>
/// 構造函數
/// </summary>
public StringList(int capacity)
{
m_Capacity = capacity;
m_Strings = new string[capacity];
m_Size = 0;
}
public void Dispose()
{
}
/// <summary>
/// 讀取某行內容
/// </summary>
/// <param name="index"></param>
public string this[int index]
{
get
{
if ((index < 0) || (index >= m_Size))
{
throw new ArgumentOutOfRangeException();
}
return this.m_Strings[index];
}
set
{
if ((index < 0) || (index >= m_Size))
{
throw new ArgumentOutOfRangeException();
}
this.m_Strings[index] = value;
}
}
/// <summary>
/// 調整內存大小
/// </summary>
protected void EnsureCapacity(int min)
{
if (this.m_Strings.Length < min)
{
int num1 = (this.m_Strings.Length == 0) ? 0x10 : (this.m_Strings.Length * 2);
if (num1 < min)
{
num1 = min;
}
this.Capacity = num1;
}
}
/// <summary>
/// 追加一行
/// </summary>
public int AppendText(string value)
{
if (this.Count == m_Strings.Length)
{
EnsureCapacity(this.Count + 1);
}
m_Strings[this.Count] = value;
m_Size++;
return m_Size;
}
/// <summary>
/// 插入一行
/// </summary>
/// <param name="index"></param>
public int InsertText(int index, string value)
{
if (index < 0)
{
index = 0;
}
if (this.Count == m_Strings.Length)
{
EnsureCapacity(this.Count + 1);
}
if (index < this.Count)
{
Array.Copy(this.m_Strings, index, this.m_Strings, index + 1, this.m_Size - index);
}
m_Strings[index] = value;
m_Size++;
return m_Size;
}
/// <summary>
/// 查找數据的位置
/// </summary>
public int IndexOf(string value)
{
return Array.IndexOf(this.m_Strings, value, 0, this.m_Size);
}
/// <summary>
/// 刪除一行
/// </summary>
/// <param name="index"></param>
public void RemoveAt(int index)
{
if ((index < 0) || (index >= this.m_Size))
{
throw new ArgumentOutOfRangeException();
}
this.m_Size--;
if (index < this.m_Size)
{
Array.Copy(this.m_Strings, index + 1, this.m_Strings, index, this.m_Size - index);
}
this.m_Strings[this.m_Size] = null;
}
/// <summary>
/// 輸出字符串。
/// </summary>
public override string ToString()
{
System.Text.StringBuilder s = new System.Text.StringBuilder(this.Count);
for (int i = 0; i < this.Count; i++)
{
s.Append(m_Strings[i] + "/r/n");
}
return s.ToString();
}
/// <summary>
/// 輸出字符串。
/// </summary>
/// <param name="startIndex"></param>
/// <param name="count"></param>
/// <returns></returns>
public string ToString(int startIndex, int count)
{
if (startIndex < 0)
{
startIndex = 0;
}
else if (startIndex >= this.Count)
{
return "";
}
if (count <= 0)
{
return "";
}
if (count + startIndex > this.Count)
{
count = this.Count - startIndex;
}
System.Text.StringBuilder s = new System.Text.StringBuilder(this.Count);
for (int i = startIndex; i < count; i++)
{
s.Append(m_Strings[i] + "/r/n");
}
return s.ToString();
}
/// <summary>
/// 清除內容
/// </summary>
public void Clear()
{
this.m_Size = 0;
}
/// <summary>
/// 保存到文件
/// </summary>
/// <param name="fileName"></param>
/// <param name="encoding"></param>
public void SaveToFile(string fileName, System.Text.Encoding encoding)
{
System.IO.StreamWriter sw2 = new System.IO.StreamWriter(fileName, false, encoding);
for (int i = 0; i < this.Count; i++)
{
sw2.Write(m_Strings[i] + "/r/n");
}
sw2.Close();
}
public void SaveToFile(string fileName)
{
System.IO.StreamWriter sw2 = new System.IO.StreamWriter(fileName, false, System.Text.ASCIIEncoding.Default);
for (int i = 0; i < this.Count; i++)
{
sw2.Write(m_Strings[i] + "/r/n");
}
sw2.Close();
}
/// <summary>
/// 導入一文本文件
/// </summary>
/// <param name="fileName"></param>
public void LoadFromFile(string fileName)
{
this.Clear();
System.IO.StreamReader sr2 = new System.IO.StreamReader(fileName, System.Text.ASCIIEncoding.Default);
while (sr2.Peek() >= 0)
{
this.AppendText(sr2.ReadLine());
}
sr2.Close();
}
public void LoadFromFile(string fileName, System.Text.Encoding encoding)
{
this.Clear();
System.IO.StreamReader sr2 = new System.IO.StreamReader(fileName, encoding);
while (sr2.Peek() >= 0)
{
this.AppendText(sr2.ReadLine());
}
sr2.Close();
}
}
2、導出按鈕事件內容:
if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
StringList strlist = new StringList();
int i, j;
try
{
for (i = 0; i < dataGridView1.Columns.Count; i++)
{
str1 = str1 + dataGridView1.Columns[i].HeaderText + '/t';
}
strlist.AppendText(str1);
str1 = "";
for (i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (j = 0; j < dataGridView1.Columns.Count; j++)
{
str1 = str1 + dataGridView1.Rows[i].Cells[j].Value.ToString() + '/t';
}
strlist.AppendText(str1);
str1 = "";
}
strlist.SaveToFile(saveFileDialog1.FileName + ".xls");
MessageBox.Show("文件導出成功﹗");
}
finally
{
strlist.Dispose();
}
}