DataTable Data Export CSV file

Source: Internet
Author: User

public static void Saveasexcel (DataTable dt1)
{
System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog ();//= new SaveFileDialog ();
SfD. Filter = "Export File (*.csv) |*.csv";
SfD. FilterIndex = 0;
SfD. Restoredirectory = true;
SfD. Title = "Export File save path";
SfD. ShowDialog ();
String SystemTime;
SystemTime = DateTime.Now.Year.ToString () +datetime.now.month+datetime.now.day+datetime.now.hour+ Datetime.now.minute+datetime.now.second;
String strFilePath = globalvar.path+ "dataquery\\" + SystemTime + ". csv";//SFD. FileName;
StringBuilder strvalue = new StringBuilder ();
StreamWriter SW = new StreamWriter (new FileStream (strFilePath, filemode.createnew), Encoding.default); Output table Header
Usercontroldataquery bd = new Usercontroldataquery (); The busidetail here is the Busidetail.xaml where you perform the export operation
Bd. Writeheader (SW);
int I, j,k;
UShort Count=0;
foreach (DataRow Dr in Dt1. Rows)
{
Strvalue.remove (0, strvalue.length);
for (i = 0; I <= dt1. columns.count-1; i++)
{
if (i==2)
Count = Convert.touint16 (Dr[i]);
if (i = = 4)
{
Byte[] BValue = (byte[]) dr[i];

ushort[] Uvalue = new Ushort[count];
ushort Max, Min;
max = 0;
min = 0;
for (j = 0; J < Count; J + +)
{
UVALUE[J] = (ushort) (bvalue[2 * j + 1] * + bvalue[2 * j]);
if (Max < uvalue[j])
max = Uvalue[j];
if (min > Uvalue[j])
min = Uvalue[j];
}
Strvalue.append ("Maximum value =" +max+ ";" + "Minimum value =" +min);
Strvalue.append (",");
Strvalue.append (Select.scale);
Strvalue.append (",");
Continue
}

Strvalue.append (Dr[i]. ToString ());
Strvalue.append (",");
if (i = = (dt1. COLUMNS.COUNT-1))
{

for (k=0;k<4;k++)
{
if (k = = 0)
{
Strvalue.append (SELECT.ALARMUP);
Strvalue.append (",");
}
if (k = = 1)
{
Strvalue.append (Select.alarmdown);
Strvalue.append (",");
}
if (k = = 2)
{
Strvalue.append (Select.dangerup);
Strvalue.append (",");
}
if (k = = 3)
{
Strvalue.append (Select.dangerdown);
Strvalue.append (",");
}

}

}

}
Strvalue.remove (strvalue.length-1, 1);//Move out the last, character
Sw. WriteLine (strvalue);
}
Sw. Close ();
System.Windows.MessageBox.Show ("Export file succeeded! "," Success ", messageboxbutton.ok,messageboximage.information);
}//Output table header

private void Writeheader (StreamWriter sw)
{
string Strheader = "Monitoring object, monitoring time, sampling length, sampling frequency, value, unit, status, alarm upper limit, alarm limit, hazard limit, hazard limit";
Sw. WriteLine (Strheader);
}

DataTable Data Export CSV file

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.