标签: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();
}
标签:io os ar for on ad line new as
原文地址:http://www.cnblogs.com/kaleidoscope/p/4018524.html