Export TXT from Dataset

Source: Internet
Author: User

There are already many files exported from dataset to Word, Excel, txt, etc.ArticleWhy do I need to write this article? Because I haven't found any that meets the requirements after searching for a long time (not necessarily none), I have to write one by myself. I also encountered a problem later. I hope my friends in the garden can help me solve the problem and see if there is any better solution.

In this article, I have two questions about export. The first problem is that the width of each exported column is automatically allocated according to the length of the column. It is still not clear enough. The following example shows the width of each column.

First, construct our dataset. I originally wanted to query the data from the database, so although it is troublesome, it is more direct.

 Dataset DS = New  Dataset (); Datatable Table = New  Datatable (); Datacolumn [] Columns = New  Datacolumn [7]; columns [0] =New  Datacolumn ( "Empno" ); Columns [1] = New  Datacolumn ( "Empname" ); Columns [2] = New  Datacolumn ( "Dept" ); Columns [3] = New  Datacolumn ( "Post" ); Columns [4] = New  Datacolumn ("Salary" ); Columns [5] = New  Datacolumn ( "Bank" ); Columns [6] = New  Datacolumn ( "Bankno" ); Table. Columns. addrange (columns ); Datarow Row1 = table. newrow (); row1 [ "Empno" ] = "1001" ; Row1 [ "Empname" ] = "Andy Lau" ; Row1 ["Dept" ] = "Office" ; Row1 [ "Post" ] = "General Manager" ; Row1 [ "Salary" ] = 6000; row1 [ "Bank" ] = "China Construction Bank" ; Row1 [ "Bankno" ] = "5475694879437594" ; Datarow Row2 = table. newrow (); row2 [ "Empno" ] = "1002" ; Row2 ["Empname" ] = "Zhou Xun" ; Row2 [ "Dept" ] = "Finance" ; Row2 [ "Post" ] = "Cashier" ; Row2 [ "Salary" ] = 4000; row2 [ "Bank" ] = "China Construction Bank" ; Row2 [ "Bankno" ] = "5475695489548395" ; Datarow Row3 = table. newrow (); row3 ["Empno" ] = "1003" ; Row3 [ "Empname" ] = "ABC" ; Row3 [ "Dept" ] = "Information Department" ; Row3 [ "Post" ] = "ProgramMember" ; Row3 [ "Salary" ] = 5000; row3 [ "Bank" ] = "China Construction Bank" ; Row3 [ "Bankno" ] ="5475763489548395" ; Table. Rows. Add (row1); table. Rows. Add (row2); table. Rows. Add (row3); DS. Tables. Add (table );

AboveCodeThere are three data items.

Second, write the export method.

 /// <Summary> ///  Export dataset to txt  /// </Summary> /// <Param name = "ds"> </param> /// <Param name = "FILENAME">  Export TXT file name  </Param> /// <Param name = "page"> </param>  Public static void Totxt ( Dataset DS, String Filename,Page Page ){ Stringbuilder SB = New  Stringbuilder (); Datatable Table = Ds. Tables [0]; Datarowcollection Rows = table. Rows; Datacolumncollection Columns = table. columns; // Number of rows  Int Rowcount = rows. count; // Number of Columns  Int Columncount = columns. count; // Maximum length of each column Int [] Columnmaxlength = New int [Columncount]; // Traverse all records to obtain the maximum length of each column  For ( Int I = 0; I <columncount; I ++ ){ Int [] TMPS = New int [Rowcount]; For ( Int J = 0; j <rowcount; j ++) {TMPS [J] = system. Text. Encoding . Default. getbytecount (rows [J] [I]. tostring ();} columnmaxlength [I] = arraymaxvalue (TMPS );}// Compare the maximum length obtained above with the column name  For ( Int I = 0; I <columncount; I ++ ){ If (Columnmaxlength [I] <system. Text. Encoding . Default. getbytecount (columns [I]. columnname) {columnmaxlength [I] = system. Text. Encoding . Default. getbytecount (columns [I]. columnname );}} // Append the column name to a string  For ( Int I = 0; I <columncount; I ++ ){ Int A = columnmaxlength [I]-system. Text. Encoding . Default. getbytecount (columns [I]. columnname ); Stringbuilder SB1 = New  Stringbuilder (); // The interval between each column is 3.  For ( Int J = 0; j <A + 3; j ++) {sb1.append ( "" );} Sb. append (columns [I]. columnname). append (sb1.tostring ();} sb. appendline (); sb. appendline (); // Append the record to a string  For (Int I = 0; I <rowcount; I ++ ){ For ( Int J = 0; j <columncount; j ++ ){ Int A = columnmaxlength [J]-system. Text. Encoding . Default. getbytecount (rows [I] [J]. tostring ()); Stringbuilder SB1 = New  Stringbuilder (); For ( Int K = 0; k <A + 3; k ++) {sb1.append ("" );} Sb. append (rows [I] [J]). append (sb1.tostring ();} sb. appendline ();} Httpcontext . Current. response. Clear (); Httpcontext . Current. response. Buffer = False ; Httpcontext . Current. response. contentencoding = system. Text. Encoding . Utf8; Httpcontext . Current. response. appendheader ( "Content-disposition" , "Attachment; filename =" +Httpcontext . Current. server. htmlencode (filename) + ". Txt" ); Httpcontext . Current. response. contenttype = "Text/plain" ; Page. enableviewstate = False ; Httpcontext . Current. response. Write (sb. tostring ()); Httpcontext . Current. response. End ();} /// <Summary> ///  Returns the maximum value of an array.  /// </Summary> /// <Param name = "arr"> </param> /// <returns> </returns> Private Static int Arraymaxvalue ( Int [] ARR ){ Int Maxint = arr [0]; For ( Int I = 0; I <arr. length; I ++ ){ If (ARR [I]> maxint) {maxint = arr [I] ;}} Return Maxint ;}

In the above method, we should be able to correct and reduce a lot of code. If you are interested, you can modify it. In the method, I use the byte length of the string, rather than the length of the string. If the length of a string is used, the length of an English letter and a Chinese character is 1. Obviously, the Chinese character must be wide. I found that the width of a Chinese character is almost the same as that of two English letters (looking at it). A Chinese character is equal to two bytes, So it uses the length of bytes.

Finally, the export result is shown in:

 Public static void Totxt (Dataset DS, String Filename, Page Page ){ Stringbuilder SB = New  Stringbuilder (); Datatable Table = Ds. Tables [0]; Datarowcollection Rows = table. Rows; Datacolumncollection Columns = table. columns; Int Rowcount = rows. count; Int Columncount = columns. count;Int K = 1; For ( Int I = 0; I <rows. Count; I ++ ){ Decimal Factsalary = Convert . Todecimal (rows [I] [ "Factsalary" ]); Int A = ( Int ) (Factsalary-factsalary % 5000)/5000; If (A> 0 ){ For ( Int J = 0; j <A; j ++) {sb. append (I + K + J). append ( "," ); Sb. append (rows [I] [ "Bankno" ]. Tostring (). append ( "," ); Sb. append (rows [I] [ "Empname" ]. Tostring (). append ( "," (Sb. append (5000). append ( "," ); Sb. append ( "Official service reimbursement" ); Sb. appendline ();} k + = A; sb. append (I + k). append ( "," ); Sb. append (rows [I] ["Bankno" ]. Tostring (). append ( "," ); Sb. append (rows [I] [ "Empname" ]. Tostring (). append ( "," ); Sb. append (factsalary % 5000). append ( "," ); Sb. append ( "Official service reimbursement" ); Sb. appendline ();} Else {Sb. append (I + k). append ( "," ); Sb. append (rows [I] [ "Bankno" ]. Tostring (). append ( "," ); Sb. append (rows [I] [ "Empname" ]. Tostring (). append ( "," ); Sb. append (rows [I] [ "Factsalary" ]). Append ( "," ); Sb. append ( "Official service reimbursement" ); Sb. appendline ();}} Httpcontext . Current. response. Clear (); Httpcontext . Current. response. Buffer = False ; Httpcontext . Current. response. contentencoding = system. Text.Encoding . Utf8; Httpcontext . Current. response. appendheader ( "Content-disposition" , "Attachment; filename =" + Httpcontext . Current. server. htmlencode (filename) + ". Txt" ); Httpcontext . Current. response. contenttype = "Text/plain" ; Page. enableviewstate = False ; Httpcontext . Current. response. Write (sb. tostring ());Httpcontext . Current. response. End ();}

The second problem is that in each exported TXT file, the total amount cannot exceed 0.1 million, and a single file cannot exceed 5000. If it exceeds 5000, split the file.

I wrote a method to split a single file with more than 5000 entries, but the total amount in a TXT file is no more than 0.1 million. One solution is to create a file first, import a file of no more than 0.1 million to this file, create another file, and continue to import it to this file until the export is complete.

Slightly modify the dataset data above and change the data amount to 10001, and respectively, as shown in the final export result:

 

It's not too early. I hope my friends in the garden can provide better solutions. Thank you!

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.