// Export the file as an svc File
2 public void ExportToSvc (System. Data. DataTable dt, string strName)
3 {
4 string strPath = Path. GetTempPath () + strName + ". csv ";
5
6 if (File. Exists (strPath ))
7 {
8 File. Delete (strPath );
9}
10 // print the header first
11 StringBuilder strColu = new StringBuilder ();
12 StringBuilder strValue = new StringBuilder ();
13 int I = 0;
14
15 try
16 {
17 StreamWriter sw = new StreamWriter (new FileStream (strPath, FileMode. CreateNew), Encoding. GetEncoding ("GB2312 "));
18
19 for (I = 0; I <= dt. Columns. Count-1; I ++)
20 {
21 strColu. Append (dt. Columns [I]. ColumnName );
22 strColu. Append (",");
23}
24 strColu. Remove (strColu. Length-1, 1); // Remove the last character
25
26 sw. WriteLine (strColu );
27
28 foreach (DataRow dr in dt. Rows)
29 {
30 strValue. Remove (0, strValue. Length); // Remove
31
32 for (I = 0; I <= dt. Columns. Count-1; I ++)
33 {
34 strValue. Append (dr [I]. ToString ());
35 strValue. Append (",");
36}
37 strValue. Remove (strValue. Length-1, 1); // Remove the last character
38 sw. WriteLine (strValue );
39}
40
41 sw. Close ();
42}
43 catch (Exception ex)
44 {
45 MessageBox. Show (ex. Message );
46
47}
48
49 System. Diagnostics. Process. Start (strPath );
50
51}
//*******
Author: wesimy
Time: 07/31/05
Version: v.2.1
Function Name: DatatableToCSVFile
Parameter description: DataTable dt exports CSV data
String xbkPahth CSV template, mainly stores some header formats
String SavePath export path
Ref string err error prompt
Function: exports data from a DataTable to a CSV file.
********//
Public void DatatableToCSVFile (System. Data. DataTable dt, string xbkPath, string SavePath, ref string err)
{
String row;
Try
{
String header;
String tmp;
StreamReader sr = new StreamReader (xbkPath );
Header = sr. ReadLine ();
Sr. Close ();
FileStream fs = File. Create (SavePath );
StreamWriter sw = new StreamWriter (fs );
Sw. WriteLine (header );
Foreach (DataRow dr in dt. Rows)
{
Row = "";
For (int I = 0; I <dt. Columns. Count; I ++)
{
If (I! = Dt. Columns. Count-1)
{
Tmp = dr [I]. ToString (). Trim (). Replace (",","");
Row = row + tmp + ",";
}
Else
{
Tmp = dr [I]. ToString (). Trim (). Replace (",",".");
Row = row + tmp;
}
}
Sw. WriteLine (row );
}
Sw. Flush ();
Sw. Close ();
}
Catch (Exception ex)
{