C#将DataTable内容导出导csv文件

     #region 将DataTable内容导出导csv文件
        public static void dataTableToCsv(DataTable table, string file)
        {
            FileInfo fi = new FileInfo(file);
            string path = dataTablezdy(file);//另存为
            string name = fi.Name;
            //\/:*?"<>|
            //把文件名和路径分别取出来处理
            name = name.Replace(@"\", "\");
            name = name.Replace(@"/", "/");
            name = name.Replace(@":", ":");
            name = name.Replace(@"*", "*");
            name = name.Replace(@"?", "?");
            name = name.Replace(@"<", "<");
            name = name.Replace(@">", ">");
            name = name.Replace(@"|", "|");
            string title = "";

            FileStream fs = new FileStream(path + "\\" + name, FileMode.Create);
            StreamWriter sw = new StreamWriter(new BufferedStream(fs), System.Text.Encoding.Default);

            for (int i = 0; i < table.Columns.Count; i++)
            {
                switch (table.Columns[i].ColumnName)
                {
                    case "drcode":
                        table.Columns[i].ColumnName = "药品编码";
                        break;
                    case "drname":
                        table.Columns[i].ColumnName = "名称";
                        break;
                    case "drmodel":
                        table.Columns[i].ColumnName = "药品规格";
                        break;
                    case "packunit":
                        table.Columns[i].ColumnName = "包装单位";
                        break;
                    case "dilotn":
                        table.Columns[i].ColumnName = "批号";
                        break;
                    case "drmanf":
                        table.Columns[i].ColumnName = "厂家";
                        break;
                    case "npp":
                        table.Columns[i].ColumnName = "进价";
                        break;
                    case "realsent":
                        table.Columns[i].ColumnName = "数量";
                        break;
                    case "mzhs":
                        table.Columns[i].ColumnName = "门诊换算系数";
                        break;
                    case "zyhs":
                        table.Columns[i].ColumnName = "住院换算系数";
                        break;
                    case "sqh":
                        table.Columns[i].ColumnName = "单据号";
                        break;
                    case "grgs":
                        table.Columns[i].ColumnName = "购货公司";
                        break;
                    case "tprice":
                        table.Columns[i].ColumnName = "零售总价";
                        break;
                    case "tpunits":
                        table.Columns[i].ColumnName = "进价总价";
                        break;
                    case "pack":
                        table.Columns[i].ColumnName = "包装规格";
                        break;
                    case "drunit":
                        table.Columns[i].ColumnName = "最小单位";
                        break;
                }
                title += table.Columns[i].ColumnName + ",";
            }
            title = title.Substring(0, title.Length - 1) + "\n";
            sw.Write(title);

            foreach (DataRow row in table.Rows)
            {

                string line = "";
                for (int i = 0; i < table.Columns.Count; i++)
                {
                    line += row[i].ToString().Replace(",", "") + ",";
                }
                if (line.Contains("\n") || line.Contains("\r"))
                {
                    line = line.Replace("\r", "").Replace("\n", "");
                }
                line = line.Substring(0, line.Length - 1) + "\n";

                sw.Write(line);
            }

            sw.Close();
            fs.Close();
        }

        private static string dataTablezdy(string file)
        {

            string filetext = "";
            SaveFileDialog x = new SaveFileDialog();
            x.FileName = file;
            x.Filter = "所有文件(*.*)|*.*";
            if (x.ShowDialog() == DialogResult.OK)
            {
                string filelocal = x.FileName.ToString();
                filetext = filelocal.Substring(0, filelocal.LastIndexOf("\\"));

            }
            else
            {
                FileInfo fi = new FileInfo(file);
                filetext = fi.DirectoryName;
            }
            return filetext;
        }
        #endregion
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值