Export TXT from dataset (continued)

Source: Internet
Author: User

In the TXT export from dataset article, we still have a problem to solve: the total amount of each TXT cannot exceed 0.1 million, and the single amount cannot exceed 50 thousand (5000 in the previous article ).

According to the solution I mentioned in the previous article, first generate a TXT file on the server, and then package and download it to the client.

First, we need two methods: one is to create a file and the other is to compress the file. The following describes the specific methods.Code.

To create a TXT file:

 /// <Summary> ///  Create TXT document  /// </Summary> /// <Param name = "str">  Write TXT content  </Param> /// <Param name = "list">  Path list of generated TXT files  </Param>  Private Static void Createtxtfile ( String STR,List < String > List ){ If (Str. Length = 0 ){ Return ;} String Filename = Guid . Newguid () + ". Txt" ; String Path = @ "F: \ TXT \\" + Filename; List. Add (PATH ); Streamwriter Sr = File . Createtext (PATH); Sr. Write (STR); Sr. Close ();}

File compression method:

Icsharpcode. sharpziplib. dll must be referenced.

 Public static void Compressfile ( String Savepath, String Txtname, List < String > Filenames ){ Zipoutputstream Stream = New  Zipoutputstream ( File . Create (savepath )); // Set the compression level (0-9). The larger the value, the more powerful the compression. Stream. setlevel (8 ); // Traverse all files to be compressed For ( Int I = 0; I <filenames. Count; I ++ ){ If ( File . Exists (filenames [I]) { Filestream FS = File . Openread (filenames [I]); Byte [] Buffer = New byte [Fs. Length]; // Set the buffer size  Int Size = FS. Read (buffer, 0, buffer. Length );// Number of bytes read into the buffer // string entryname = filename. substring (filename. lastindexof ("\") + 1 );  String Entryname = txtname + "(" + (I + 1) + "Cmd.txt" ; Zipentry Entry = New  Zipentry (Entryname); stream. putnextentry (entry); stream. Write (buffer, 0, size ); Try { // If the number of bytes in the read buffer is not as large as the total number of bytes requested  While (Size <fs. Length ){ Int Sizeread = FS. Read (buffer, 0, buffer. Length); stream. Write (buffer, 0, sizeread); size + = sizeread ;}} Catch ( Exception Ex ){ Throw Ex ;} Finally {Fs. Close ();}} // Delete an object  File . Delete (filenames [I]);} stream. Finish (); stream. Close ();}

The following describes how to export a TXT file.

Before that, define a salary class

 Public class Salary { /// <Summary> ///  Employee ID  /// </Summary>  Public String Empno { Get ; Set ;} /// <Summary> ///  Employee name  /// </Summary>  Public String Empname { Get ; Set ;}/// <Summary> ///  Department  /// </Summary>  Public String Dept { Get ; Set ;} /// <Summary> ///  Title  /// </Summary>  Public String Post { Get ; Set ;} /// <Summary> ///  Salary /// </Summary>  Public decimal Salary { Get ; Set ;} /// <Summary> ///  Account Opening bank  /// </Summary>  Public String Bank { Get ; Set ;} /// <Summary> ///  Bank account  /// </Summary>  Public String Bankno { Get ; Set ;}}

Write another method to generate a single TXT document string

 Public static string Gentxtstring ( List < Salary > List ){ Stringbuilder SB = New  Stringbuilder (); For ( Int I = 0; I <list. Count; I ++) {sb. append (I + 1). append ( "," ); Sb. append (list [I]. empno). append ("," ); Sb. append (list [I]. empname). append ( "," ); Sb. append (list [I]. Dept). append ( "," ); Sb. append (list [I]. Post). append ( "," ); Sb. append (list [I]. Salary). append ( "," ); Sb. append (list [I]. Bank). append ( "," ); Sb. append (list [I]. bankno). append ( "," ); Sb. appendline ();} Return SB. tostring ();}

How to export txt

 Public static void Totxt (Dataset DS, String Filename ){ 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; // Sum of amounts  Decimal Totalsalary = 0;// TXT file path list  List < String > Filelist = New  List < String > (); // Salary list  List < Salary > Salarylist = New  List < Salary > (); // Traverse all rows  For (Int I = 0; I <rows. Count; I ++ ){ // Salary  Decimal Salary = Convert . Todecimal (rows [I] [ "Salary" ]); // Fixed value 50 thousand  Decimal C = 50000.00 m; // The salary is composed of several hundred thousand  Int A = ( Int ) (Salary/C ); // The salary is less than or equal to 0.1 million  If (Salary <= 100000) {totalsalary + = salary; If (Totalsalary> 100000 ){ // If the total amount is greater than 0.1 million, create a new txt Createtxtfile (gentxtstring (salarylist), filelist); totalsalary = salary; salarylist. Clear ();} If (A> 0 ){ For ( Int J = 0; j <A; j ++) {salarylist. Add ( New  Salary () {Empno = rows [I] [ "Empno" ]. Tostring (), empname = rows [I] [ "Empname" ]. Tostring (), DEPT = rows [I] [ "Dept" ]. Tostring (), post = rows [I] [ "Post" ]. Tostring (), factsalary = C, bank = rows [I] [ "Bank" ]. Tostring (), bankno = rows [I] [ "Bankno" ]. Tostring ()});} // The remainder is not 0.  If (Salary % C! = 0) {salarylist. Add ( New  Salary () {Empno = rows [I] [ "Empno" ]. Tostring (), empname = rows [I] [ "Empname" ]. Tostring (), DEPT = rows [I] [ "Dept" ]. Tostring (), post = rows [I] [ "Post" ]. Tostring (), factsalary = salary % C, bank = rows [I] [ "Bank" ]. Tostring (), bankno = rows [I] [ "Bankno" ]. Tostring ()});}} Else {Salarylist. Add ( New  Salary () {Empno = rows [I] [ "Empno" ]. Tostring (), empname = rows [I] ["Empname" ]. Tostring (), DEPT = rows [I] [ "Dept" ]. Tostring (), post = rows [I] [ "Post" ]. Tostring (), factsalary = salary, bank = rows [I] [ "Bank" ]. Tostring (), bankno = rows [I] [ "Bankno" ]. Tostring ()});}} // The salary is higher than 0.1 million  Else { For ( Int J = 0; j <A; j ++) {totalsalary + = C; If (Totalsalary> 100000) {createtxtfile (gentxtstring (salarylist), filelist); totalsalary = C; salarylist. Clear ();} salarylist. Add ( New  Salary () {Empno = rows [I] [ "Empno" ]. Tostring (), empname = rows [I] [ "Empname" ]. Tostring (), DEPT = rows [I] [ "Dept" ]. Tostring (), post = rows [I] [ "Post" ]. Tostring (), factsalary = C, bank = rows [I] [ "Bank" ]. Tostring (), bankno = rows [I] [ "Bankno" ]. Tostring ()});} If (Salary % C! = 0) {totalsalary + = salary % C; If (Totalsalary> 100000) {createtxtfile (gentxtstring (salarylist), filelist); totalsalary = salary % C; salarylist. Clear ();} salarylist. Add ( New  Salary () {Empno = rows [I] [ "Empno" ]. Tostring (), empname = rows [I] [ "Empname" ]. Tostring (), DEPT = rows [I] [ "Dept" ]. Tostring (), post = rows [I] ["Post" ]. Tostring (), factsalary = salary % C, bank = rows [I] [ "Bank" ]. Tostring (), bankno = rows [I] [ "Bankno" ]. Tostring ()});}}} If (Totalsalary <= 100000) {createtxtfile (gentxtstring (salarylist), filelist);} compressfile ( @ "F: \ TXT \" + Filename + ". Zip" , Filename, filelist );}

Construct a dataset with the following data.

Empno Empname Dept Post Salary Bank Bankno
1000 Andy Lau Office General Manager 110000 Bank of Communications 55784756584762
1001 Zhou xingchi Office Vice President 105000 China Construction Bank 58476594865764
1002 Wang Lihong Finance Manager 90000 Agricultural Bank 95486437546534
1003 Nicholas Tse Finance Cashier 6000 ICBC 59583457547336
1004 Wu yanzu Software Department ProgramEmployee 10300 China Construction Bank 59445473365484
1005 Lin Zhiying Software Department Engineer 24000 Bank of China 95584744956852
1006 He rundong Software Department Engineer 30000 Agricultural Bank 95483574775483
1007 Gu tianle Software Department Intern 3000 Agricultural Bank 95483474454499
1008 Zheng yijian Software Department Intern 5000 Agricultural Bank 43945748374934

The above data is left blank and can be replaced with any data.

Call the totxt method in the main method.

 
Txt. Totxt (DS,"Export TXT test");Console. Writeline ("Exported");

After running, a compressed file is generated under F: \ TXT.

Double-click TXT test. Zip to view the generated TXT file, as shown in:

In this way, every TXT generated will not exceed 0.1 million. If you are interested, you can test the test for me to see if there is anything wrong!

The source code is provided below: Download

Before running the program, create a TXT folder under drive F, or modify the path in the program.

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.