码迷,mamicode.com
首页 > Windows程序 > 详细

winform导出csv

时间:2015-08-25 18:58:03      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

public void ExportToSvc1(string strFileName)
{
string strPath = strFileName + ".csv";

StringBuilder strValue = new StringBuilder();
int i = 0;
try
{
Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
sfd.Filter = "导出文件 (*.csv)|*.csv";
sfd.FilterIndex = 0;
sfd.RestoreDirectory = true;
sfd.Title = "导出文件保存路径";
sfd.FileName = strFileName + ".csv";
sfd.ShowDialog();
string strFilePath = sfd.FileName;

if (strFilePath != "")
{
if (File.Exists(strFilePath))
{
File.Delete(strFilePath);
}

StreamWriter sw = new StreamWriter(new FileStream(strFilePath, FileMode.CreateNew), Encoding.GetEncoding("GB2312"));
DataSet ds = new DataSet();
WriteHeader1(sw);
//Date,Line,BatchNo,SackNo.,Shipping No,Weight,
ds = plb.SelectFlightDetailMonthExport(this.dateTimePicker4.Value.ToString("yyyyMM"), cbbline1.SelectedItem.ToString());
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
StringBuilder strColu = new StringBuilder();
string temp = ds.Tables[0].Rows[i][0].ToString();
strColu.Append(ds.Tables[0].Rows[i][0]);
strColu.Append(",");
string temp1 = ds.Tables[0].Rows[i][1].ToString();
strColu.Append(ds.Tables[0].Rows[i][1]);
strColu.Append(",");

strColu.Append(ds.Tables[0].Rows[i][2]);
strColu.Append(",");

strColu.Append(ds.Tables[0].Rows[i][3]);
strColu.Append(",");
strColu.Append(ds.Tables[0].Rows[i][4]);
strColu.Append(",");
strColu.Append(ds.Tables[0].Rows[i][5]);
strColu.Append(",");
sw.WriteLine(strColu);

}
sw.Close();
}
else
{

}

}
catch (Exception ex)
{
throw ex;
}

}

private void WriteHeader1(StreamWriter sw)
{
string strHeader = "Date,Line,BatchNo,SackNo.,Shipping No,Weight,";
sw.WriteLine(strHeader);
}

winform导出csv

标签:

原文地址:http://www.cnblogs.com/panjuan/p/4757608.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!