C # _ export dataTable data to CSV and XLS files

Source: Internet
Author: User
// 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, it mainly stores the ref string err error prompt function for the path exported by string SavePath in the header format: export the DataTable data to the 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)
{
Err = ex. ToString ();

}
} // ******* Author: wesimy time: 07/31/05 version: v.2.1 function name: DatatableToExelFile parameter description: DataTable dt exports xls data string xbkPahth xls template, it mainly stores some headers in the format of string SavePath to export the path ref string err error prompt function: export the DataTable data to a CSV file. Note: Excel namespaces are required. In addition, in different versions of OFFICE, some methods may be incorrect during BUILD. You can check the parameter to determine which method to use .... * ******* // public void DatatableToExcelFile (System. data. dataTable dt, string xbkPath, string SavePath, ref string err)
{
Try
{
Excel. Application excel;
Excel. _ Workbook xBk;
Excel. _ Worksheet xSt;
 
Excel = new Excel. ApplicationClass ();

XBk = excel. Workbooks. Open (@ xbkPath,
Type. Missing,
Type. Missing,
Type. Missing, Type. Missing );
XSt = (_ Worksheet) xBk. Worksheets ["OSI Full inspection result"]; int beginRow = 5;
For (int I = 0; I <dt. Rows. Count; I ++)
{
For (int j = 1; j <dt. Columns. Count + 1; j ++)
{
XSt. Cells [I + beginRow, j] = dt. Rows [I] [J-1];
}
} XSt. SaveAs (@ SavePath, Type. Missing,
Type. Missing, Type. Missing); excel. Visible = false;
If (xBk! = Null)
XBk. Close (false, xbkPath, Type. Missing );
If (xBk! = Null)
System. Runtime. InteropServices. Marshal. ReleaseComObject (xBk );
XBk = null;
If (xSt! = Null)
System. Runtime. InteropServices. Marshal. ReleaseComObject (xSt );
XSt = null;

If (excel! = Null)
{
Excel. Quit ();
System. Runtime. InteropServices. Marshal. ReleaseComObject (excel );
Excel = null;
}
}
Catch (Exception ex)
{
Err = ex. Message;
}

}
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.