Datatable is converted into CSV and binary streams, and Excel and transfer are exported.

Source: Internet
Author: User

/// <Summary>
/// 1 Changes to CSV file stream
/// 2 zip Compression
/// </Summary>
/// <Param name = "ds"> </param>
/// <Returns> </returns>
Private byte [] tocsvfilestream (datatable ds, Dictionary <string, Object> DIC)
{
// Replace the Chinese column header name
DS = datatablecolumename_en2ch (DS, DIC );

// Datatable-> CSV data
Byte [] DATA = exportdatatocsvstream (DS );

// After processing, delete and clear the cache
DS = NULL;
GC. Collect ();

// Zip
Byte [] zipdata = csharpziper. zipdata (data, 6 );
Return zipdata;
}

# Region datatalbe-> CSV

///


// export the CSV format [file stream]
///
//
//
private byte [] exportdatatocsvstream (datatable)
{< br> If (datatable = NULL | datatable. columns. count = 0 | datatable. rows. count = 0)
{< br> return NULL;
}

Streamex writer = new streamex (true );
String column = string. empty;
Column = createcolumn (datatable );
Writer. Write. Write (1); // 1 Head
Writer. writestring (column );

String line = string. empty;
Writer. Write. Write (datatable. Rows. Count); // row count
Foreach (datarow row in datatable. Rows)
{
Line = createline (ROW );
Writer. writestring (line );
}

return writer. memorystreambuffer;
}

private string createline (datarow row)
{< br> int COUNT = m_list.count;
stringbuilder sb = new stringbuilder ();
for (INT I = 0; I {< br> metaitem item = m_list [I];
If (item = NULL |! Item. showflag | item. name. tolower (). trim () = "customcolumn_number")
{< br> continue;
}< br> int Index = mapping [item. name];

Object text = Index =-1? Dbnull. Value: Row [item. Name];
If (item! = NULL)
{
If (! Item. showflag)
Continue;
Else if (! (Text is dbnull )&&! String. isnullorempty (item. showformat ))
{
Switch (item. datatype)
{
Case datatype. einteger:
TEXT = (INT) text). tostring (item. showformat );
Break;
Case datatype. elong:
TEXT = (long) text). tostring (item. showformat );
Break;
Case datatype. edouble:
TEXT = (double) text). tostring (item. showformat );
Break;
Case datatype. edatetime:
TEXT = (datetime) text). tostring (item. showformat );
Break;
}
}
}

If (I = count-1)
{
SB. appendformat ("{0}", text );
}
Else
{
SB. appendformat ("{0} {1}", text, splitchar [0]);
}
}

Return sb. tostring ();
}

Dictionary <string, int> mapping = new dictionary <string, int> ();
Private string createcolumn (datatable)
{
Mapping = new dictionary <string, int> ();
Int COUNT = m_list.count;
Stringbuilder sb = new stringbuilder ();
For (INT I = 0; I <count; I ++)
{
Metaitem item = m_list [I];
If (item = NULL |! Item. showflag | item. Name. tolower (). Trim () = "customcolumn_number ")
{
Continue;
}
Mapping [item. Name] = getcolumenameindex (datatable, item. Name );
String text = item. text;
If (I = count-1)
{
SB. appendformat ("{0}", text );
}
Else
{
SB. appendformat ("{0} {1}", text, splitchar [0]);
}
}

Return sb. tostring ();
}

private int getcolumenameindex (datatable, string columanname)
{< br> columanname = columanname. tolower (). trim ();
for (INT I = 0; I {< br> datacolumn Col = datatable. columns [I];
If (Col. columnname. tolower (). trim () = columanname)
{< br> return I;
}< br> return-1;
}

private char [] splitchar
{< br> Get
{< br> return New char [] {'\ t '};
}< BR ># endregion

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.