码迷,mamicode.com
首页 > 其他好文 > 详细

CSV导入导出

时间:2014-10-11 12:07:05      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:io   os   ar   for   on   ad   line   new   as   

        public static DataTable ImportCSV(string strFileName, string SheetName, string[] fields)
        {
            if (strFileName == "") return null;
            string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Text;DATA SOURCE=" + strFileName.Replace("\\" + SheetName, "");
            OleDbDataAdapter ExcelDA = new OleDbDataAdapter("select * from " + SheetName, strConn);
            DataTable dtCSV = new DataTable();
            foreach (string field in fields)
            {
                dtCSV.Columns.Add(field, typeof(string));
            }
            ExcelDA.Fill(dtCSV);
            foreach (DataColumn column in dtCSV.Columns)
            {
                column.ColumnName = column.ColumnName.Trim();
            }
            return dtCSV;
        }
 public static string GenerateCSVReportContent(DataTable dataSource)
        {
            StringBuilder result = new StringBuilder();
            using (System.IO.StringWriter sw = new System.IO.StringWriter(result))
            {
                StringBuilder columns = new StringBuilder();
                for (int i = 0; i < dataSource.Columns.Count; i++)
                {
                    if (i == 0)
                    {
                        columns.Append(dataSource.Columns[i].ColumnName);
                    }
                    else
                    {
                        columns.Append("," + dataSource.Columns[i].ColumnName);
                    }
                }
                sw.WriteLine(columns.ToString());
                foreach (DataRow row in dataSource.Rows)
                {
                    for (int i = 0; i < dataSource.Columns.Count; i++)
                    {
                        if (i == 0)
                        {
                            sw.Write(row[i] == null ? String.Empty : "\"" + row[i].ToString().Replace("\"", "").Replace(",", "").Replace(" ", "") + "\"");
                        }
                        else if (i == dataSource.Columns.Count - 1)
                        {
                            sw.Write(",");
                            sw.WriteLine(row[i] == null ? String.Empty : "\"" + row[i].ToString().Replace("\"", "").Replace(",", "").Replace(" ", "") + "\"");
                        }
                        else
                        {
                            sw.Write(",");
                            sw.Write(row[i] == null ? String.Empty : "\"" + row[i].ToString().Replace("\"", "").Replace(",", "").Replace(" ", "") + "\"");
                        }
                    }
                    sw.Flush();
                }
            }
            return result.ToString();
        }


        
    

 public static string GenerateCSVReportContent(DataTable dataSource)
        {
            StringBuilder result = new StringBuilder();
            using (System.IO.StringWriter sw = new System.IO.StringWriter(result))
            {
                StringBuilder columns = new StringBuilder();
                for (int i = 0; i < dataSource.Columns.Count; i++)
                {
                    if (i == 0)
                    {
                        columns.Append(dataSource.Columns[i].ColumnName);
                    }
                    else
                    {
                        columns.Append("," + dataSource.Columns[i].ColumnName);
                    }
                }
                sw.WriteLine(columns.ToString());
                foreach (DataRow row in dataSource.Rows)
                {
                    for (int i = 0; i < dataSource.Columns.Count; i++)
                    {
                        if (i == 0)
                        {
                            sw.Write(row[i] == null ? String.Empty : "\"" + row[i].ToString().Replace("\"", "").Replace(",", "").Replace(" ", "") + "\"");
                        }
                        else if (i == dataSource.Columns.Count - 1)
                        {
                            sw.Write(",");
                            sw.WriteLine(row[i] == null ? String.Empty : "\"" + row[i].ToString().Replace("\"", "").Replace(",", "").Replace(" ", "") + "\"");
                        }
                        else
                        {
                            sw.Write(",");
                            sw.Write(row[i] == null ? String.Empty : "\"" + row[i].ToString().Replace("\"", "").Replace(",", "").Replace(" ", "") + "\"");
                        }
                    }
                    sw.Flush();
                }
            }
            return result.ToString();
        }


        

CSV导入导出

标签:io   os   ar   for   on   ad   line   new   as   

原文地址:http://www.cnblogs.com/kaleidoscope/p/4018524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!