出力csv
2024-10-13 23:15:45
public static void ExportResultLog(System.Data.DataTable dt, string fileName, string path)
{
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
} string strPath = path + fileName; StringBuilder strColu = new StringBuilder();
StringBuilder strLineValue = new StringBuilder();
try
{
if (System.IO.File.Exists(strPath))
{
string[] stringlines = System.IO.File.ReadAllLines(strPath, Encoding.Default);
string line = string.Empty;
foreach (string readLine in stringlines)
{
line += readLine + "\r\n";
}
line = line.Remove(line.Length - 2, 2); System.IO.StreamWriter sw = new System.IO.StreamWriter(strPath, false, Encoding.GetEncoding("UTF-8")); sw.WriteLine(line); foreach (DataRow dr in dt.Rows)
{
strLineValue.Remove(0, strLineValue.Length);
for (int i = 0; i <= dt.Columns.Count - 1; i++)
{
strLineValue.Append(ReplaceChar(dr[i] == DBNull.Value ? "" : dr[i].ToString()));
strLineValue.Append(",");
}
strLineValue.Remove(strLineValue.Length - 1, 1);
sw.WriteLine(strLineValue.ToString());
}
sw.Close();
}
else
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(strPath, false, Encoding.GetEncoding("UTF-8")); for (int i = 0; i <= dt.Columns.Count - 1; i++)
{
strColu.Append(dt.Columns[i].ColumnName);
strColu.Append(",");
}
strColu.Remove(strColu.Length - 1, 1);
sw.WriteLine(strColu); foreach (DataRow dr in dt.Rows)
{
strLineValue.Remove(0, strLineValue.Length);
for (int i = 0; i <= dt.Columns.Count - 1; i++)
{
strLineValue.Append(ReplaceChar(dr[i] == DBNull.Value ? "" : dr[i].ToString()));
strLineValue.Append(",");
}
strLineValue.Remove(strLineValue.Length - 1, 1);
sw.WriteLine(strLineValue.ToString());
}
sw.Close();
}
}
catch (Exception ex)
{
throw ex;
} } public static void ExportLog(System.Data.DataTable dtLogInfo, string fileName, string path)
{
string strPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path); if (!System.IO.Directory.Exists(strPath))
{
System.IO.Directory.CreateDirectory(strPath);
} strPath = path + fileName; StringBuilder strColu = new StringBuilder();
StringBuilder strValue = new StringBuilder();
try
{
if (System.IO.File.Exists(strPath))
{
string[] stringlines = System.IO.File.ReadAllLines(strPath, Encoding.Default);
string line = string.Empty;
foreach (string readLine in stringlines)
{
line += readLine + "\r\n";
} System.IO.StreamWriter sw = new System.IO.StreamWriter(strPath, false, Encoding.GetEncoding("UTF-8"));
strValue.Remove(0, strValue.Length);
for (int i = 0; i <= dtLogInfo.Columns.Count - 1; i++)
{
strValue.Append(ReplaceChar(dtLogInfo.Rows[0][i] == DBNull.Value ? "" : dtLogInfo.Rows[0][i].ToString()));
strValue.Append(",");
}
strValue.Remove(strValue.Length - 1, 1);
string value = line + strValue.ToString();
sw.WriteLine(value);
sw.Close();
}
else
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(strPath, false, Encoding.GetEncoding("UTF-8")); for (int i = 0; i <= dtLogInfo.Columns.Count - 1; i++)
{
strColu.Append(dtLogInfo.Columns[i].ColumnName);
strColu.Append(",");
}
strColu.Remove(strColu.Length - 1, 1);
sw.WriteLine(strColu); strValue.Remove(0, strValue.Length);
for (int i = 0; i <= dtLogInfo.Columns.Count - 1; i++)
{
strValue.Append(ReplaceChar(dtLogInfo.Rows[0][i] == DBNull.Value ? "" : dtLogInfo.Rows[0][i].ToString()));
strValue.Append(",");
}
strValue.Remove(strValue.Length - 1, 1);
sw.WriteLine(strValue.ToString()); sw.Close();
}
}
catch (Exception ex)
{
throw ex;
}
}
最新文章
- shift
- 用Google Analytics跟踪JavaScript Errors (译)
- IE中Ajax数据缓存的问题
- ASP无惧上传类不能上传中文双引号文件及ASP函数InStr存在bug
- 表达式语言--在MVC中应用表达式语言
- SuperSocket入门(三)-Telnet多服务实例和服务实例交互配置详解
- 【转载 | 笔记】IIS无法删除应该程序池 因为它包含X个应用程序
- web的分页方法
- axis2与cxf区别
- Apache与Nginx
- Python学习笔记9-多线程和多进程
- redis-master/slave模式
- java.lang.ClassCastException: java.lang.Short cannot be cast to java.lang.String(Short类型无法强转成String类型)
- WEB中会话跟踪[转]
- 在 CSS 中,width 和 height 指的是内容区域的宽度和高度
- DevExpress v17.2新版亮点—DevExtreme篇(一)
- 互评Beta版本——二次元梦之队——“I Do”
- week 4 Vocabulary in paper
- Problem G: 切煎饼
- GB2312简体中文编码表