如何导出具有特定名称的csv文件?

时间:2015-06-03 07:24:29

标签: c#

我想通过特定名称从数据库导出csv文件,例如患者的数据包含“Ward name”字段为“GENERAL WARD(FEMALE)”,则生成的文件名必须为“GENF.csv”。我尝试使用switch-case但它无法正常工作。如果有任何变化请编辑。

感谢。

以下是代码:

//Function declares methods to export
public void ExportAll() {
    try {
        int i = 0;
        for (int icount = 0; icount <= 400; icount++) {
            object o = saiNathHospitalDataSet.Tables["PatientTable"].Rows[i]["WARD NAME"];
            object obj = saiNathHospitalDataSet.Tables["PatientTable"].Rows[i]["PTNAME"];

            int swichexpression = 9;
            object a = "GENERAL WARD (FEMALE)";
            object b = "GENERAL WARD (MALE)";
            object c = "RECOVERY";
            object d = "SEMI DELUXE 02";
            object e = "SEMI DELUXE 05";
            object f = "SEMI DELUXE 06";
            object g = "ICU";
            object h = "SEMI SPECIAL 03";
            object j = "SEMI SPECIAL 01";

            switch (swichexpression) {
                case 1:
                if (o == a) { 
                    // o = "GENF";
                    DataExport("select * from PatientTable where [WARD NAME] =" + o + "", "GENF.csv");
                }
                break;
                case 2:
                if (o == b) { 
                    // o = "GENM";
                    DataExport("select * from PatientTable where [WARD NAME] =" + o + "", "GENM.csv");
                }
                break;
                case 3:
                if (o == c) {
                    //o = "REC";
                    DataExport("select * from PatientTable where [WARD NAME] ="  + o + "", "REC.csv");
                }
                break;
                case 4:
                if (o == d) { 
                    // o = "SDELX02";
                    DataExport("select * from PatientTable where [WARD NAME] =" + o + "", "SDELX02.csv");
                }
                break;
                case 5:
                if (o == e) { 
                    // o = "SDELX05";
                    DataExport("select * from PatientTable where [WARD NAME] =" + o + "", "SDELX05.csv");
                }
                break;
                case 6:
                if (o == f) { 
                    // o = "SDELX06";
                    DataExport("select * from PatientTable where [WARD NAME] =" + o + "", "SDELX06.csv");
                }
                break;
                case 7:
                if (o == g) { 
                    //o = "ICU";
                    DataExport("select * from PatientTable where [WARD NAME] =" + o + "", "ICU.csv");
                }
                break;
                case 8:
                if (o == h) { 
                    //o = "SSPEC03";
                    DataExport("select * from PatientTable where [WARD NAME] =" + o + "", "SSPEC03.csv");
                }
                break;
                case 9:
                if (o == j) {
                    //string z = "SSPEC01";
                    DataExport("select * from PatientTable where [WARD NAME] =" + o + "", "SSPEC01.csv");
                }
                break;
            }
            //DataExport("select * from PatientTable", "" + s + ".csv");
            i++;
        }
    }
    catch {

    }
}

已编辑::数据导出方法,

  public void DataExport(string SelectQuery, string fileName)
    {
        try
        {
            using (var dt = new DataTable())
            {
                using (var da = new SqlDataAdapter(SelectQuery, con))
                {
                    da.Fill(dt);

                    var header = String.Join(
                        ",",
                        dt.Columns.Cast<DataColumn>().Select(dc => dc.ColumnName));

                    var rows =
                        from dr in dt.Rows.Cast<DataRow>()
                        select String.Join(
                            ",",
                            from dc in dt.Columns.Cast<DataColumn>()
                            let t1 = Convert.IsDBNull(dr[dc]) ? "" : dr[dc].ToString()
                            let t2 = t1.Contains(",") ? String.Format("\"{0}\"", t1) : t1
                            select t2);

                    using (var sw = new StreamWriter(txtreceive.Text + "\\" + fileName))
                    {
                        sw.WriteLine(header);
                        foreach (var row in rows)
                        {
                            sw.WriteLine(row);
                        }
                        sw.Close();
                    }
                }
            }
        }
        catch { }
    }

1 个答案:

答案 0 :(得分:1)

你的开关盒是不正确的,现在你正在混合使用switch case和if语句,但你的switch case总是会转到case 9而你实际上并不需要整数的switch case。您需要将其更改为以下内容:

    string o = saiNathHospitalDataSet.Tables["PatientTable"].Rows[i]["WARD NAME"];

    const string a = "GENERAL WARD (FEMALE)";
    const string b = "GENERAL WARD (MALE)";
    const string c = "RECOVERY";
    const string d = "SEMI DELUXE 02";
    const string e = "SEMI DELUXE 05";
    const string f = "SEMI DELUXE 06";
    const string g = "ICU";
    const string h = "SEMI SPECIAL 03";
    const string j = "SEMI SPECIAL 01";

    switch (o)
    {
        case (a):
            //Do something
            break;
        case (b):
            //Do something
            break;
        default:
            //Do something
            break;
    }

编辑:如果您想为每个病房导出每个病人的文件,则必须将导出方法的foreach循环更改为:

foreach (var row in rows)
{
    using (var sw = new StreamWriter(txtreceive.Text + "\\" + "Some variable to identify different patients" + filename))
    {
        sw.WriteLine(header);
        sw.WriteLine(row);
    }
}

假设每行代表一名患者,您需要每行创建一个文件。