在一些项目中,有一些动态数据保存的需求,保存为CSV。

解决方法如下:

引用:

using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Windows.Forms;

-----------------------------------------------------------

 private  object CsvCodeData_Lock = new object();

/// <summary>
/// 保存数据为CSV文件
/// </summary>
/// <param name="ListData">数据</param>
/// <param name="Header">文件头</param>
/// <param name="FilePath">文件路径</param>
/// <param name="fileType">文件分类</param>
/// <returns></returns>
public bool CsvDataSave(List<SortedList> ListData, SortedList Header, string FilePath, string fileType)
{
bool Re = false;
if (!Directory.Exists(FilePath))
{
Directory.CreateDirectory(FilePath);
}
lock (CsvCodeData_Lock)
{
string dt = DateTime.Now.ToString("yyyy-MM-dd");
string filename = Path.Combine(FilePath, fileType + "-" + dt + ".csv");
// string filename = Path.Combine(CsvCodeFilePath, "电池扫码-" + "-" + dt + ".csv");
StreamWriter sw = null;
try
{
if (!File.Exists(filename))
{
sw = new StreamWriter(filename, false, Encoding.Default);


StringBuilder sb_value = new StringBuilder();

foreach (System.Collections.DictionaryEntry objDE in Header)
{
// sb_field.Append(objDE.Key.ToString() + ",");
sb_value.Append( objDE.Value.ToString() + ",");

}
// sb_field.Remove(sb_field.Length - 1, 1);
sb_value.Remove(sb_value.Length - 1, 1);

sw.WriteLine(sb_value.ToString());

}
else
{
sw = new StreamWriter(filename, true, Encoding.Default);

foreach (SortedList dr in ListData)
{
StringBuilder sb_value2 = new StringBuilder();
foreach (System.Collections.DictionaryEntry objDE in Header)
{
string StrVal = dr[objDE.Key.ToString()].ToString();
sb_value2.Append( StrVal + ",");
}
sb_value2.Remove(sb_value2.Length - 1, 1);
sw.WriteLine(sb_value2.ToString());
}


}
sw.Close();
sw = null;
Re = true;
}
catch (Exception ex)
{
if (sw != null)
{
sw.Close(); sw = null;
}
LogMsg.MsgException(ex.Message, "CsvDataSave");
Re = false;
}
}
return Re;
}

posted @ 2023-03-15 09:41 chenaran 阅读(83) 评论(0) 推荐(0)
摘要: (一)创建服务QuarzServiceusing System.ServiceProcess;using System.Text;using Quartz;using Quartz.Impl;using WinNet.Log;namespace QuarzService{ public par... 阅读全文
posted @ 2015-03-31 11:44 chenaran 阅读(452) 评论(0) 推荐(0)
摘要: using DevExpress.XtraEditors;using DevExpress.XtraEditors.Controls;using DevExpress.XtraGrid;using DevExpress.XtraGrid.Views.Base;private void gridControl1_Leave(object sender, EventArgs e) {DevExpress.XtraGrid.Views.Base.ColumnView view = gridControl1.FocusedView as ColumnView; view.CloseEditor(); 阅读全文
posted @ 2013-04-22 11:24 chenaran 阅读(297) 评论(0) 推荐(0)
摘要: 关于 C# System.Windows.Forms.NumericUpDown 控件,如何禁用鼠标中间键?方法如下:声明一个事件:Num_DiscountAmount.MouseWheel +=new MouseEventHandler(Num_DiscountAmount_MouseWheel);编写一个事件private void Num_DiscountAmount_MouseWheel(object sender, MouseEventArgs e) { HandledMouseEventArgs h = e as HandledMouseEve... 阅读全文
posted @ 2013-03-06 12:18 chenaran 阅读(680) 评论(0) 推荐(0)
摘要: 在使用.NET创建的程序或组件时,元数据(metadata)和代码(code)都存储于“自成一体”的单元中,这个单元称为装配件。我们可以在程序运行期间访问这些信息。在System.Reflection中有这样一个class————Assembly,我们可以通过它来加载一个装配件。方法如下:Assembly assm=Assembly.LoadFrom(fileName);其中filename是要加载的装配件的文件名称(带路径)。接下来,我们就可以通过使用System.Reflection内提供的Info classes来获取装配件中的信息了。首先让我们看一下这些Info classes:M.. 阅读全文
posted @ 2013-02-22 17:37 chenaran 阅读(400) 评论(0) 推荐(0)
摘要: 转载于:http://ipmingsee.blog.163.com/blog/static/7126372012013111923547/或:http://support.microsoft.com/kb/323490/zh-cn 阅读全文
posted @ 2013-02-22 16:23 chenaran 阅读(246) 评论(0) 推荐(0)
摘要: 转载于:http://msdn.microsoft.com/en-us/library/y6dc64f2(v=vs.80).aspx 阅读全文
posted @ 2013-02-22 16:01 chenaran 阅读(254) 评论(0) 推荐(1)
摘要: 有时候写一些操作数据库是,要执行多条Sql语句,不得不用事务处理。用程式方法: public static bool ExecuteSqlTransaction(ArrayList list) { bool yes = false; using (SqlConnection con = new SqlConnection(_ConnectString)) { con.Open(); using (SqlTransaction trans = con.Beg... 阅读全文
posted @ 2013-02-19 17:20 chenaran 阅读(705) 评论(0) 推荐(0)
摘要: private DataTable RowToCol(DataTable Dt_newData) { DataTable Dt_Return = new DataTable(); DataColumn col = new DataColumn(); col.ColumnName = "Title"; col.Caption = "Title"; Dt_Return.Columns.Add(col); List<string> colName = n... 阅读全文
posted @ 2012-09-14 08:51 chenaran 阅读(285) 评论(0) 推荐(0)
摘要: 在combobox 同时显示图片,文字,如图:引用:using System.Windows.Media.Imaging;功能函数如下: /// <summary> /// combobox 下拉同時顯示 圖片文字 /// </summary> /// <param name="_cmbImg"></param> /// <param name="ImgPath"></param> /// <param name="Title"></param& 阅读全文
posted @ 2011-07-27 16:22 chenaran 阅读(439) 评论(2) 推荐(1)
点击右上角即可分享
微信分享提示