c#数据导出为csv

private void btnexport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if ((this.gvgoods != null))
            {
                if (this.bsgoods.Count == 0) { return; }
                IList goodslist = bsgoods.List as IList;
                IList toSaveList = new List();
                for (int i = 0; i < goodslist.Count; i++)
                {
                    if (goodslist[i].PurchManList == null)
                    {
                        if (goodslist[i].GoodsType != "" && goodslist[i].Code != "")
                        {
                            IList purchManList = AllocatePurchTaskManager.SelectPurchManExited(dbNode, goodslist[i]);
                            goodslist[i].PurchManList = purchManList;
                        }
                    }
                    for (int j = 0; j < goodslist[i].PurchManList.Count; j++)
                    {
                        AllocatePurchTaskInfo toSave = new AllocatePurchTaskInfo();
                        if (goodslist[i].PurchManList[j].ISOrNot == false)
                        { continue; }
                        toSave.ISOrNot = goodslist[i].PurchManList[j].ISOrNot;
                        toSave.PurchManCode = goodslist[i].PurchManList[j].PurchManCode;
                        toSave.PurchManName = goodslist[i].PurchManList[j].PurchManName;
                        toSave.goodsType = goodslist[i].goodsType;
                        toSave.Code = goodslist[i].Code;
                        toSave.Name = goodslist[i].Name;
                        if (goodslist[i].GoodsType == "品类")
                        {
                            toSave.CategoryItemCode = "0000";
                        }
                        toSaveList.Add(toSave);
                    }
                }
                FolderBrowserDialog path = new FolderBrowserDialog();
                path.ShowDialog();
                if (path.SelectedPath == null || path.SelectedPath == "") { return; }
                FileStream fs = new FileStream(path.SelectedPath + @"\采购任务分配设置导出.CSV", FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(fs,UnicodeEncoding.GetEncoding("GB2312"));
                sw.WriteLine("编码类型" + "," + "编码" + "," + "名称" + "," + "品类基本项" + "," + "采购人编码" + "," + "采购人名称" + ",");
                for (int i = 0; i < toSaveList.Count; i++)
                {
                    sw.Write(toSaveList[i].goodsType + ",");
                    sw.Write(toSaveList[i].Code + ",");
                    sw.Write(toSaveList[i].Name + ",");
                    if (toSaveList[i].GoodsType == "品类")
                    {
                        sw.Write("0000" + ",");
                    }
                    else
                    {
                        sw.Write("" + ",");
                    }
                    sw.Write(toSaveList[i].PurchManCode + ",");
                    sw.WriteLine(toSaveList[i].PurchManName);
                }
                sw.Flush();
                sw.Close();
                XtraMessageBox.ShowSuccessMessage("导出成功");
            }
        }

你可能感兴趣的:(c#数据导出为csv)