0

我需要帮助才能使用 linq 将数据导出到 Excel。我从 linq 获得了数据,但不知道如何导出数据。我的代码如下

 using (DataClassesDataContext dc = new DataClassesDataContext())
    {
        foreach (var mt in dc.Mapping.GetTables())
        {
            string sql = String.Format("Select * from {0} where ID = {1} ", mt.TableName, TenantID);
             var data = dc.ExecuteQuery(mt.RowType.Type, sql);
            //data is here now.
            string path = "D:\\CSvFiles\\";
            if (!System.IO.Directory.Exists(path))
                System.IO.Directory.CreateDirectory(path);

            path = path + mt.TableName + ".csv";
            foreach (var item in data)
            {
                System.IO.File.WriteAllText(path, item.ToString());

            }
        }
    }
4

1 回答 1

0

尝试这个:

using (DataClassesDataContext dc = new DataClassesDataContext())
    {
        foreach (var mt in dc.Mapping.GetTables())
        {
            string sql = String.Format("Select columnName from {0} where ID = {1} ", mt.TableName, TenantID);
             var data = dc.ExecuteQuery(mt.RowType.Type, sql).ToArray();
            //data is here now.
            string path = "D:\\CSvFiles\\";
            if (!System.IO.Directory.Exists(path))
                System.IO.Directory.CreateDirectory(path);

            path += mt.TableName + ".csv";
            System.IO.File.WriteAllText(path, String.Join(",",data));
        }
    }
于 2013-06-29T16:15:39.973 回答