我希望能夠將datagridview的結果另存為逗號分隔符形式的文本(txt),請問怎麼做?
大哥們,能不能講點詳細的
用我們用的另一種軟件是這樣實現的,但是用c#不知道怎麼樣遍歷datagridview
cds1.COPYSDF(-1,\\172.18.1.26\ACCVOUCH\;
+CDS0.'ResourceDate'+CDS0.'posid'+"("+STR(CDS0.'isbs')+")"+MoneyCode+".txt",",")
F = FOPEN(\\172.18.1.26\ACCVOUCH\;
+CDS0.'ResourceDate'+CDS0.'posid'+"("+STR(CDS0.'isbs')+")"+MoneyCode+".txt",",")
FSEEK(F, 0, 2)
iLength = FTELL(F)
FSEEK(F, 0, 0)
str = FREAD(F, iLength)
FSEEK(F, 0, 0)
FWRITE(F, str, 0) FCLOSE(F)
WCURSOR(1)
[CODE]DataTable dtGrid1 = null; //定义一个数据表
dtGrid1 = (DataTable)this.dataGridView1.DataSource; //获取datagridview里面的数据
SaveFileDialog sf = new SaveFileDialog(); //对话框
sf.Filter = @"txt文件(*.txt)|*.txt|所有文件(*.*)|*.*"; //定义保存的文件的类型
string str = "";
if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
{
if (sf.FileName == "") //如果没有输入文件名
{
return;
}
try
{
StreamWriter sw = null; //文件流
sw = new StreamWriter(sf.FileName, false, Encoding.Unicode);
sw.WriteLine("编号\t\t姓名\t\t年龄\t\t地址"); //写入显示的标题
for (int i = 0; i < dtGrid.Rows.Count; i++) //循环写入dataGridView中的内容
{
for (int j = 0; j < dtGrid.Columns.Count; j++)
{
str = dtGrid2.Rows[i][j].ToString();
sw.Write(str + ",");
}
sw.WriteLine("");
}
sw.Dispose(); //释放资源
sw.Close(); //关闭数据流
MessageBox.Show("数据导出成功!", "系统提示:");
}
catch
{
MessageBox.Show("保存时发生未知错误,请稍后再试!","系统提示:");
}
}[/CODE]
[此贴子已经被作者于2007-2-2 20:19:32编辑过]