winform导出csv

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);
}

posted @   卡玛兹  阅读(1702)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
你好
点击右上角即可分享
微信分享提示